Sending HTML emails using PHP

Hello. I am trying to figure out how to send HTML emails using phpMailer.

Here is a snippet of the function I wrote to use phpMailer to send emails…

$mail->From = $from;
$mail->FromName = $from;
$mail->AddReplyTo($from, $from);
$mail->SetFrom($from, $from);
$mail->Subject = $subject;
$mail->Body = $body;


$results = $mail->send( );

This week I wrote a PHP page that sends an email to my AT&T account and the email is purely text with even the hyperlink stripped out.

I know that ever since the data breach, AT&T is very hard on any emails, but I don’t think that is the problem.

One thing I noticed in the code above which I got from the creator of phpMailer is that unlike the built-in PHP mail( ) function, there is no place for email headers or markup.

The HTML emails I want to be able to send to users are not fancy, but I do need basic things like: working hyperlinks, paragraphs and line breaks that work, and the ability to do basic HTML/CSS like creating shaded boxes with colored font and so on.

I am not sure how to get this working.



Are you sure? Looks like markup to me.

$mail->Body = "<i>Mail body in HTML</i>";

If by ”headers" you mean <head>, you don’t want that anyway, put the CSS (ugh, I know, but…) inline. eg.
<td style="font-weight: bold">

When a user creates a new account, I send an email with an activation - much like SitePoint did when I registered.

My script has this code…

if (record was inserted into database){

$body = "<p>Dear " . htmentities($firstName) . ",</p>\r\n";
$body .= "<p>Thank you for creating a new account.  To finishing registering, please click on the link below:</p>";
$body .= "<p style='padding: 0 0 2em 2em;'>" . MAIN_URL . '/activate.php?z=' . $activationCode . "</p>\r\n";
$body .= "<p>Sincerely,<br /><br /><br />\r\n\r\n\r\n\";
$body .= "Support</p>";

That is basic HTML and inline CSS and it should provide a working hyperlink and some carriage returns, yet when I get that email in my inbox I get something that looks like this…

As you can see, the little markup and formatting that I had was stripped out in the final email.

Hopefully that does a better job of what I am doing and what is happening.



One thing is that the “link below” isn’t a link. The URL is in <p> not <a>
As for the padding and the <br>s not getting picked up, I don’t know why not.
It might be the em unit isn’t recognized, so maybe px would work better.
Last I knew, some email clients see multiple \r\ns as indication of a content boundary. Try like

Oops, I totally forgot to add an < a href= > in my code!

Even so, why are the < p > getting ignored or stripped out?

How do I add carriage returns into my e-mail?




I just tried something that may have solved my problem…

$body = "<div style=' my styles go here '>Instructions</div>";
$body .= "<p style='padding: 0 0 1em 0;'>Dear " . htmlentities($firstName) . ",</p>\r\n";
$body .= "<p style='padding: 0 0 1em 0;'>Thank you for creating...</p>\r\n";
$body .= and so on....

The results in my AT&T email now look exactly as I wanted. I am not sure why I had to add embedded styles to the < p > tags.



1 Like

I did another test to my Gmail account and this seems to be working quite well.

Yeah! :yum:

Now what I am trying to figure out is what purpose the /r/n at the end of these lines is supposed to do.

$body .= "<p style='padding: 0 0 1em 0;'>Thank you for creating...</p>\r\n";

Long ago I read something on the Internet that suggested adding those characters at the end of each line in your email, but I honestly don’t remember what their purpose is.

Could someone help me understand what those are about?

Also, my code is working for now with AT&T and Gmail, but is there anything else I should add to make things more foolproof?

I thought that a properly formatted email was supposed to have headers in it like this…

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= 'From: <>' . "\r\n";
$headers .= 'Cc:' . "\r\n";

I don’t want my email to a user to be treated as spam or get rejected for another reason, and I want my emails to work equally well for Mac and PC users, in all major browsers, and on mobile as well.



HTML has rendered view and source view.
\r\n is for newlines in the source. But they do not result in newlines in the rendered view. For newlines in the rendered view HTML needs <br> or block level tags.

If you don’t care if the source view has a-whole-lot-of-stuff-all-on-one-line you can omit them (the \r\ns) from the body (message) content except for at the end, where they are needed for email clients to know that the body has ended.

Does that mean I need to make sure there is a \r\n at the end of my email body like this…

$body .= "<p style='padding: 0 0 3em 0;'>Sincerely,</p>";
$body .= "<p style='padding: 0 0 1em 0;'>Customer Service</p>\r\n";

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.