mailx的命令_Linux邮件和mailx命令教程,包括示例和从命令行发送电子邮件

mailx的命令

mailx的命令

Linux has a lot of tools, services, and applications related to email. An email has a different architecture than standard client-server. We will look at a command-line based mailing application named mailx. mailx more advanced version of the mail tool. mailx command supports the MIME, IMAP, POP3, SMTP, and S/MIME protocols and based Berkeley Mail 8.1 mail command.

Linux有许多与电子邮件相关的工具,服务和应用程序。 电子邮件的架构不同于标准的客户端服务器。 我们将看一个基于命令行的邮件应用程序mailx。 mailx mail工具的更高级版本。 mailx命令支持MIME,IMAP,POP3,SMTP和S / MIME协议以及基于Berkeley Mail 8.1的邮件命令。

一般概念 (General Concept)

As stated in previous paragraph email systems are bit more complex than standard client server architecture. There are some terms we should learn before continue

如前一段所述,电子邮件系统比标准客户端服务器体系结构要复杂得多。 在继续之前,我们应该学习一些术语

  • Mail Client is an application used by user directly to send and get emails. Mail clients connects to the mail transfer agent to transmit emails.

    Mail Client是用户直接用于发送和获取电子邮件的应用程序。 邮件客户端连接到邮件传输代理以传输电子邮件。

  • Mail Transfer Agent (MTA) is an intermedia component used to receive, send, store mail messages. there are some protocol used to accomplish these tasks like smtp,pop3,imap etc.

    Mail Transfer Agent (MTA)是用于接收,发送和存储邮件消息的中间组件。 有一些用于完成这些任务的协议,例如smtp,pop3,imap等。

Below are some architectural view of a simple email transmission.

以下是一些简单的电子邮件传输的体系结构视图。

Mail Client Sender --> MTA of sender --> MTA receiver --> Mail Client Receiver

安装mailx命令 (Install mailx Command)

Linux distributions provides two mailx command one from the mailutils package which is installed by default and the other one is from the bsd-mailx package. In order to work properly we should install the bsd-mailx package like below.

Linux发行版提供了两个mailx命令,一个来自默认安装的mailutils软件包,另一个来自bsd-mailx软件包。 为了正常工作,我们应该像下面那样安装bsd-mailx软件包。

$ sudo apt install bsd-mailx
Install mailx Command
安装mailx命令

帮帮我(Help)

Brief help information about the mailx command can be printed with the --help option like below.

可以使用--help选项打印有关mailx命令的简要帮助信息,如下所示。

$ mail --help
Mail Help
Mail Help
邮件帮助

发送电子邮件(Send Email)

We will simply mail some text without provide extra information. This is the fastest way to mail also. In this tutorial we will simply use local mail provider but in the SMTP configuration section we will deal with a SMTP server. In this example we will provide subject part of the mail with -s option and we will also provide email address which is the local user named ismail

我们只会邮寄一些文本,而不会提供其他信息。 这也是最快的邮寄方式。 在本教程中,我们将仅使用本地邮件提供程序,但在SMTP配置部分中,我们将处理SMTP服务器。 在此示例中,我们将为邮件的主题部分提供-s选项,还将提供电子邮件地址,该地址为名为ismail的本地用户

$ mail -s "Hello World" [email protected]
Send Email
Send Email
发送电子邮件

There is a warning which says Null message body; hope that's ok . We think it is ok.

有一个警告,指出Null message body; hope that's ok Null message body; hope that's ok 。 我们认为还可以。

LEARN MORE  How To Specify Host, Port and Protocol For Tcpdump?
了解更多信息如何为Tcpdump指定主机,端口和协议?

阅读电子邮件 (Read Email)

In previous example we have send mail to local system user ismail . When user ismail logins to the system he will get a message saying he have an email message. This email can be read from /var/mail/ismail where ismail is the users name with simple cat command.

在前面的示例中,我们已将邮件发送到本地系统用户ismail 。 当用户ismail登录到系统时,他将收到一条消息,提示他有一封电子邮件。 可以从/var/mail/ismail中读取此电子邮件,其中ismail是使用简单cat命令的用户名。

$ cat /var/mail/ismail
Read Email
Read Email
阅读电邮

从文件读取电子邮件正文(Read Email Body From File)

While sending email there will be body part of the mail. This body part can be written by hand. But if it is long and repetitive task we do not want to do always there is an alternative. Body part can be read from a file by simply redirecting the content to the mail command like below. In this example body.txt file contains body part of the mail. This file may also an html file too.

在发送电子邮件时,邮件的正文部分将会出现。 身体部位可以手写。 但是,如果这是一项漫长而又重复的任务,那么我们不希望总是有另一种选择。 只需将内容重定向到如下所示的mail命令,即可从文件中读取主体部分。 在此示例中, body.txt文件包含邮件的正文部分。 该文件也可能是html文件。

body.txt (body.txt)

