在本文中,您将学习如何使用流行的邮件命令发送电子邮件. 同样重要的是,您也可以学习如何发送Linux电子邮件附件。
- -s:表示邮件的主题
- -a:用于表示附件
- -c:用于复制电子邮件地址(CC)
- -b:用于盲目复制电子邮件地址(BCC)
Linux 从命令行发送电子邮件
我们将学习如何在Linux中发送电子邮件。
- 邮件
- 邮件
- 邮件
- 邮件
- 邮件
1、使用邮件命令
Linux 邮件命令非常受欢迎,通常用于从命令行发送电子邮件。邮件安装在 Debian 和 Redhat 系统上作为 mailutils 和 mailx 包的一部分。
1$ sudo apt install mailutils -y
对于 CentOS 和 RedHat 发行版,运行:
1$ yum install mailx
When you run the command, the following window will pop up. Press the TAB button and hit on 'OK' In the next Window, scroll and hit 'Internet Site'.
The system will thereafter finish up with the installation process.
测试邮件命令
如果邮件命令已成功安装,请使用以下格式测试应用程序并按入:
1$ mail –s "Test Email" email_address
用您的电子邮件地址代替email_address
。
1$ mail –s "Test Email" [email protected]
After pressing "Enter", you’ll be prompted for a Carbon Copy (Cc:) address. If you wish not to include a copied address, proceed and hit ENTER. Next, type the message or the body of the Email and hit ENTER. Finally, Press Ctrl + D simultaneously to send the Email. Output
Alternatively, you can use the echo command to pipe the message you want to send to the mail command as shown below.
1$ echo "sample message" | mail -s "sample mail subject" email_address
例如,
1$ echo "Hello world" | mail -s "Test" [email protected]
Output Let’s assume you have a file that you want to attach. Let's call the file
message.txt
How do you go about it? Use the command below.
1$ mail -s "subject" -A message.txt email_address
-A
旗定义了文件的附件,例如;
1$ mail -s "Important Notice" -A message.txt [email protected]
Output
To send an email to many recipients run:
1$ mail –s "test header" email_address email_address2
2、使用 mailx 命令
Mailx 是邮件命令的最新版本,以前在其他实现中被称为 nail。 Mailx 自 1986 年以来一直存在,并于 1992 年被纳入 POSIX 中。 Mailx 是 Debian 的邮件组合包的一部分,用于各种场景。用户、系统管理员和开发人员可以使用此邮件工具。 mailx 的实现也与邮件命令行语法相同。
1$ sudo apt install mailx
要在 RedHat & CentOS 运行中安装 mailx:
1$ yum install mailx
测试 Mailx 命令
您可以使用 echo 命令将输出指向邮件命令,而不会被提示为 CC 和消息体,如下所示:
1$ echo "message body" | mail -s "subject" email_address
例如,
1$ echo "Make the most out of Linux!" | mail -s "Welcome to Linux" [email protected]
3、使用MUTT命令
Mutt 是一个轻量级的 Linux 命令行电子邮件客户端。 与可以做基本的事情的邮件命令不同,mutt 可以发送文件附件。 Mutt 还读取来自 POP/IMAP 服务器的电子邮件,并通过终端连接本地用户。 要在 Debian/Ubuntu 系统中安装mutt:
1$ sudo apt install mutt
要在 Redhat / CentOS 系统中安装 mutt:
1$ sudo yum install mutt
测试Mutt命令
您可以在电子邮件地址后直接发送< /dev/null
的空白消息。
1$ mutt -s "Test Email" email_address < /dev/null
例如,
1$ mutt -s "Greetings" [email protected] < /dev/null
Output Mutt command can also be used to attach a file as follows.
1$ echo "Message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- email_address
例如,
1$ echo "Hey guys! How's it going ?" | mutt -a report.doc -s "Notice !" -- [email protected]
Output
使用 mpack 命令
mpack 命令用于将文件编码为 MIME 消息,并将其发送给一个或多个收件人,甚至可以用于发布到不同的新闻组。
1$ sudo apt install mpack
要在 Redhat / CentOS 系统中安装 mpack:
1$ sudo yum install mpack
mpack 命令测试
使用mpack通过命令行发送电子邮件或附件很简单:
1$ mpack -s "Subject here" -a file email_address
例如,
1$ mpack -s "Sales Report 2019" -a report.doc [email protected]
Output
五、使用邮件
此命令是许多发行版中使用的另一个流行的 SMTP 服务器. 要在 Debian/ Ubuntu 系统中安装 sendmail,运行:
1$ sudo apt install sendmail
要在 RedHat / CentOS 系统中安装 sendmail:
1$ sudo yum install sendmail
测试发送命令
您可以使用以下指令使用 sendmail 命令发送电子邮件:
1$ sendmail email_address < file
例如,我创建了一个文件 report.doc
包含以下文本:
1Hello there !
使命的发送是:
1$ sendmail < report.doc [email protected]
Output You can use -s option to specify the email subject.
摘要
虽然命令行电子邮件客户端更简单,计算量较少,但您只能使用它们将电子邮件发送到个人电子邮件域,而不是到 Gmail 或 Yahoo 域,因为需要额外的身份验证。