Well your problem has no relation any problem with PHP or with Phpmailer, but rather that, style CSS
does not work for all email providers.
In your case the destination is gmail and this removes all <head>
, <style>
of your email.
So the only solution to create CSS-styled email that works with the vast majority of email providers is to go to CSS inline, there are even some tools for this (converts CSS style into inline CSS), such as CSS Inliner Tool, Inliner, Inline Styler.
This source quotes beyond the CSS inline, other techniques for your email to be properly interpreted by most providers. How:
- Keep your code simple: Coding for HTML e-mail is different from coding for website. The simpler the code, the less room for errors.
- Use CSS only for general style elements: You’ll get the best results if you use CSS for general elements such as fonts or colors.
- Use inline CSS (in-line): Browser-based email applications, like Gmail, take tags
<head>
and <body>
by default.
Another tip, which is not in the source cited, create the layout of your email within tables (<table>
), do not save tables, create tables within table cells, forget Tableless, at the time of creating emails. Some providers (Outlook Desktop and Outlook Webmail) love not respecting the styles of their beautiful layout with Divs. (I recently had to refactor a complete layout for tables =(
).
You have to put inside the tags
<head></head>
.– Diego Souza
@Diegosouza Even so, it doesn’t work.
– Tiago
Now the email is showing all the html
– Tiago
How’s your email header ?
text/html
?– Diego Souza
php or html header?
– Tiago
To php this way
header ('Content-type: text/html; charset=UTF-8');
html is as in the above code– Tiago
And by chance your Webmail or e-mail program is configured to read emails format HTML, Rich Text or Text ?
– Diego Souza
I’m testing localhost for gmail.
– Tiago
In Phpmailer you can configure the e-mail header. Put this too:
$mail->CharSet = "text/html; charset=UTF-8;"
.– Diego Souza
I couldn’t do it either....
– Tiago
You set up the right email ? You have to have Body.
<html><head><style type="text/css">
 .corpo {margin: 15px;padding: 15px;background-color: #FFF;}
 .body {background-color: #E1E1E1;font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;font-size: 16px;}
 </style></head><body> <div class="body">
 <div class="corpo">
 <p><img src="http://...../images/logoEmpresa.png" width="90" height="50"></p>
 <p>O conteúdo da nova tag div é inserido aqui</p>
 </div>
 </div></body></html>
– Diego Souza
I’m not sure about...
– Tiago
Cara u enabled the right HTML?
– Hiago Souza
Like, does phpmailer work? How is HTML when you receive the email?
– Hiago Souza
html works like
<H1>
, but CSS that doesn’t work.– Tiago