2

Send email with attachment

You don’t need a client, and you don’t need to have your own mailbox. Of course, there is a high probability that you will be in the trash can, but if you back up files for yourself occasionally, who cares about trash.

TL;DR

Linux

 echo "有附件有温度的邮件" | mailx -s "这里是标题" -r www@qsn.so -a ./attachementfile.zip   yujiaao@msn.com

Mac OS

 uuencode ./mysecret_video.gz video.gz | mail -s "好看的小片片"    yujiaao@msn.com 

The key to becoming an advanced Linux user is to use more command lines and less GUI; more keyboards and fewer mice! With the spread of Linux command-line tools, the use of the command line can not only perform administrative tasks, but also perform some non-administrative, actually vital daily tasks.

In this article, we will learn how to use the mail command in Linux to send emails with file attachments.

prerequisites

You must have configured your email with SMTP in the Linux machine. This email and server will be used by the "mail" program we are going to learn about today.

Send email from the command line in Linux
The program "Mail" can be used to send e-mails and file attachments with e-mails from the command line. This program is not available by default and can be installed in Debian and Red Hat based distributions using the following command:

$ sudo apt install mailutils [在Debian/Ubuntu/Mint 上]
$ sudo dnf install mailx [在RedHat/CentOS/Fedora 上]

The syntax for sending email using "mail" is as follows:

$ echo "电子邮件消息正文" | mail -s "邮件主题" target@domain.com

You can see that we are using the echo command to output the message body and redirect this output to the "mail" command. This is because the'mail' command reads the message body input from standard input.

Send emails with file attachments from the command line
Similarly, to use mail to attach files,'-A' can use parameters:

$ echo "电子邮件消息正文" | mail -s "邮件主题" target@domain.com -A <要附加的文件>

To send an email to multiple recipients, just specify multiple email IDs separated by commas.

$ echo "电子邮件消息正文" | mail -s "邮件主题" target1@domain.com;target2@domain.com,target3@domain.com -A <要附加的文件>

To include a text file as the message body of the email, instead of using echo, you can redirect the text of the file to a command as follows:

$ mail -s "邮件主题" target@domain.com -A <要附加的文件> <mailtext.txt

in conclusion

Today we saw a way to send emails and attachments from the Linux command line. There are other programs, such as "mutt" and "Sendmail", which are similar to "mail" and can be used for the same purpose.


Yujiaao
12.7k 声望4.7k 粉丝

[链接]