PHP MIME - email text and attachment
Posted: Wed Aug 05, 2009 10:18 am
I spent an inordinate amount of time trawling the web reading white papers, online mailer classes, tutorials, etc, etc, but I seem to be so far away of constructing my PHP script to prepare and send a text and attachment email.
Below is the code I've constructed from all of the reading above. I'm no longer receiving any PHP errors, but it seems as though nothing happens when I run the following script. I have checked the specified file exists in the designated folder, and I've double checked my code structure against a number of different online mailer calsses, and to be truthful I'm at a total loss.
Can anyone help, and perhaps explain what part(s) are incorrect and why? I would be grateful.
THE CODE extract..........
$to = "name@domain";
$subject = "A test email";
$random_hash = md5(date('r', time()));
$headers = "From: name@domain";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("../subfolder/attach.zip")));
//sample of text
$headers .= "\r\nContent-Type: text/plain; charset='iso-8859-1'; boundary=\"PHP-text-".$random_hash."\"";
$headers .= "Content-Transfer-Encoding: 7bit";
$message='\n';
$message.= $_POST['FirstName'];
$message.=' ';
$message.= $_POST['LastName'];
$message.=' of ';
$message.= $_POST['CoName'];
$message.=' has submitted the following Form details from your web site\n\n';
//end sample of text
$headers .= "--PHP-text--$random_hash--";
$headers .= "\r\nContent-Type: application/zip; name=attach.zip; charset='iso-8859-1'; boundary=\"PHP-attach-".$random_hash."\"";
$headers .= "Content-Transfer-Encoding: base64";
$headers .= "Content-Disposition: attachment";
$attachment;
$headers .= "--PHP-attach--$random_hash--";
$headers .= "--PHP-mixed-$random_hash--";
if(mail($to, $subject, $message, $headers)) {
//the rest of my tested mail parameters follow from here
Below is the code I've constructed from all of the reading above. I'm no longer receiving any PHP errors, but it seems as though nothing happens when I run the following script. I have checked the specified file exists in the designated folder, and I've double checked my code structure against a number of different online mailer calsses, and to be truthful I'm at a total loss.
Can anyone help, and perhaps explain what part(s) are incorrect and why? I would be grateful.
THE CODE extract..........
$to = "name@domain";
$subject = "A test email";
$random_hash = md5(date('r', time()));
$headers = "From: name@domain";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents("../subfolder/attach.zip")));
//sample of text
$headers .= "\r\nContent-Type: text/plain; charset='iso-8859-1'; boundary=\"PHP-text-".$random_hash."\"";
$headers .= "Content-Transfer-Encoding: 7bit";
$message='\n';
$message.= $_POST['FirstName'];
$message.=' ';
$message.= $_POST['LastName'];
$message.=' of ';
$message.= $_POST['CoName'];
$message.=' has submitted the following Form details from your web site\n\n';
//end sample of text
$headers .= "--PHP-text--$random_hash--";
$headers .= "\r\nContent-Type: application/zip; name=attach.zip; charset='iso-8859-1'; boundary=\"PHP-attach-".$random_hash."\"";
$headers .= "Content-Transfer-Encoding: base64";
$headers .= "Content-Disposition: attachment";
$attachment;
$headers .= "--PHP-attach--$random_hash--";
$headers .= "--PHP-mixed-$random_hash--";
if(mail($to, $subject, $message, $headers)) {
//the rest of my tested mail parameters follow from here