0

How to send email include html code using php ?

I tried to use this code.

<?PHP
    include("connect.php");
    $email = "test_mail@hotmail.com";    
    $to = $email;
    $subject = "test subject";
    $message = "

    <body style='margin: 0; padding: 0;'>
        <table border='1' cellpadding='0' cellspacing='0' width='100%'>
            <tr>
                <td>
                    <img src='https://i.stack.imgur.com/Jy9QUm.jpg'/>
                </td>
            </tr>
            <tr>
                <td>
                    test text
                </td>
            </tr>
        </table>
    </body>

    ";

    $headers  = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: EXAMPLE <noreply@example.com>' . "\r\n";
    $headers .= 'Return-Path: return@example.com' . "\r\n";
    mail($to, $subject, $message, $headers, '-freturn@example.com');
    ?>

When i open my email it's will show like this in my email.

<body style='margin: 0; padding: 0;'>
    <table border='1' cellpadding='0' cellspacing='0' width='100%'>
        <tr>
            <td>
                <img src='https://i.stack.imgur.com/Jy9QUm.jpg'/>
            </td>
        </tr>
        <tr>
            <td>
                test text
            </td>
        </tr>
    </table>
</body>

But i want to show like this

enter image description here

How can i do ?

........................................................................................................................................................

1 Answers1

0

Reason being is, that you don't have <!doctype html> and </html> tags (and other tags) and your HTML isn't being rendered as proper/full HTML markup.

$message = "
<!doctype html>
<html>
<head>
<title></title>
</head>
  <body style='margin: 0; padding: 0;'>
    <table border='1' cellpadding='0' cellspacing='0' width='100%'>
        <tr>
            <td>
                <img src='https://i.stack.imgur.com/Jy9QUm.jpg'/>
            </td>
        </tr>
        <tr>
            <td>
                test text
            </td>
        </tr>
    </table>
  </body>
</html>
";
  • Which upon testing afterwards, was successful.

Foonotes: (edit)

Even though I placed an answer, I decided to close the question being an exact duplicate of their other question How to send mail using php by insert html into mail content? where they were also nonrespondant and the answer given in there, answered what they posted for code.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141