mail

Send and receive emails from the command line

Supplementary instructions

mail command is a command-line email sending and receiving tool. The operating interface is not as easy to use as elm or pine, but its functions are very complete.

grammar

mail(option)(parameter)

Options

-b<address>: Specify the recipient address of the blind copy;
-c<address>: specifies the recipient address of the copy;
-f<mail file>: Read the emails in the specified mail file;
-i: Do not display information sent by the terminal;
-I: Use interactive mode;
-n: When the program is used, the settings in the mail.rc file are not used;
-N: Do not display the title of the email when reading the email;
-s<email subject>: specifies the subject of the email;
-u<user account>: Read the specified user’s mail;
-v: Display detailed information when executing.

Parameters

Email address: The email address of the recipient.

Example

Use shell directly as editor

mail -s "Hello from jsdig.com by shell" admin@jsdig.com
hello,this is the content of mail.
welcome to www.jsdig.com

The first line is the command entered. -s represents the subject of the email, and the following [email protected] is the recipient of the email. After entering this line of command and pressing Enter, we will enter the writing of the email body. We You can enter any text, such as the two lines above. When the email body is entered, you need to press CTRL+D to end the input. At this time, you will be prompted to enter the Cc address, which is the email carbon copy address. The email is sent without pressing Enter.

Using pipes for email sending

echo "hello,this is the content of mail.welcome to www.jsdig.com" | mail -s "Hello from jsdig.com by pipe" admin@jsdig.com

Use the pipe to type this line of command directly to complete sending the email, where the text after echo is the email body.

Use files to send emails

mail -s "Hello from jsdig.com by file" admin@jsdig.com < mail.txt

After using the above command, we can send the content of the mail.txt file to [email protected] as the content of the email.

You can use the above three methods to send emails to external mailboxes. However, because the email content is typed directly in the shell in the previous two methods, Chinese cannot be entered. Even if we enter Chinese by pasting, the received email will It's also garbled. But in the third way, we can edit the email content under window, put it under Linux, and then send it, so that we can send Chinese normally. However, there is currently no solution for the Chinese title of the email.

Because the mail program itself calls sendmail to send emails, we can use sendmail parameters in the mail command to configure it. For example, if I want to use a specific sender to send emails, I can use the following command:

mail -s "Hello from jsdig.com with sender" admin@jsdig.com -- -f user@jsdig.com<mail.txt

In the above command, we used parameters such as – -f [email protected], which is a sendmail option, where -f represents the email address of the sender of the email.

In many cases, we also need to use email to send attachments. It is also very simple to use the mail command to send attachments under Linux, but first you need to install the uuencode software package. This program encodes binary files to make them suitable for sending by email. Install this package on CentOS as follows:

yum install sharutils

After the installation is complete, we can send attachments using the following command:

uuencode test.txt test | mail -s "hello,see the attachement" [email protected]<mail.txt

After completion, you can send the text.txt file as an email attachment. uuencode has two parameters, the first is the file to be sent, and the second is the file name to be displayed.

Here I mainly introduce some methods of using mail to send emails under CentOS. The required requirement is that your Linux must have sendmail installed and turned on, and ensure that you can connect to the external network. In addition, the commands mentioned in the article have been personally tested by me and are guaranteed to be fully usable, but you need to replace the email address in the command with your own email address.