1

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!";

}

?>

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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'

Comments

1

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.

2 Comments

The example I used doesnt use html in it but in reality I do have html in there.
Suspected as much, but was just making the point - not so much for you even, more 'cos increasingly people seem to default to html email when often there's no need.

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.