上犹电脑信息网我们一直在努力
您的位置:上犹电脑信息网 > 设置问题 > 怎么发送邮件-4个可以发送完整电子邮件的命令行工具

怎么发送邮件-4个可以发送完整电子邮件的命令行工具

作者:上犹日期:

返回目录:设置问题

今天的文章里我们会讲到一些使用Linux命令行工具来发送带附件的电子邮件的方法。它有很多用处,比如在应用程序所在服务器上,使用电子邮件发送一个文件过来,或者你可以在脚本中使用这些命令来做一些自动化操作。在本文的例子中,我们会使用foo.tar.gz文件作为附件。


有不同的命令行工具可以发送邮件,这里我分享几个多数用户会使用的工具,如mailxmuttswaks


我们即将呈现的这些工具都是非常有名的,并且存在于多数Linux发行版默认的软件仓库中,你可以使用如下命令安装:


Debian / Ubuntu系统


apt-get install muttrapt-get install swaksrapt-get install mailxrapt-get install sharutilsr

在基于Red Hat的系统,如 CentOS或者Fedora


yum install muttryum install swaksryum install mailxryum install sharutilsr


1) 使用 mail / mailx

mailx工具在多数Linux发行版中是默认的邮件程序,现在已经支持发送附件了。如果它不在你的系统中,你可以使用上边的命令安装。有一点需要注意,老版本的mailx可能不支持发送附件,运行如下命令查看是否支持。


$ man mailr

第一行看起来是这样的:


mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .r

如果你看到它支持-a的选项(-a 文件名,将文件作为附件添加到邮件)和-s选项(-s 主题,指定邮件的主题),那就是支持的。可以使用如下的几个例子发送邮件。


a) 简单的邮件


运行mail命令,然后mailx会等待你输入邮件内容。你可以按回车来换行。当输入完成后,按Ctrl + D,mailx会显示EOT表示结束。


然后mailx会自动将邮件发送给收件人。


$ mail user@example.comrrHI,rGood MorningrHow are yourEOTr

b) 发送有主题的邮件


$ echo "Email text" | mail -s "Test Subject" user@example.comr

-s的用处是指定邮件的主题。


c) 从文件中读取邮件内容并发送


$ mail -s "message send from file" user@example.com < /path/to/filer

d) 将从管道获取到的echo命令输出作为邮件内容发送


$ echo "This is message body" | mail -s "This is Subject" user@example.comr

e) 发送带附件的邮件


$ echo “Body with attachment "| mail -a foo.tar.gz -s "attached file" user@example.comr

-a选项用于指定附件。



2) mutt

Mutt是类Unix系统上的一个文本界面邮件客户端。它有20多年的历史,在Linux历史中也是一个很重要的部分,它是最早支持进程打分和多线程处理的客户端程序之一。按照如下的例子来发送邮件。


a) 带有主题,从文件中读取邮件的正文,并发送


$ mutt -s "Testing from mutt" user@example.com < /tmp/message.txtr

b) 通过管道获取echo命令输出作为邮件内容发送


$ echo "This is the body" | mutt -s "Testing mutt" user@example.comr

c) 发送带附件的邮件


$ echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gzr

d) 发送带有多个附件的邮件


$ echo "This is the body" | mutt -s "Testing" user@example.com -a foo.tar.gz –a bar.tar.gzr


3) swaks

Swaks(Swiss Army Knife,瑞士军刀)是SMTP服务上的瑞士军刀,它是一个功能强大、灵活、可编程、面向事务的SMTP测试工具,由John Jetmore开发和维护。你可以使用如下语法发送带附件的邮件:


$ swaks -t "foo@bar.com" --header "Subject: Subject" --body "Email Text" --attach foo.tar.gzr

关于Swaks一个重要的地方是,它会为你显示整个邮件发送过程,所以如果你想调试邮件发送过程,它是一个非常有用的工具。


它会给你提供了邮件发送过程的所有细节,包括邮件接收服务器的功能支持、两个服务器之间的每一步交互。


(LCTT 译注:原文此处少了 sharutils 的相关介绍,而多了 uuencode 的介绍。)



4) uuencode

邮件传输系统最初是被设计来传送7位编码(类似ASCII)的内容的。这就意味这它是用来发送文本内容,而不能发会使用8位的二进制内容(如程序文件或者图片)。uuencode(“UNIX to UNIX encoding”,UNIX之间使用的编码方式)程序用来解决这个限制。使用uuencode,发送端将二进制格式的转换成文本格式来传输,接收端再转换回去。


我们可以简单地使用uuencodemailx或者mutt配合,来发送二进制内容,类似这样:


$ uuencode example.jpeg example.jpeg | mail user@example.comr


Shell脚本:解释如何发送邮件

#!/bin/bashrrFROM=""rSUBJECT=""rATTACHMENTS=""rTO=""rBODY=""rr# 检查文件名对应的文件是否存在rfunction check_filesr{routput_files=""rfor file in $1rdorif [ -s $file ]rthenroutput_files="${output_files}${file} "rfirdonerecho $output_filesr}rrecho "*********************"recho "E-mail sending script."recho "*********************"rechorr# 读取用户输入的邮件地址rwhile [ 1 ]rdorif [ ! $FROM ]rthenrecho -n -e "Enter the e-mail address you wish to send mail from:n[Enter] "relserecho -n -e "The address you provided is not valid:n[Enter] "rfirrread FROMrecho $FROM | grep -E '^.+@.+$' > /dev/nullrif [ $? -eq 0 ]rthenrbreakrfirdonerrechorr# 读取用户输入的收件人地址rwhile [ 1 ]rdorif [ ! $TO ]rthenrecho -n -e "Enter the e-mail address you wish to send mail to:n[Enter] "relserecho -n -e "The address you provided is not valid:n[Enter] "rfirrread TOrecho $TO | grep -E '^.+@.+$' > /dev/nullrif [ $? -eq 0 ]rthenrbreakrfirdonerrechorr# 读取用户输入的邮件主题recho -n -e "Enter e-mail subject:n[Enter] "rread SUBJECTrrechorrif [ "$SUBJECT" == "" ]rthenrecho "Proceeding without the subject..."rfirr# 读取作为附件的文件名recho -e "Provide the list of attachments. Separate names by space.rIf there are spaces in file name, quote file name with "."rread attrrechorr# 确保文件名指向真实文件rattachments=$(check_files "$att")recho "Attachments: $attachments"rrfor attachment in $attachmentsrdorATTACHMENTS="$ATTACHMENTS-a $attachment "rdonerrechorr# 读取完整的邮件正文recho "Enter message. To mark the end of message type ;; in new line."rread linerrwhile [ "$line" != ";;" ]rdorBODY="$BODY$linen"rread linerdonerrSENDMAILCMD="mutt -e "set from=$FROM" -s "$SUBJECT" r$ATTACHMENTS -- "$TO" <<< "$BODY""recho $SENDMAILCMDrrmutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- $TO <<< $BODYr

** 脚本输出 **


$ bash send_mail.shr*********************rE-mail sending script.r*********************rrEnter the e-mail address you wish to send mail from:r[Enter] test@gmail.comrrEnter the e-mail address you wish to send mail to:r[Enter] test@gmail.comrrEnter e-mail subject:r[Enter] Message subjectrrProvide the list of attachments. Separate names by space.rIf there are spaces in file name, quote file name with ".rsend_mail.shrrAttachments: send_mail.shrrEnter message. To mark the end of message type ;; in new line.rThis is a messagertextr;;r


总结

有很多方法可以使用命令行/Shell脚本来发送邮件,这里我们只分享了其中4个类Unix系统可用的工具。希望你喜欢我们的文章,并且提供您的宝贵意见,让我们知道您想了解哪些新工具。


作者:Bobbin Zachariah译者:goreliu校对:wxy


本文由 LCTT原创翻译,Linux中国荣誉推出


本文由 LCTT 原创翻译,Linux中国首发。也想加入译者行列,为开源做一些自己的贡献么?欢迎加入LCTT!


翻译工作和译文发表仅用于学习和交流目的,翻译工作遵照CC 协议规定,如果我们的工作有侵犯到您的权益,请及时联系我们。


欢迎遵照CC 协议规定转载,敬请在正文中标注并保留原文/译文链接和作者/译者等信息。


文章仅代表作者的知识和看法,如有不同观点,请楼下排队吐槽:D


本文标签:怎么发送邮件(9)

相关阅读

关键词不能为空
极力推荐

电脑蓝屏_电脑怎么了_win7问题_win10问题_设置问题_文件问题_上犹电脑信息网

关于我们