Hi poftut 
 
How are you? I hope your pageviews are good. 
 
Have a high sessioned day

And we send

然后我们发送

$ mail -s "Hello World" [email protected] < body.txt

There is other way to send mail by redirecting body content with pipe like below.

还有其他发送邮件的方法,如下所示,使用管道重定向正文内容。

$ cat body.txt | mail -s "Hello World" [email protected]

设置多个收件人 (Set Multiple Recipient)

Another useful feature of mail command is providing multiple recipients by simply delimiting recipients emails. In this example we will send email both to the [email protected] and [email protected] . Keep in mind that there may be more than two recipients.

mail命令的另一个有用功能是通过简单地分隔收件人电子邮件来提供多个收件人。 在此示例中,我们将既发送电子邮件到[email protected]也发送给[email protected] [email protected] 。 请记住,可能有两个以上的收件人。

$ mail -s "Hello World" [email protected],[email protected] < body.txt
Set Multiple Recipient
Set Multiple Recipient
设置多个收件人

As we can see mail notification is appeared very fast.

如我们所见,邮件通知的显示速度非常快。

将CC和BCC添加到邮件 (Add CC and BCC To The Mail)

Carbon Copy - CC and Blind Carbon Copy - BCC are used to send copy of the mail to the other recipients in a visible or hidden way. To provide CC use -c option with email addresses. To provide BCC use -b option with email addresses. In the example we will send mail to the [email protected] with [email protected] in CC and [email protected] in BCC. From this example we will use same options but more featured mailx command.

Carbon Copy - CCBlind Carbon Copy - BCC用于以可见或隐藏的方式将邮件的副本发送给其他收件人。 要为CC提供-c选项和电子邮件地址。 要提供密件抄送,请在电子邮件地址中使用-b选项。 在这个例子中,我们将发送邮件至[email protected][email protected]在CC和[email protected]在BCC。 在此示例中,我们将使用相同的选项,但功能更多的mailx命令。

$ mailx -s "Hello World" [email protected] -c [email protected] -b [email protected]  < body.txt
CC and BCC
CC and BCC
CC和BCC

If we look our mailbox we can see mail and cc but can not see bcc

如果我们查看邮箱,我们可以看到邮件和抄送,但看不到密件抄送

LEARN MORE  What Is SMTP Port Number (25, 587, 465, 2525)?
了解更多信息什么是SMTP端口号(25、587、465、2525)?

从名称和地址指定 (Specify From Name And Address)

As we know we can set sender name and email address explicitly. We will use -r option to set sender name and email address. Specify email address like <[email protected]> . In this example we write sender name as İsmail Baydan and sender address as [email protected]

众所周知,我们可以显式设置发件人姓名和电子邮件地址。 我们将使用-r选项设置发件人名称和电子邮件地址。 指定电子邮件地址,例如< [email protected] > 。 在此示例中,我们将发件人名称写为İsmail Baydan ,将发件人地址写为[email protected]

$ mailx -s "Hello World" [email protected] -r "İsmail Baydan<[email protected]>"
Specify From Name And Address
Specify From Name And Address
从名称和地址指定

The sender address and name can be seen explicitly.

可以明确看到发件人的地址和名称。

指定“回复”地址 (Specify “Reply To” Address)

Reply to address is used to set return address for the mail if the receiver wants to write back. This can be especially useful for automated systems where return will be do a specific email address.Reply to information can be set with -S option. In the example we will set reply to address as [email protected]

如果收件人想回信,则“回复地址”用于设置邮件的回信地址。 这对于自动化系统特别有用,在自动化系统中,返回将使用特定的电子邮件地址。可以使用-S选项设置对信息的-S 。 在示例中,我们将回复地址设置为[email protected]

$ mailx -s "Hello World" -S replyto="İsmail Baydan <[email protected]>" [email protected]

添加附件 (Add Attachment)

Attachments are crusial part of the email. Emai users generally attaches some documents, image, zip file to the emails to send to the receiver. In GUI email clients it can be easy as copy paste or selecting file. But how can we send email attachments in command line interface. Attachments can be added with-a option with the attachements path. In the example we will add file name a.txt to our mail as attachment.

附件是电子邮件的关键部分。 Emai用户通常会将一些文档,图像,zip文件附加到电子邮件中以发送给接收者。 在GUI电子邮件客户端中,复制粘贴或选择文件很容易。 但是我们如何在命令行界面中发送电子邮件附件。 可以使用-a选项在附件路径中添加附件。 在示例中,我们将文件名a.txt作为附件添加到我们的邮件中。

$ mailx -s "Some File" [email protected] -a a.txt
Attachment
Attachment
附件

使用外部SMTP服务器(Use External SMTP Server)

Up to now we have used the the local mail system. Local mail system is provided as a simple mechanism by Linux operating system. In the real world examples email system generally uses SMTP, POP3, IMAP services. But in order to send emails we need to setup SMTP server for the mail and mailx command. SMTP configuraiont is put into command line and have some text to type.

到目前为止,我们已经使用了本地邮件系统。 Linux操作系统将本地邮件系统作为一种简单的机制提供。 在实际示例中,电子邮件系统通常使用SMTP,POP3,IMAP服务。 但是,为了发送电子邮件,我们需要为mail和mailx命令设置SMTP服务器。 SMTP配置文件放入命令行,并输入一些文本。

$ mailx -v -s "This is the subject" -S smtp="mail.example.com:587"  -S smtp-use-starttls  -S smtp-auth=login  \
-S smtp-auth-user="[email protected]"  -S smtp-auth-password="1q2w3e" -S ssl-verify=ignore  [email protected]

We will look what this command options and arguments mean.

我们将看看此命令选项和参数的含义。

  • -S smtp="mail.example.com:587" specifies the SMTP server host name or IP address and port number which is 587

    -S smtp="mail.example.com:587"指定SMTP服务器主机名或IP地址和端口号587

  • -S smtp-use-starttls specifies to use tls encrytion to make communication encrypted and secure

    -S smtp-use-starttls指定使用tls加密使通信加密和安全

  •  -S smtp-auth=login specifies what type of SMTP authentication will occur.

    -S smtp-auth=login指定将进行哪种类型的SMTP身份验证。

  • -S smtp-auth-user="[email protected]" specified the username that will be used to authenticate

    -S smtp-auth-user=" [email protected] "指定了用于认证的用户名

  •  -S smtp-auth-password="1q2w3e" specifies the password. Be sure that the system you are using is secure because the clear text password is provided.

    -S smtp-auth-password="1q2w3e"指定密码。 由于提供了明文密码,因此请确保所使用的系统是安全的。

  • -S ssl-verify=ignore this is used to ignore ssl verification of SMTP server. This option is used to prevent self signed ssl problems. But this makes the operation less secure.

    -S ssl-verify=ignore这用于忽略SMTP服务器的ssl验证。 此选项用于防止自签名ssl问题。 但这会使操作的安全性降低。

LEARN MORE  How To Download and Install Windows Essentials?
了解更多如何下载和安装Windows Essentials?

调试,故障排除和详细输出 (Debug, Troubleshoot and Verbose Output)

The general assumption is that there will be problems where sending emails. So wee need to debug to get details of the problem. For both mail and mailx commands. The -v option can be used to debug the mailing operation.

一般的假设是在发送电子邮件时会出现问题。 因此,我们需要进行调试以获取问题的详细信息。 对于mailmailx命令。 -v选项可用于调试邮件操作。

$ mailx -v -s "Some File" [email protected] < body.txt

从命令行使用mailx命令列出电子邮件 (List Emails with mailx Command From Command Line)

We list and read delivered emails with the mailx command like below. The list provides information like sender, date, time, size and the subject part.

我们使用如下的mailx命令列出并阅读传递的电子邮件。 该列表提供了发件人,日期,时间,大小和主题部分等信息。

$ mailx
List Emails with mailx Command From Command Line
从命令行使用mailx命令列出电子邮件

翻译自: https://www.poftut.com/linux-mail-mailx-commands-tutorial-examples-send-email-command-line/

mailx的命令

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mail和mailx是两个不同的命令,但用途相似,都用于在Linux系统下发送邮件。下面是它们的区别和使用示例: 1. mail命令: - 区别:mail命令Linux系统自带的邮件发送工具,它是基于sendmail的命令行接口。 - 使用示例: - 发送邮件:`echo "邮件内容" | mail -s "邮件主题" 收件人邮箱地址` - 发送带附件的邮件:`echo "邮件内容" | mail -s "邮件主题" -a 附件文件 收件人邮箱地址` - 从文件中读取邮件内容:`mail -s "邮件主题" 收件人邮箱地址 < 邮件内容文件` - 查看收件箱:`mail` - 删除所有邮件:`mail -d` - 退出邮件查看界面:按下`q` 2. mailx命令: - 区别:mailx命令也是用于发送邮件,但它相对于mail命令来说更加灵活和强大,支持更多的邮件发送选项。 - 使用示例: - 发送邮件:`echo "邮件内容" | mailx -s "邮件主题" 收件人邮箱地址` - 发送带附件的邮件:`echo "邮件内容" | mailx -s "邮件主题" -a 附件文件 收件人邮箱地址` - 发送HTML格式邮件:`echo "<html><body><h1>Hello, World!</h1></body></html>" | mailx -s "邮件主题" -a "Content-Type: text/html" 收件人邮箱地址` - 从文件中读取邮件内容:`mailx -s "邮件主题" 收件人邮箱地址 < 邮件内容文件` - 查看收件箱:`mailx` - 删除所有邮件:`mailx -d` - 退出邮件查看界面:按下`q` 请注意,以上示例中的参数和选项可以根据实际需求进行调整和组合使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值