Problem sending email in HTML

Hello,
Here is my script to send emails in text and HTML format, but the problem is that I have no content for HTML email when the email client supports it.
Can you tell me if you find any errors (or advise) in my code.

Here’s my PHP code:

<?php
   $frontier = "-----=" . md5(mt_rand());
   $to = "me@mail.com";
   $subject = "My Subject";
   $text = "Hello World";
    
   $html_body = <<<EOF
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div style=\\"text-align:center\\">$text</div>
      </body>
    </html>
EOF;

    $headers = "From: <{$_SERVER['SERVER_ADMIN']}>\\r\
";
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\\r\
";
    $headers .= "MIME-Version: 1.0\\r\
";
    $headers .= "Content-Type: multipart/alternative; boundary=\\"$frontier\\"";
    
    // text
    $body = "--$frontier\\r\
";
    $body .= "Content-Type: text/plain; charset=\\"utf-8\\"\\r\
";
    $body .= "Content-Transfer-Encoding: 8bit\\r\
";
    $body .= $text . "\\r\
";
    
    // html
    $body .= "--$frontier\\r\
";
    $body .= "Content-Type: text/html; charset=\\"utf-8\\"\\r\
";
    $body .= "Content-Transfer-Encoding: 8bit\\r\
";
    $body .= $html_body . "\\r\
";

    $body .= "--$frontier--";

    return mail($to, $subject, $body, $headers);

Thank you!

Not sure how acceptable divs are in html emails.

Why don’t you start with the simplest thing you can do, and then gradually work your way up to where you want to be.


$to = <your email address here> ;
$subject = "I'm only testing you ....";
$body="<html><body>Boo!</body></html>";

mail($to, $subject, $body);

If the problem is not there, then it is somewhere else …

Yes this code works, but I do not understand why my code does not work. I find no error in me but the text does not appear in moin email client

You have 2 choices. Either do what I suggested, gradually add more complexity from that simple example which does work - checking as you go (incremental development) or comment out each line one a time till you find the error (a basic debugging tactic).

These are some of the simple practices you have to adopt if you want to make personal progress.

Ok, I did, and I noticed that this problem is caused because there was a line before the promière <html>
I replace this piece code


   $html_body = <<<EOF
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div style="text-align:center">$text</div>
      </body>
    </html>
EOF;

by


   $html_body = <<<EOF
<html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div style="text-align:center">$text</div>
      </body>
    </html>
EOF;

It works now

Correct me if I am wrong, but I believe $body should be part of $headers. From what I recall from doing something similar, I had to have ‘’ for $body, like so:


<?php
   $frontier = "-----=" . md5(mt_rand());
   $to = "me@mail.com";
   $subject = "My Subject";
   $text = "Hello World";
    
   $html_body = <<<EOF
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <div style=\\"text-align:center\\">$text</div>
      </body>
    </html>
EOF;

    $headers = "From: <{$_SERVER['SERVER_ADMIN']}>\\r\
";
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\\r\
";
    $headers .= "MIME-Version: 1.0\\r\
";
    $headers .= "Content-Type: multipart/alternative; boundary=\\"$frontier\\"\\r\
";
    
    // text
    $headers .= "--$frontier\\r\
";
    $headers .= "Content-Type: text/plain; charset=\\"utf-8\\"\\r\
";
    $headers .= "Content-Transfer-Encoding: 8bit\\r\
";
    $headers .= $text . "\\r\
";
    
    // html
    $headers .= "--$frontier\\r\
";
    $headers .= "Content-Type: text/html; charset=\\"utf-8\\"\\r\
";
    $headers .= "Content-Transfer-Encoding: 8bit\\r\
";
    $headers .= $html_body . "\\r\
";

    $headers .= "--$frontier--";

    return mail($to, $subject, '', $headers);
?>

Sorry, I edited my post because it was incorrect.
Now see my new post with the solution.
Thank you for your help.