PHP mail script not sending in HTML format

Posted on 2012-08-14
Last Modified: 2012-08-14
I have a PHP script that receives some incoming POST data to build a message. The message is sending, but not in HTML format. I receive the HTML code as text in the message, rather than an HTML-encoded mail message. I got most of this code from, is there something I'm not doing quite right?


	$msg='<html><head><title>Liever, Hyman & Potter</head></html><body>' . $_POST['msg'] . '</body></html>';
	$headers='MIME-Version: 1.0' . '\r\n' . 'Content-type: text/html; charset=iso-8859-1' . '\r\n' . 'From:' . $_POST['sender'];

	mail($recipient, $subject, $msg, $sender);
	echo 'Mail Sent.';


Open in new window

Thank you!
Question by:bbdesign
    LVL 30

    Accepted Solution

    I strongly suggest you to use PHPMailer ( it solves all problems easily

      $mailer = new PHPMailer();
      $mailer->IsSMTP(); // set mailer to use SMTP
      $mailer->Host = "";
      $mailer->SMTPAuth = true;
      $mailer->Username = 'username';
      $mailer->Password = 'password';

      $mailer->SetFrom('', 'Me');
      $mailer->AddAddress('', 'You');
      $mailer->Subject = $Subject;
      if (!$mailer->Send()) {
        echo "Some problem occurred!";
        echo "Email sent!";

    LVL 14

    Assisted Solution

    by:Scott Madeira
    In your particular example you need to be careful when you use single quotes and double quotes.

    $headers='MIME-Version: 1.0' . '\r\n' . 'Content-type: text/html; charset=iso-8859-1' . '\r\n' . 'From:' . $_POST['sender'];

    The single quotes around '\r\n' will add a literal \r\n to your $headers variable.  You need to use double quotes like this:  "\r\n" to have those translated into carriage return and line feed.

    but, like MarqusG said, use a PHP mail class that already takes care of all of the headaches - you just need to call it properly.

    Author Comment

    Thanks for your help. Yes, the quotes were part of the problem. I also put $sender in the mail command when I should have put $header. AND I was missing the closing </title> tag. Sorry, that was really sloppy... I normally program in ASP so PHP isn't my thing. Thank you!
    LVL 82

    Expert Comment

    by:Dave Baldwin
    First, you not sending the 'headers'.  On , it shows the proper format to be:  mail($to, $subject, $message, $headers);

    Second, your message format is not correct or complete for 'proper' HTML Multipart email.

    @marqusG's suggestion to use PHPMailer is a good idea though the current page for it is

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now