So I have the following code. It’s supposed to send HTML emails that were edited in the precedent screen. However, once the email is sent, in html tags, the first double quote and letter of each tag go missing, for instance,
<span style="something">
becomes
<span style=omething">
The odd part is, the message is printed fine if echoed to the screen, so I don’t think it has anything to do with how it’s being sent to the whole function, but how it’s being sent to the mail() function…but I have no clue what the issue could be. Here is the code, without all the getting of variables and such because i don’t think it’s particularly relevant.
//start of the headers
$headers = "From: $from_name<$from_email>\n";
$headers .= "Reply-To: <$reply_to>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: $from_name<$from_email>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <$from_email>\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html\r\n"; //changed to support html
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
/* note: add HTML by changing the Content-Type to text/html */
////TEST 1 - this is where i attempted to hardcode some code in to see if it would send properly: it wouldn't. printed fine though.
$message .="<font class=\"Apple-style-span\" face=\"'Courier New'\" size=\"5\">\n";
$message .= "bbbbb<b>bbbb<i>bbb<u>bbb<font class=\"Apple-style-span\" color=\"#000099\">\n";
$message .= "bbbbb<span class=\"Apple-style-span\" style=\"background-color: rgb(102, 255, 204);\">bbbbbb</span></font></u></i></b></font>\n";
/////end test 1
$message .= $body.'\n';
$message .= "\n\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
if($file_present==1){ // added this to fix empty file bug
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
}
echo $message; //this is where it echoes fine, with all tags intact
// send the message
if( mail("$to_name<$to_email>", $subject, $message, $headers)){ //this is where it goes wrong.
echo("<p>Message successfully sent to ".$to_email.".</p>");
} else {
echo("<p>Message delivery to ".$to_email." failed.</p>");
}
Well, I’ve been going through my old questions. Figured I should post how I solved this. My initial assumption for this was right: there was nothing wrong with the body of the message. The issue was…
The headers! No, it doesn’t really make sense to me. But after I initially removed the X-Sender/related header information, everything started sending almost fine. Then I changed the boundary to a random value, and suddenly everything worked fine. Anyone who can explain to me why this happens is welcome to, but I’m just glad it works.