Add HTML formatting in phpmailer

php, phpmailer

Solution

By default, the PHP `mail()` function is `text/plain`.

In your `mail()` headers, change the `content-type` to `text/html` and try.

Example:

<?php 
    $body = "<html>\n"; 
    $body .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n"; 
    $body = $message; 
    $body .= "</body>\n"; 
    $body .= "</html>\n"; 

    $headers  = "From: My site<noreply@example.com>\r\n"; 
    $headers .= "Reply-To: info@example.com\r\n"; 
    $headers .= "Return-Path: info@example.com\r\n"; 
    $headers .= "X-Mailer: Drupal\n"; 
    $headers .= 'MIME-Version: 1.0' . "\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

    return mail($recipient, $subject, $message, $headers); 
?>

PHP Mailer:

You need to set `IsHTML(true)`:

$mail->IsHTML(true);

Problem

I am using PHP mailer to send an online form directly to email. I want to edit the form like this: ``` $message = '<p>The following request was sent from: '</p>; $message .= '<p>Name: '.$name.'</p><br />'; $message .= '<p>Phone: '.$phone.'</p><br />'; $message .= '<p>Email: '.$email.'</p><br />'; ``` However, in the email I receive back, I get the `<p>`, `<br />`, etc. Not the formatting I want, but the actual HTML. It has always worked for me, then I realized I was using the stock PHP mail function. not sure if PHP mailer has different parameters OR if I just missing small here? Where would I set the headers for text/html? ``` $mail = new PHPMailer(); $mail -> IsSMTP(); $mail -> Host = "---"; $mail -> Port = 25; $mail -> SMTPAuth = false; $mail -> Username = EMAIL_USER; $mail -> Password = EMAIL_PASS; $mail -> FromName = $from_name; $mail -> From = $from; $mail -> Subject = $subject; $mail -> Body = $message; $mail -> AddAddress("---"); $result = $mail -> Send(); ```

Original source