Phpmailer - CSS does not appear

Asked

Viewed 1,625 times

5

Hi, I’m trying to set up phpmailer to send a html message with css, but it’s not recognizing css.

What is wrong?

$mail->Body = '
        <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>

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

';
  • You have to put inside the tags <head></head>.

  • @Diegosouza Even so, it doesn’t work.

  • Now the email is showing all the html

  • How’s your email header ? text/html ?

  • php or html header?

  • To php this way header ('Content-type: text/html; charset=UTF-8'); html is as in the above code

  • And by chance your Webmail or e-mail program is configured to read emails format HTML, Rich Text or Text ?

  • I’m testing localhost for gmail.

  • In Phpmailer you can configure the e-mail header. Put this too: $mail->CharSet = "text/html; charset=UTF-8;".

  • I couldn’t do it either....

  • You set up the right email ? You have to have Body. <html><head><style type="text/css">&#xA; .corpo {margin: 15px;padding: 15px;background-color: #FFF;}&#xA; .body {background-color: #E1E1E1;font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;font-size: 16px;}&#xA; </style></head><body> <div class="body">&#xA; <div class="corpo">&#xA; <p><img src="http://...../images/logoEmpresa.png" width="90" height="50"></p>&#xA; <p>O conteúdo da nova tag div é inserido aqui</p>&#xA; </div>&#xA; </div></body></html>

  • I’m not sure about...

  • Cara u enabled the right HTML?

  • Like, does phpmailer work? How is HTML when you receive the email?

  • html works like <H1>, but CSS that doesn’t work.

Show 10 more comments

2 answers

3

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 =().

1


Just like @Fernando said

I will leave my solution in collaboration, in case others have the same doubt:

<body style="margin:0px;">
<table width="100%" border="0" cellspacing="0" cellpadding="50" style="background-color:#BCBCBC; margin:0px;"><tbody><tr><td height="331" valign="top">

    <table width="90%" border="0" align="center" cellpadding="50" style="background-color:#FFFFFF;"><tbody><tr><td>
          Aqui vai seu texto
    </td></tr></tbody></table>

</td></tr></tbody></table>

Final result:

inserir a descrição da imagem aqui

Now just work your message according to your need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.