Use HTML to write a simple email template

Use HTML to write a simple email template

Today, I want to write about a "low-tech" problem.

By the way, I subscribe to quite a few newsletters, such as JavaScript Weekly. Get a weekly email with the week's big news.
2015712153636746.jpg (349×460)

One day, I was thinking, could I also make an email like this?

Then, I realized it wasn’t that easy. Putting aside the backend and editing work, just designing an email template requires a lot of thought.
2015712153840405.jpg (550×671)

Because this formatted email is actually a web page, its official name is HTML Email. Whether it can be displayed properly depends entirely on the email client. Most email clients (such as Outlook and Gmail) will filter HTML settings, making emails unrecognizable.

I found that the trick to writing HTML emails is to use the same methods used to create web pages 15 years ago. Here is the writing guide I put together.

1. Doctype

Currently, the most compatible Doctype is XHTML 1.0 Strict. In fact, Gmail and Hotmail will delete your Doctype and replace it with this Doctype.

XML/HTML CodeCopy content to clipboard
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >   
  2.   
  3. < html   xmlns = "http://www.w3.org/1999/xhtml" >   
  4.   
  5.  < head >   
  6.   
  7.   < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8"   />   
  8.   
  9.   < title > HTML Email Writing Guide </ title >   
  10.   
  11.   < meta   name = "viewport"   content = "width=device-width, initial-scale=1.0" />   
  12.   
  13.  </ head >   
  14.   
  15. </ html >   

Using this Doctype means that HTML5 syntax cannot be used.

2. Layout

The layout of a web page must use a table. First, place a large outer table to set the background.

XML/HTML CodeCopy content to clipboard
  1. < body   style = "margin: 0; padding: 0;" >   
  2.   
  3.  < table   border = "1"   cellpadding = "0"   cellspacing = "0"   width = "100%" >   
  4.   
  5.   < tr >   
  6.    < td > Hello! </ td >   
  7.   </ tr >   
  8.   
  9.  </ table >   
  10.   
  11. </ body >   

The border property of the table is equal to 1, which is for the convenience of development. When officially released, set this property to 0.

On the inner layer, place the second table. Used to display content. The width of the second table is set to 600 pixels to prevent it from exceeding the client's display width.

XML/HTML CodeCopy content to clipboard
  1. < table   align = "center"   border = "1"   cellpadding = "0"   cellspacing = "0"   width = "600"   style = "border-collapse: collapse;" >   
  2.   
  3.  < tr >   
  4.   < td > Row 1 </ td >   
  5.  </ tr >   
  6.   
  7.  < tr >   
  8.   < td > Row 2 </ td >   
  9.  </ tr >   
  10.   
  11.  < tr >   
  12.   < td > Row 3 </ td >   
  13.  </ tr >   
  14.   
  15. </ table >   

Set as many rows as the email content has.

3. Images

Images are the only external resources that may be cited. Other external resources, such as style sheet files, font files, video files, etc., cannot be referenced at all.

Some clients will add borders to image links, so you need to remove them.

CSS CodeCopy content to clipboard
  1. img { outline : none ; text-decoration : none ; -ms-interpolation-mode: bicubic;}
  2.   
  3. a img { border : none ;}
  4.   
  5. <img border = "0" style = "display:block;" >

It should be noted that many clients do not display images by default (such as Gmail), so make sure that the main content can be read even without images.

4. Inline styles

All CSS rules are best used inline. Because the style placed in the header of the web page is likely to be deleted by the client. For client support for CSS rules, please see here.

Also, do not use CSS abbreviations, as some clients do not support them. For example, instead of writing:

XML/HTML CodeCopy content to clipboard
  1. style = "font: 8px/14px Arial, sans-serif;"   

If you want to express

XML/HTML CodeCopy content to clipboard
  1.  < p   style = "margin: 1em 0;" >   

To write it like this:

XML/HTML CodeCopy content to clipboard
  1. < p   style = "margin-top: 1em; margin-bottom: 1em; margin-left: 0; margin-right: 0;" >   

5. W3C Validation and Testing Tools

To ensure that the final code can pass the W3C check, because some clients will strip unqualified attributes. Also use the test tools (1, 2, 3) to see the display results on different clients.

When sending HTML Email, don't forget that the MIME type cannot be used

XML/HTML CodeCopy content to clipboard
  1. Content-Type: text/plain;

Instead, use

XML/HTML CodeCopy content to clipboard
  1. Content-Type: Multipart/Alternative;

For sending tools, consider using MailChimp and Campaign Monitor.

6. Templates

It’s a good idea to use templates that others have already made (here and here), and you can find more on the Internet.

If you want to develop your own, you can refer to HTML Email Boilerplate and Emailology.

<<:  TypeScript union types, intersection types and type guards

>>:  Detailed tutorial on installing Docker on CentOS 8.4

Recommend

Solution to the low writing efficiency of AIX mounted NFS

Services provided by NFS Mount: Enable the /usr/s...

Install Linux using VMware virtual machine (CentOS7 image)

1. VMware download and install Link: https://www....

HTML Tutorial: Collection of commonly used HTML tags (6)

These introduced HTML tags do not necessarily ful...

Docker completely deletes private library images

First, let’s take a look at the general practices...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

Example of converting timestamp to Date in MySQL

Preface I encountered a situation at work: In the...

Docker exposes port 2375, causing server attacks and solutions

I believe that students who have learned about th...

How to use Dayjs to calculate common dates in Vue

When using vue to develop projects, the front end...

...

MySQL 5.7.20 installation and configuration method graphic tutorial (win10)

This article shares the installation and configur...

How to implement import and export mysql database commands under linux

1. Export the database using the mysqldump comman...