Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include external stylesheets internally for email templates

I'm trying to craft some nice email templates for my site, but I'm having a conflict between my desire for well-crafted code and functionality.

My problem is that all of my email templates are formatted like standard templates:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet"...>
    </head>
    <body>
        Some stuff
    </body>
</html>

which displays just fine as a webpage, but in trying to send this as a formatted email, it's essentially just sending the Some stuff section, which means none of the formatting really makes it across.

My current emailing code looks like this:

message = Message(
    subject="Subject",
    html= render_template(
        'emails/confirmation_email.html',
        confirmation_code=confirmation.confirmation_code
    ),
    sender = ("sender", "[email protected]")
)

I'm using Jinja2 templates and the Flask-Mail extension.

Basically, I really want to include these stylesheets in my email, but I really object to including everything in a style tag.

like image 309
Slater Victoroff Avatar asked Dec 16 '13 07:12

Slater Victoroff


People also ask

Can I use internal CSS in email template?

Here, the best thing that can be used is internal CSS as it provides much ease when it comes to code editing. But, if the email template is for one time campaign than inline CSS would surely be an ideal choice.

Can email templates use external CSS?

First thing is you cannot link external CSS from a CDN or anyplace. you have to write you CSS rules inline html. And you cannot use divisions(divs)in you email template html code. You have to make the template's structure with html tables.

Can I use style tag in email template?

Yes you can. However you have to keep in mind that few email clients respect css standards. Just stick to basic css properties like margin and padding , etc., and it should all be fine. Also you can style your html elements inline ( <div style=""> ) though it's not an elegant solution.

Can I use Flexbox in email template?

As for email applications, Flexbox works well in Apple Mail (on OS X or iOS), Android default email application (on Android 4.4 and below), Outlook app (on iOS and Android), and Thunderbird 31 (and above).


1 Answers

You need to inline your CSS in HTML email. This is because some email clients (Gmail for example) strip your <style> tags. Here is the list of support (click the pdf link for the full chart).

There are several CSS inlining tools to make your job easier If you prefer to work traditionally within the <style> tag. You should never place your CSS in an external file however.

like image 186
John Avatar answered Nov 14 '22 23:11

John