Forum Moderators: coopster
I'm trying to get a PHP script working for return customers to a friend's business - they type in a custom message, their email address and their friend's email address and it sends both of them a coupon for $20 off their next visit (coupon is in PDF format). I almost have it but am having some trouble - currently, if the client is Yahoo mail, the message comes through with an attachment but no message body; With Gmail, the body comes through with all the headers inline and no attachment; With Thunderbird, the body and attachments come through but there's also another attachment with additional header information. Is there something simple I'm missing? This should be simple, I would think. I'm using some code I found on another forum, modified to suit my needs. What do you think? Thanks in advance.
<?php
$emailaddress=$_POST['emailaddress'];
$fromaddress=$_POST['fromaddress'];
$emailsubject=$_POST['emailsubject'];
$body=$_POST['body'];
function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
{
$eol="\r\n";
$mime_boundary=md5(time());
# Common Headers
$headers .= 'From: <'.$fromaddress.'>'.$eol;
$headers .= 'Subject: <'.$emailsubject.'>'.$eol;
$headers .= 'Return-Path: <'.$fromaddress.'>'.$eol; // these two to set reply address
$headers .= 'Reply-To: <'.$fromaddress.'>'.$eol;
$headers .= "Message-ID: <".$now." info@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$headers .= 'Content-Type: multipart/mixed; boundary='.$mime_boundary."\"".$eol;
$headers .= 'MIME-Version: 1.0'.$eol;
$msg = "";
if ($attachments!== false)
{
for($i=0; $i < count($attachments); $i++)
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
$handle=fopen($attachments[$i]["file"], 'rb');
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; //! This line needs TWO end of lines! IMPORTANT!
$msg .= $f_contents.$eol.$eol;
}
}
}
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used!
$to = $emailaddress.','.$fromaddress;
mail($to, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
echo "mail send";
}
#$fromaddress = "info@example.com";
$emailsubject="$20 off your next stay with xoxo company";
$attachments = Array(
Array("file"=>"./referral_coupon.pdf", "content_type"=>"application/pdf")
);
send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
?>
[edited by: eelixduppy at 7:41 pm (utc) on Nov. 7, 2007]
[edit reason] use example.com [/edit]
Have you tried using the open source version of sendmail [google.com]. I haven't come across anything that it couldn't handle on all of my projects so far.
If you want to proceed with your own code, you need to tweak the headers and keep on testing it a few times to get it right.
Habtom