How to send email in Linux shell


A simple email

echo -e "To: [email protected]\nCC: [email protected]\nFrom: handy<[email protected]>\nSubject: test\n\nhello world" | sendmail -t

It seems a bit complicated. In fact, the sendmail program reads the source code of the mail from the standard input. The – t parameter indicates that the recipient information is extracted from the source code of the mail and sent to the recipient’s mail server. We will sort it out a bit and save the source code of the mail in the email.txt As follows

To: [email protected]
CC: [email protected]
From: handy<[email protected]>
Subject: test

hello world

Change the above command tocat email.txt | sendmail -tSo it’s clear at a glance. The email information received is as follows

Message format

As you can see from the previous email source code, the email is a text protocol similar to HTTP, which consists of the email header and the email content, separated by blank lines in the middle. Each line of the email header corresponds to a field, similar to the HTTP header, such as to, CC, from, subject, which represents the recipient, CC, sender, and title respectively. If there are multiple recipients or CC, use the The content of the e-mail is what we really see in the e-mail client

Mail client can view the mail source code, for example, the following is a mail I received the source code

Email title in Chinese

If Chinese characters are directly used in the e-mail title, the received e-mail will be garbled. In order to avoid this situation, Base64 coding should be used for Chinese, which is also the most commonly used coding method for e-mail. Of course, Chinese characters (UTF-8 or GBK, etc.) must be encoded before Base64 coding. This is the same concept as HTML coding, using UTF-8 and Base64 coding The format is as follows


Among them, XXXXXX is the encoded data, and python can be used to encode Chinese quickly. For example, UTF-8 coding is used for Chinese ‘test’, and then Base64 coding is used. The result is

>>> import base64
>>> base64.standard_ B64encode (U 'test'. Encode ('utf-8 '))

Use Chinese in from and subject, email source code is as follows

To: [email protected]
CC: [email protected]
From: =?UTF-8?B?5rWL6K+V?=<[email protected]>
Subject: =?UTF-8?B?5rWL6K+V?=

hello world

Here, the sender’s name and email title are changed to ‘test’, and the received email effect is

Use HTML for email content

If the email content is HTML code, you need to add a content type field to the email header to mark the text type. At the same time, you need to mark the character code of the email content. The following email source code sends the HTML content

To: [email protected]
CC: [email protected]
From: =?UTF-8?B?5rWL6K+V?=<[email protected]>
Subject: =?UTF-8?B?5rWL6K+V?=
Content-type: text/html;charset=utf-8

<h1>hello world</h1>

The effect of received mail is


The above is the whole content of this article, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]