realtebo
realtebo

Reputation: 25671

PHP: Sending an html plus plain text email I got mail with empty body

This is php code

    $boundary = uniqid("np");

    $headers = "MIME-Version: 1.0\n" ;
    $headers .= "From: $from\n";
    $headers .= "To: $to\n";
    if (!is_null($reply_to)) $headers .= "Reply-To: $reply_to \n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\n";


    //here is the content body
    $message = "This is a MIME encoded message.";
    $message .= "\n\n--" . $boundary . "\n";
    $message .= "Content-type: text/plain;charset=utf-8\n\n";

    //Plain text body
    $message .= "Hello,\nThis is a text email, the text/plain version.
    \n\nRegards,\nYour Name";
    $message .= "\n\n--" . $boundary . "\n";
    $message .= "Content-type: text/html;charset=utf-8\n\n";

    $message .= "Hello, <BR> This is a text email, the <bold>html version</bold>.";
    $message .= "<BR>Regards";
    $message .= "<BR>Your Name";
    $message .= "\n\n--" . $boundary . "--";

    $sendmail = mail($to, $title, $message, $headers);

$headers variable contains:

MIME-Version: 1.0
From: Italia S.p.a. <[email protected]>
To: My Name <[email protected]>
Reply-To: My Alt Name <[email protected]> 
Content-Type: multipart/alternative;boundary=np536b88fc27327

$message variable contains:

This is a MIME encoded message.

--np536b88fc27327
Content-type: text/plain;charset=utf-8

Hello,
This is a text email, the text/plain version.


Regards,
Your Name

--np536b88fc27327
Content-type: text/html;charset=utf-8

Hello, <BR> This is a text email, 
the <bold>html version</bold>.<BR>Regards<BR>Your Name

--np536b88fc27327--

Problem is: when sending using mail($from, $to, $message, $headers) mail is delivered, but it's empty

Upvotes: 1

Views: 491

Answers (2)

Sumit Bijvani
Sumit Bijvani

Reputation: 8179

Header should be..

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

Check PHP mail() function.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Email headers must be separated by \r\n.

Upvotes: -1

Related Questions