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

}

?>

      

+1


a source to share


3 answers


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.

+1


a source


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);

      

+2


a source


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

Using: "\r\n"

Instead '\r\n'

+2


a source







All Articles