Summary of principles for writing HTML pages for emails

Summary of principles for writing HTML pages for emails
Since HTML email is not an independent HOST page on this site, it is hosted by someone else. So writing HTML emails is very different from writing HTML pages. This is because all mainstream email services for Internet users will more or less filter the HTML emails they receive in the background. There is no doubt that JS code is strictly filtered out, including all event monitoring attributes such as onclick and onmouseover. This is based on email security considerations. Not only that, CSS code will also be partially filtered. What I want to talk about is how to write HTML emails that are not filtered by major mainstream mailboxes and can be displayed normally.

First, let's take a look at how the mailbox displays HTML emails. I have never worked on an email system, and the filtering algorithms behind the major email systems are not so easy for outsiders to know. Therefore, we can only infer which writing methods are accepted by the mailbox and which ones will be filtered out through the front-end display. Through the analysis of gmail, hotmail, 163, sohu, sina, I divide the mailboxes into two categories:

The first category includes gmail, hotmail, and sohu. For this type of mailbox, the email content is laid out in a div in the entire mailbox page. As shown in the figure:


The second category includes 163 and sina. In this type of mailbox, the email content is laid out in an independent iframe. As shown in the figure:

Friends who are familiar with HTML know that the iframe content is an independent document, which is unrelated to the elements and CSS of the parent page and can be treated almost as an independent page. If the email content is in a div, then the email content is an integral part of the entire mailbox page. Obviously, emails that use iframe as a display method will be much more tolerant of email content because it gives you a sufficiently independent display space. The div is not so polite. Imagine if you wrote this CSS in your email, would the font size on the entire email page become 20px and become a mess:
<style type="text/css">
body {font-size:20px}
</style>
If we need to write a unified email template that is compatible with all mailboxes, we must avoid the above external CSS writing method. In addition, styles such as float and position that form abnormal content flows will also be filtered. If you write them, it is likely to affect the performance of external mailboxes.


Here are some writing principles:
1. One of the global rules is, do not write <style> tags, do not write classes, all CSS use style attributes, and use style to write inline CSS for elements that require certain styles.

2. The second global rule is to use fewer pictures. The mailbox will not filter your img tags, but the system will often default to not loading pictures from unfamiliar emails. If an email uses a lot of pictures, it will be ugly and even the content cannot be seen clearly if the pictures are not loaded. Impatient users will delete it directly. Always add alt to images.

3. Do not write float, position and other styles in the style, because they will be filtered. So how to achieve left-right layout or more complex layout? Use table.

4. The background color can be set in the style content, but the img will be filtered, which means that the background image cannot be set through CSS. But there is a very interesting element attribute, also called background, which can define an image path. This is a good alternative. Although the function is limited, for example, the background image cannot be positioned, it is better than nothing. For example, to add a background to a cell, you must write:
<td background=”http://image1.koubei.com/images/common/logo_koubei.gif”></td>

5. The div mode mailbox does not support flash, and the iframe mode mailbox needs to be verified.

Finally, I would like to mention that Sohu's email is very strange. It will add a space after each text paragraph, causing the normal layout to not fit on one line and to wrap, thus making some layouts messed up. Therefore, if you want to be compatible with Sohu Mail, you must be extra careful when encountering some compact layouts, try to reduce the number of text segments and leave enough width.

<<:  JavaScript Dom Object Operations

>>:  Mobile development tutorial: Summary of pixel display issues

Recommend

Three examples of nodejs methods to obtain form data

Preface Nodejs is a server-side language. During ...

Explanation and example usage of 4 custom instructions in Vue

Four practical vue custom instructions 1. v-drag ...

MySQL master-slave replication principle and practice detailed explanation

Table of contents Introduction effect principle f...

Example code for evenly distributing elements using css3 flex layout

This article mainly introduces how to evenly dist...

An article to give you a deep understanding of Mysql triggers

Table of contents 1. When inserting or modifying ...

MySQL latest version 8.0.17 decompression version installation tutorial

Personally, I think the decompressed version is e...

Implementation code for using CSS text-emphasis to emphasize text

1. Introduction In the past, if you wanted to emp...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

17 404 Pages You'll Want to Experience

How can we say that we should avoid 404? The reas...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...

Independent implementation of nginx container configuration file

Create a container [root@server1 ~]# docker run -...

Install Python 3.6 on Linux and avoid pitfalls

Installation of Python 3 1. Install dependent env...

How to query date and time in mysql

Preface: In project development, some business ta...