PHP MIME - email text and attachment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mt68
Forum Newbie
Posts: 5
Joined: Wed Aug 05, 2009 10:06 am

PHP MIME - email text and attachment

Post 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
synking
Forum Newbie
Posts: 7
Joined: Wed Aug 05, 2009 10:10 am

Re: PHP MIME - email text and attachment

Post by synking »

The page i used to get all of this done is

http://www.webcheatsheet.com/PHP/send_e ... chment.php
mt68
Forum Newbie
Posts: 5
Joined: Wed Aug 05, 2009 10:06 am

Re: PHP MIME - email text and attachment

Post 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; ?>--
Post Reply