Chris H Chris H - 6 months ago 21
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='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
<link href='http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css' 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 {
position:relative;
padding:100px;
background-color:FFF;
}

.h3 {
text-align:center;
color:#008800;
}
</style>



<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/>
</div>
</div>
STARTMESSAGE;


Edit:

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

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


$mail->IsHTML(true);
$mail->Body = $message;

//$mail->msgHTML($message);

$mailsent = $mail->send();

if (!$mailsent)
{
echo "Mailer Error: " . $mail->ErrorInfo;
error_log("Mail not sent for $firstname $lastname $mail->ErrorInfo \n", 1, "myemail@example.org");
error_log("Message text is $message \n", 1, "myemail@example.org");
error_log("Value of variables from POST method $post_variables \n ", 1, "myemail@example.org");
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");
fwrite($logfile,$backtrace);

}
else
{
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");
header('Location: http://www.example.org/index.php?success=yes');
}

Answer

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

and

@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.

Comments