Hi Everyone,
I was wondering if A) I could get a second set of eyes on this, and 😎 if there is some way to test for these sort of issues so that I could try to figure it out in the future.
The assorted attachments seem to work out fine, but I can’t get the message to appear in the email body. Upon submission I have the variables for the content print out and they do as expected, but for some reason I just can’t get it to appear within the email.
Unfortunately this doesn’t generate any errors, so I don’t really know how to test for them…
the code is on a unix box, and I am receiving the emails through Outlook 2007
function send_email_form () {
$eol = "\r\n";
// set variables
$name_to = $_POST["name_to"];
$email_to = $_POST["email_to"];
$name_from = $_POST["name_from"];
$email_from = $_POST["email_from"];
$user_message = $_POST["user_message"];
$formatted_message = "<html>\n
<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:14px; color:#666666;\">\n
$user_message<br>\n
</body>\n
</html>\n";
$filename = $_FILES['attachment']['name'];
$filetype = $_FILES["attachment"]["type"];
$filesize=$_FILES["attachment"]["size"];
$tmp = $_FILES['attachment']['tmp_name'];
// if (!($fp = fopen($tmp, 'rb'))) echo "File failed to open";
$fp = fopen($tmp, "rb");
$fdata = fread($fp, $filesize);
$fdata = chunk_split(base64_encode($fdata)); // encode data into text form
fclose($fp);
print("<p>tmp: $tmp</p>");
print("<p>filename: $filename</p>");
print("<p>fdata: $fdata</p>");
print("<p>filetype: $filetype</p>");
print("<p>filesize: $filesize</p>");
// something to add br tags for email
// set unique mime boundary
$mime_boundary = md5(time());
// Headers for email
$to = "$email_to";
$subject = $_POST["subject"];
$headers .= "From: ".$name_from." <".$email_from.">".$eol;
$headers .= "Reply-To: ".$name_from." <".$email_from.">".$eol;
$headers .= "Return-Path: ".$name_from." <".$email_from.">".$eol; // these two to set reply address
$headers .= "Message-ID: <".time()."-".$email_from.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
// Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;
// Open first part of the mail
$message = "--".$mime_boundary.$eol;
// open a mime boundary for the text-html section
$multialt_mime_boundary = $mime_boundary."_multialt";
$message .= "Content-Type: multipart/alternative; boundary=\"".$multialt_mime_boundary."\"".$eol.$eol;
// Text Email Section
$message = "--".$multialt_mime_boundary.$eol;
$message .= "Content-Type: text/plain; charset=UTF-8".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "$user_message".$eol.$eol;
// HTML Email Section
$message .= "--".$multialt_mime_boundary.$eol;
$message .= "Content-Type: text/html; charset=UTF-8".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "$formatted_message".$eol.$eol;
// close the text/html Message Boundary
$message .= "--".$multialt_mime_boundary."--".$eol.$eol;
// Attachment Start
$message .= "--".$mime_boundary.$eol;
$message .= "Content-Type: ".$filetype."; name=\"".$filename."\"".$eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Description: ".$filename.$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol;
$message .= $fdata.$eol.$eol;
$message .= "--".$mime_boundary."--".$eol.$eol;
// Send the email and display result message
$mail_sent = mail($to, $subject, $message, $headers);
echo $mail_sent ? "Your Message has been sent<br />$formatted_message<br />$user_message" : "Mail failed";
}
I have some print()s and echos just so I can verify certain variables why I test, but for the life of me I can't figure it out.
To my knowledge my boundaries are set correctly, so as we all think in these situations it "should" work.
Appreciate any help I can get!