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.
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.
Hmmm.
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 <br>\r\n<br>\r\n<br>\r\n
I did another test to my Gmail account and this seems to be working quite well.
Yeah!
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…
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.