Austin Lovell
Austin Lovell

Reputation: 1049

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!

Upvotes: 0

Views: 98

Answers (2)

EdiSainer
EdiSainer

Reputation: 112

Try use this PHP code.

<?php
    $body_mail = "<h1>Test Rendering HTML Email</h1>\r\n";
    $headers = "From: [email protected]\r\n";
    $headers .= "Reply-to: [email protected]\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $mail_sent = @mail("[email protected]", "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";

Upvotes: 0

Rottingham
Rottingham

Reputation: 2604

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);

Upvotes: 1

Related Questions