Mail () for html email
I am trying to write an error reporting function for a website in php. I cannot get the headers correctly so that the message is displayed 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: <code@website.com>' . '\r\n';
$headers .= 'Reply-To: $email ' . '\r\n';
$headers .= 'X-Priority: 1' . '\r\n';
$subject = "[ERROR REPORT] Page: " . $page;
mail("myEmail@gmail.com", $subject, $message, $headers );
$mesg = "Thank you for your report!";
}
?>
a source to share
Basically repeating what I answered for another question , I am mostly preparing for your own work, but when it comes to mail, I would heartily recommend simplifying yourself and using something like Swift Mailer or PHPMailer (in this ok for my money).
As a bonus bonus (and if you post an answer, etc.), you will also be much less likely to be flagged as spam.
EDIT: Maybe this is just the example you used, but there is no actual HTML in your post. Why not just use plain text? And yes, I would use one of the classes I suggest for plain text.
a source to share
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 HTML email (with plain body text, for clients that don't support HTML) is as easy 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'=>'me@domain.com', 'To'=>'joe@schmoe.com', 'Subject'=>'Cool Email');
$mime = new Mail_Mime();
$mime->setTxtBody($plainBody);
$mime->setHtmlBody($htmlBody);
$message = $mime->get();
$headers = $mime->headers($headers);
$mail = $em->send('joe@schmoe.com', $headers, $message);
a source to share