Chris H Chris H - 2 years ago 68
HTML Question

Sending a email message having HTML tags via PHPMailer using PHP heredoc feature

When a user fills out a contact form, I want the user to get an HTML message with a image at the start of message and some styling for a box with shadow in which the user's name and phone
number are shown.

I tried using $mail->IsHTML(true) and $mail->msgHTML($message) using PHPmailer but the message was not sent.

The message is below:

$message = <<<STARTMESSAGE

<script src=''></script>
<link href='' rel='stylesheet'>

<style type='text/css'>

.box {
margin: 40px;

-moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
-webkit-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
box-shadow: 5px 5px 5px rgba(68,68,68,0.6);

display: block;

.content {

.h3 {

<img src='./img/logo.png' class='img-responsive center-block' alt='My website'>

<h3> Contact Form Submission </h3>

<div class='box'>
<div class='content'>

The following information was submitted: <br/> <br/>

<strong>Full Name:</strong> $firstname $lastname <br/> <br/>
<strong>Phone number:</strong> $phone <br/> <br/>


$mail = new PHPMailer;
//Set who the message is to be sent from
$mail->setFrom($from , "Contact Form");
$mail->addAddress('', "John Doe");

$mail->Subject = "Contact Form submission ".$name;

$mail->Body = $message;


$mailsent = $mail->send();

if (!$mailsent)
echo "Mailer Error: " . $mail->ErrorInfo;
error_log("Mail not sent for $firstname $lastname $mail->ErrorInfo \n", 1, "");
error_log("Message text is $message \n", 1, "");
error_log("Value of variables from POST method $post_variables \n ", 1, "");
error_log("\n Value of variables from POST method $post_variables \n", 3, "logfile.txt");
$logfile = fopen("errorlog.log","w+") or die("cannot open log file");
fwrite($logfile,"Mail not sent for $firstname $lastname");
$backtrace = debug_print_backtrace();
fwrite($logfile,"Back trace is \n");

echo "Message sent!";
error_log("\n Mail sent for $firstname $lastname at $date \n", 3, "logfile.txt");
error_log("\n Value of variables from POST method $post_variables \n", 3, "logfile.txt");

Answer Source

As I stated in comments:

Most mail clients will reject/ignore stylesheets / <style>, and JS linked files.

It's best not to use any JS and use an inline CSS method for styling.

An example of this was left in comments by gibberish

<h1 style="color:red;font-size:28px;">M E M O</h1>

As you also noted in comments:

@Fred, Thanks for posting back. The sending mail server had a restriction on attachment size. This wasn't the case last week so I did not know about it. Someone changed it and forgot to tell the folks who use it. That was causing the emails to be not sent. Again, thanks for all your time and posts. – Chris H


@Fred, Also, using the img src as a full http call did not work for me. I had to use: $mail->AddEmbeddedImage("img/logo.png", "logo", "img/logo.png"); $mail->Body = 'My Logo: – Chris H

This for future readers to the question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download