Page 1 of 1

PHP MIME - email text and attachment

Posted: Wed Aug 05, 2009 10:18 am
by mt68
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

Re: PHP MIME - email text and attachment

Posted: Wed Aug 05, 2009 10:32 am
by synking
The page i used to get all of this done is

http://www.webcheatsheet.com/PHP/send_e ... chment.php

Re: PHP MIME - email text and attachment

Posted: Wed Aug 05, 2009 11:23 am
by mt68
Many thanks for your help. You have helped me move a long way forward, but the plain text of the email body is still not being sent (the attachement is fine).

I'm using PHP to construct the main body text of the email, like below.......I'm obviously no following all of the correct rules....

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit



<?php
my code to construct the plain text
?>



--PHP-alt-<?php echo $random_hash; ?>--