0

I have to written the following code to send a mail using PHP in my website, which is hosted on a Linux server. The mail is sent, but the message is sent as raw HTML instead of markup text. Here's the code:

<?php
sendMail('[email protected]', 'Your recovered password', 'Your password is <b>ABC123</b>', '[email protected]');

function sendMail($from='[email protected]', $subject, $message, $to = '[email protected]'){
    $message = '<html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>'.$subject.'</title>
        </head>
        <body>'.$message.'</body>
        </html>';
    $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); 
    $message = wordwrap($message, 70);  

    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/plain; charset=UTF-8";
    $headers[] = "From: $from";
    $headers[] = "Reply-To: $from";
    $headers[] = "Subject: {$subject}";

    $additional_params = "-f $from -r $from";

    if(mail($to, $subject, $message, implode("\r\n", $headers)))
        echo 'Mail Sent';
    else
        echo 'Mail NOT Sent';
    exit();
}

?>

How do I correct the code to send the HTML mail correctly?

Thanks in advance

3
  • 1
    My first gut reaction is to change Content-type from text/plain to text/html. Commented Jul 1, 2012 at 6:09
  • 2
    The content type is text/plain? Commented Jul 1, 2012 at 6:10
  • @KingSkippus: Thanks King. The problem was I had used Content-type in header declaration as text/plain. Changing to text/html worked. Commented Jul 1, 2012 at 6:20

1 Answer 1

3

Change this line

$headers[] = "Content-type: text/plain; charset=UTF-8";

to

$headers[] = "Content-type: text/html; charset=UTF-8";

This way you tell your mail program that it's HTML and not a plain text.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.