Sending an e-mail with an attachment - having trouble...
Posted: Tue Oct 06, 2009 5:41 pm
Hi,
I'm fairly new to php, and I'm trying to do an auto-respond type email when someone submits a form. I need the email body to have some text in it and then a pdf attached as well. I've pieced together this code from tutorials online, so it may be way off...but I successfully receive the email with the pdf attached, but no matter what I do, the body of the email is blank, no text at all.
Could someone help me with this please?? Thank you in advance!
Steve
I'm fairly new to php, and I'm trying to do an auto-respond type email when someone submits a form. I need the email body to have some text in it and then a pdf attached as well. I've pieced together this code from tutorials online, so it may be way off...but I successfully receive the email with the pdf attached, but no matter what I do, the body of the email is blank, no text at all.
Code: Select all
$file = 'filename.pdf';
//create a boundary string.
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = 'From: ' . $replyFrom . "\r\nReply-To: " . $replyTo;
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
//read the atachment file contents into a string, encode it with MIME base64
$attachment = chunk_split(base64_encode(file_get_contents($file)));
//define the body of the message.
$replybody = "Thank you for having an interest in our program.\n\n";
$replybody .= "Please view the attachment to learn about more the specific prices. Call us for more info!\n\n";
$emailBody = "--PHP-mixed-{$random_hash}\r\n"
. "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
. "--PHP-alt-{$random_hash}\r\n"
. "Content-Type: text/html; charset=\"iso-8859-1\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "{$replybody}\r\n\r\n"
. "--PHP-alt-{$random_hash}\r\n"
. "--PHP-mixed-{$random_hash}\r\n"
. "Content-Type: application/pdf; name=\"Copier Packages.pdf\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment\r\n\r\n"
. "{$attachment}\r\n"
. "--PHP-mixed-{$random_hash}--\r\n\r\n";
$emailBody = wordwrap($emailBody, 75);
//send the email - these variable are defined earlier in the code...
$mail_sent = mail($replyEmail, $replySubject, $emailBody, $headers);
//if the message is sent successfully redirect back to page
if(!$mail_sent) {
header('index.html');
exit;
}
Steve