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

MySQL case when group by example

A mysql-like php switch case statement. select xx...

Linux implements automatic and scheduled backup of MySQL database every day

Overview Backup is the basis of disaster recovery...

MySQL 5.6 installation steps with pictures and text

MySQL is an open source small relational database...

Analysis and solution of abnormal problem of loading jar in tomcat

Description of the phenomenon: The project uses s...

Get a list of your top 10 most frequently used terminal commands in Linux

I think the commands I use most often are: Choice...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...

14 Ways to Create Website Content That Engages Your Visitors

When I surf the Net, I often see web sites filled...

How to run commands on a remote Linux system via SSH

Sometimes we may need to run some commands on a r...

In-depth study of JavaScript array deduplication problem

Table of contents Preface 👀 Start researching 🐱‍🏍...

Detailed explanation of the basic usage of SSH's ssh-keygen command

SSH public key authentication is one of the SSH a...

Vue.js implements the code of clicking the icon to zoom in and leaving

The previous article introduced how Vue can reali...

Example of Vue uploading files using formData format type

In Vue, we generally have front-end and back-end ...