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.
echo "有附件有温度的邮件" | mailx -s "这里是标题" -r email@example.com -a ./attachementfile.zip firstname.lastname@example.org
uuencode ./mysecret_video.gz video.gz | mail -s "好看的小片片" email@example.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.
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 "邮件主题" firstname.lastname@example.org
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 "邮件主题" email@example.com -A <要附加的文件>
To send an email to multiple recipients, just specify multiple email IDs separated by commas.
$ echo "电子邮件消息正文" | mail -s "邮件主题" firstname.lastname@example.org;email@example.com,firstname.lastname@example.org -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 "邮件主题" email@example.com -A <要附加的文件> <mailtext.txt
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.