在Linux命令行中通过电子邮件发送邮件

在本文中,您将学习如何使用流行的邮件命令发送电子邮件。同样重要的是,您还需要学习如何发送Linux电子邮件附件。一些使用的命令行选项有:

  • -s: denotes the mail’s subject
  • -a: for denoting attachment
  • -c: for the copy email address (CC)
  • -b: for the blind copy email address (BCC)

通过命令行在Linux中发送电子邮件

我们将学习关于在Linux中发送电子邮件的以下命令。

    1. 邮件

 

    1. 邮件信箱

 

    1. Mutt

 

    1. Mpack

 

    Sendmail

使用邮件命令

Linux的mail命令非常流行,通常用于在命令行发送电子邮件。Debian系统中的mail是作为mailutils包的一部分安装的,而Redhat系统中则是作为mailx包的一部分安装的。这两个命令可以在命令行上处理消息。要在Debian和Ubuntu系统中安装mailutils,请运行:

$ sudo apt install mailutils -y

对于CentOS和RedHat发行版,运行:

$ yum install mailx
Internet Site option mail command

测试邮件命令

如果邮件命令成功安装,请使用以下格式进行测试应用并按回车键:

$ mail –s "Test Email" email_address

把email_address替换为你的电子邮件地址。例如,

$ mail –s "Test Email" james@example.com
Linux send email example
$ echo "sample message" | mail -s "sample mail subject" email_address

例如,

$ echo "Hello world" | mail -s "Test" james@example.com
Linux email with subject
$ mail -s "subject" -A message.txt email_address

-A标志用于定义文件的附加内容。例如:

$ mail -s "Important Notice" -A message.txt james@example.com
Linux email with attachment
$ mail –s "test header" email_address email_address2

2. 使用邮件命令mailx。

Mailx是mail命令的更新版本,在其他实现中曾被称为nail。Mailx自1986年开始存在,并于1992年纳入POSIX中。Mailx是Debian的邮件综合包中的一部分,用于各种场景。用户、系统管理员和开发人员都可以使用这个邮件工具。Mailx的实现与mail命令行语法相同。要在Debian/Ubuntu系统中安装mailx,请运行以下命令:

$ sudo apt install mailx

在RedHat和CentOS中安装mailx的方法是:

$ yum install mailx

测试Mailx命令

您可以使用echo命令将输出直接发送到邮件命令中,而不需要提示输入CC和消息正文,如下所示:

$ echo "message body" | mail -s "subject" email_address

例如,

$ echo "Make the most out of Linux!" | mail -s "Welcome to Linux" james@example.com

3. 使用MUTT命令

Mut是一款轻量级的Linux命令行邮件客户端。与只能进行基本操作的mail命令不同,mutt能够发送文件附件。Mut还可以通过终端阅读来自POP/IMAP服务器的邮件,并与本地用户进行连接。在Debian / Ubuntu系统上安装mutt,请运行以下命令:

$ sudo apt install mutt

在Redhat / CentOS系统中安装mutt,请运行以下命令:

$ sudo yum install mutt

测试Mutt命令。

你可以在邮件地址后面使用 < /dev/null ,使用 mutt 发送一条空消息。

$ mutt -s "Test Email" email_address < /dev/null 

例如,

$ mutt -s "Greetings" james@example.com < /dev/null 
Mutt Blank Email
$ echo "Message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- email_address

举个例子,

$ echo "Hey guys! How's it going ?" | mutt -a report.doc -s "Notice !" -- james@jaykiarie.com
Mutt Email File Attached

4. 使用mpack命令

在Debian / Ubuntu系统中安装mpack命令,用于将文件编码成MIME消息并发送给一个或多个收件人,甚至可以用于发表到不同的新闻组。

$ sudo apt install mpack 

在Redhat/CentOS系统中安装mpack,请运行以下命令:

$ sudo yum install mpack

测试mpack命令

使用mpack通过命令行发送电子邮件或附件就像是这么简单:

$ mpack -s "Subject here" -a file email_address

例如,

$ mpack -s "Sales Report 2019" -a report.doc james@jaykiarie.com
Linux Mpack Send Email Attach File

5. 使用sendmail

在许多发行版中,这条命令是另一个广泛使用的SMTP服务器。要在Debian/ Ubuntu系统中安装sendmail,请运行以下命令:

$ sudo apt install sendmail

在RedHat / CentOS系统中安装sendmail,请运行以下命令:

$ sudo yum install sendmail

测试发送邮件命令

您可以使用以下指示来使用sendmail命令发送电子邮件:

$ sendmail email_address < file

例如,我创建了一个文件report.doc,其中包含以下内容:

Hello there !

发送消息的命令将是,

$ sendmail < report.doc james@example.com
Sendmail Output

将以下内容用中文进行简述一种选择:

简述:

虽然命令行电子邮件客户端更简洁且计算负荷较低,但只能用来发送邮件到个人邮件域,而不能发送到Gmail或Yahoo等域名,因为需要额外的身份验证。此外,你也无法从外部SMTP服务器接收邮件。通常,使用类似Thunderbird或Evolution的图形用户界面电子邮件客户端会更容易避免出现无法送达的问题。

发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds