Reputation: 1433
I can send just text fine, just html fine, but when I combine the two as follows the email sends but the body is empty.
$to = "[email protected]";
$subject = "Welcome";
$boundary = md5(date('U'));
$headers = "From: Company <[email protected]>" . "\n".
"X-Mailer: PHP/".phpversion() ."\n".
"MIME-Version: 1.0" . "\n".
"Content-Type: multipart/alternative; boundary=".$boundary. "\n";
"Content-Transfer-Encoding: 7bit". "\n";
// TEXT EMAIL PART
$text = "Congratulations";
// HTML EMAIL PART
$html = "<html><body>\n";
$html .= "<div>Congratulations</div>";
$html .= "</body></html>\n";
$message = "Multipart Message coming up" . "\n\n".
"--".$boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"" .
"Content-Transfer-Encoding: 7bit".
$text.
"--".$boundary.
"Content-Type: text/html; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit".
$html.
"--".$boundary."--";
mail($to, $subject, $message, $headers);
Upvotes: 0
Views: 3916
Reputation: 943556
$boundary
iswas undefined, you don't have a line break before or after any of the boundaries, and you don't have a blank line between the headers and body of each part (or a line break after any of those headers).
Don't handroll MIME. I'm sure there is a decent library for PHP that will do it for you.
Upvotes: 0
Reputation: 360672
You've forgotten line breaks after your Content-Type and Content-Transfer-Encoding lines, so the beginnings of your body content are co-mingled with the headers:
"--".$boundary.
^^^--- missing \n
"Content-Transfer-Encoding: 7bit".
^^^--- missing \n\n
$text.
As stated in the comments above, use Swiftmailer or PHPMailer to do this. They'll take care of all the piddle little details, and let you send the entire mail in far fewer lines of code.
Upvotes: 1