简体   繁体   中英

Send email with HTML

I am able to send emails from my server just fine. My problem is that my emails are not rendering as HTML. When I recive the email this is what I get:

<table style='text-align: center;'>
            <td><b>Park</b></td>
            <td style='padding: 0px 20px 0px 20px;'><b>Ratio</b></td>
            <td style='padding: 0px 20px 0px 20px;'><b>% Complete</b></td>
            <tr style='background-color: rgb(207, 226, 243)'>
                    <td>Arbordale Acres</td>
                    <td style='padding: 0px 20px 0px 20px;'>353 / 928</td>
                    <td style='padding: 0px 20px 0px 20px;'>38.04%</td>
            </tr>

            <tr style='background-color: rgb(208, 242, 208)'>
                    <td>Brentwood</td>
                    <td style='padding: 0px 20px 0px 20px;'>106 / 1036</td>
                    <td style='padding: 0px 20px 0px 20px;'>10.23%</td>
            </tr>

            <tr style='background-color: rgb(207, 226, 243)'>
                    <td>Carefree</td>
                    <td style='padding: 0px 20px 0px 20px;'>100 / 619</td>
                    <td style='padding: 0px 20px 0px 20px;'>16.16%</td>
            </tr>

There is more html that comes through, but you get the basic idea. I understand that some css does not work but I image that there would be at least some type of attempt to render the HTML in gmail rather then receive plain text.

Thank you in advance!

You need to set the MIME type in your headers before sending the email to be of content type HTML. This tells the email client to interpret the body of the email as HTML.

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

mail($to, $subject, $message, $headers);

Try use this PHP code.

<?php
    $body_mail = "<h1>Test Rendering HTML Email</h1>\r\n";
    $headers = "From: mail@yourdomain.com\r\n";
    $headers .= "Reply-to: me@yahoo.com.sg\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $mail_sent = @mail("you@mail.com", "Title TEST", $body_mail, $headers);
 echo $mail_sent ? "Success" : " Failed!";
?>

It will render your HTML email. Notice the:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM