Josh Curren
Josh Curren

Reputation: 10226

mail() header problem for html email

I am trying to write a error reporting feature for a website in php. I cannot get the headers right so that the email will display as html.

Here is the code:

if( isset($_POST['submit']) )
{
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $page = $_POST['page'];
    $email = $_POST['email'];
    $error = $_POST['error'];

    $message  =  "<html><body> \n";   
    $message .=  "Email: $email \n";
    $message .=  "Page: $page \n";
    $message .=  "OS/ Browser: $browser \n";
    $message .=  "Error: $error \n";
    $message .=  "</body></html> \n";

    $headers  = 'MIME-Version: 1.0' . '\r\n';
    $headers .= 'Content-type: text/html; charset="iso-8859-1"' . '\r\n';
    $headers .= 'From:  <[email protected]>' . '\r\n';
    $headers .= 'Reply-To: $email ' . '\r\n';
    $headers .= 'X-Priority: 1' . '\r\n';

    $subject  = "[ERROR REPORT] Page: " . $page;

    mail("[email protected]", $subject, $message, $headers );

    $mesg = "Thank you for your report!";

}

?>

Upvotes: 1

Views: 7143

Answers (3)

Cookie Monster
Cookie Monster

Reputation: 21

One problem I noticed is that you need to use double quotes instead of single quotes in the proper places.

Use: "\r\n"

Instead of: '\r\n'

Upvotes: 2

drowe
drowe

Reputation: 2320

Personally, I'm a fan of Pear Mail (http://pear.php.net/package/Mail) and Pear Mail_Mime (http://pear.php.net/package/Mail_Mime).

Sending an HTML e-mail (with a plain-text body, for clients that don't support HTML) is as simple as this:

include_once('Mail.php');
include_once('Mail/Mime.php');

$htmlBody = '<html><body><b>Hello World</b></body></html>';
$plainBody = 'Your client doesn\'t support HTML';
$em = Mail::factory('sendmail');
$headers = array('From'=>'[email protected]', 'To'=>'[email protected]', 'Subject'=>'Cool Email');
$mime = new Mail_Mime();
$mime->setTxtBody($plainBody);
$mime->setHtmlBody($htmlBody);
$message = $mime->get();
$headers = $mime->headers($headers);
$mail = $em->send('[email protected]', $headers, $message);

Upvotes: 2

da5id
da5id

Reputation: 9136

Basically repeating what I answered for another question, I'm all for rolling-your-own in most situations, but when it comes to mail I'd heartily recommend making it easier on yourself and using something like Swift Mailer or PHPMailer (in that order, for my money).

As a side-bonus (and assuming you specify reply-to, etc), you also have much less chance of being tagged as spam.

EDIT: Maybe it's just the example you've used, but there's no actual HTML in your message. Why not just use plain text? And yes, I'd use one of the classes I suggest for plain text too.

Upvotes: 1

Related Questions