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

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

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

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

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

Debian / Ubuntu 系统

  1. <span class="pln">apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install mutt</span>
  2. <span class="pln">apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install swaks</span>
  3. <span class="pln">apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install mailx</span>
  4. <span class="pln">apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install sharutils</span>

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

  1. <span class="pln">yum install mutt</span>
  2. <span class="pln">yum install swaks</span>
  3. <span class="pln">yum install mailx</span>
  4. <span class="pln">yum install sharutils</span>

 

1) 使用 mail / mailx

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

  1. <span class="pln">$ man mail</span>

第一行看起来是这样的:

  1. <span class="pln">mailx </span><span class="pun">[-</span><span class="typ">BDdEFintv</span><span class="pun">~]</span><span class="pun">[-</span><span class="pln">s subject</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">a attachment </span><span class="pun">]</span><span class="pun">[-</span><span class="pln">c cc</span><span class="pun">-</span><span class="pln">addr</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">b bcc</span><span class="pun">-</span><span class="pln">addr</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">r </span><span class="kwd">from</span><span class="pun">-</span><span class="pln">addr</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">h hops</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">A account</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">S variable</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]]</span><span class="pln"> to</span><span class="pun">-</span><span class="pln">addr </span><span class="pun">.</span><span class="pun">.</span><span class="pun">.</span>

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

a) 简单的邮件

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

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

  1. <span class="pln">$ mail user@example</span><span class="pun">.</span><span class="pln">com</span>
  2. <span class="pln">HI</span><span class="pun">,</span>
  3. <span class="typ">Good</span><span class="typ">Morning</span>
  4. <span class="typ">How</span><span class="pln"> are you</span>
  5. <span class="pln">EOT</span>

b) 发送有主题的邮件

  1. <span class="pln">$ echo </span><span class="str">"Email text"</span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Test Subject"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com</span>

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

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

  1. <span class="pln">$ mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"message send from file"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com </span><span class="pun"><</span><span class="str">/path/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">file</span>

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

  1. <span class="pln">$ echo </span><span class="str">"This is message body"</span><span class="pun">|</span><span class="pln"> mail </span><span class="pun">-</span><span class="pln">s </span><span class="str">"This is Subject"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com</span>

e) 发送带附件的邮件

  1. <span class="pln">$ echo </span><span class="pun">“</span><span class="typ">Body</span><span class="kwd">with</span><span class="pln"> attachment </span><span class="str">"| mail -a foo.tar.gz -s "</span><span class="pln">attached file</span><span class="str">" [email protected]</span>

-a选项用于指定附件。

 

2) mutt

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

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

  1. <span class="pln">$ mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Testing from mutt"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com </span><span class="pun"><</span><span class="str">/tmp/</span><span class="pln">message</span><span class="pun">.</span><span class="pln">txt</span>

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

  1. <span class="pln">$ echo </span><span class="str">"This is the body"</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Testing mutt"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com</span>

c) 发送带附件的邮件

  1. <span class="pln">$ echo </span><span class="str">"This is the body"</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Testing mutt"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com </span><span class="pun">-</span><span class="pln">a </span><span class="pun">/</span><span class="pln">tmp</span><span class="pun">/</span><span class="pln">foo</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>

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

  1. <span class="pln">$ echo </span><span class="str">"This is the body"</span><span class="pun">|</span><span class="pln"> mutt </span><span class="pun">-</span><span class="pln">s </span><span class="str">"Testing"</span><span class="pln"> user@example</span><span class="pun">.</span><span class="pln">com </span><span class="pun">-</span><span class="pln">a foo</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz </span><span class="pun">–</span><span class="pln">a bar</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>

 

3) swaks

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

  1. <span class="pln">$ swaks </span><span class="pun">-</span><span class="pln">t </span><span class="str">"[email protected]"</span><span class="pun">--</span><span class="pln">header </span><span class="str">"Subject: Subject"</span><span class="pun">--</span><span class="pln">body </span><span class="str">"Email Text"</span><span class="pun">--</span><span class="pln">attach foo</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>

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

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

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

 

4) uuencode

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

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

  1. <span class="pln">$ uuencode example</span><span class="pun">.</span><span class="pln">jpeg example</span><span class="pun">.</span><span class="pln">jpeg </span><span class="pun">|</span><span class="pln"> mail user@example</span><span class="pun">.</span><span class="pln">com</span>

 

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

  1. <span class="com">#!/bin/bash</span>
  2. <span class="pln">FROM</span><span class="pun">=</span><span class="str">""</span>
  3. <span class="pln">SUBJECT</span><span class="pun">=</span><span class="str">""</span>
  4. <span class="pln">ATTACHMENTS</span><span class="pun">=</span><span class="str">""</span>
  5. <span class="pln">TO</span><span class="pun">=</span><span class="str">""</span>
  6. <span class="pln">BODY</span><span class="pun">=</span><span class="str">""</span>
  7. <span class="com"># 检查文件名对应的文件是否存在</span>
  8. <span class="kwd">function</span><span class="pln"> check_files</span><span class="pun">()</span>
  9. <span class="pun">{</span>
  10. <span class="pln">output_files</span><span class="pun">=</span><span class="str">""</span>
  11. <span class="kwd">for</span><span class="pln"> file </span><span class="kwd">in</span><span class="pln"> $1</span>
  12. <span class="kwd">do</span>
  13. <span class="kwd">if</span><span class="pun">[</span><span class="pun">-</span><span class="pln">s $file </span><span class="pun">]</span>
  14. <span class="kwd">then</span>
  15. <span class="pln">output_files</span><span class="pun">=</span><span class="str">"${output_files}${file} "</span>
  16. <span class="kwd">fi</span>
  17. <span class="kwd">done</span>
  18. <span class="pln">echo $output_files</span>
  19. <span class="pun">}</span>
  20. <span class="pln">echo </span><span class="str">"*********************"</span>
  21. <span class="pln">echo </span><span class="str">"E-mail sending script."</span>
  22. <span class="pln">echo </span><span class="str">"*********************"</span>
  23. <span class="pln">echo</span>
  24. <span class="com"># 读取用户输入的邮件地址</span>
  25. <span class="kwd">while</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span>
  26. <span class="kwd">do</span>
  27. <span class="kwd">if</span><span class="pun">[</span><span class="pun">!</span><span class="pln"> $FROM </span><span class="pun">]</span>
  28. <span class="kwd">then</span>
  29. <span class="pln">echo </span><span class="pun">-</span><span class="pln">n </span><span class="pun">-</span><span class="pln">e </span><span class="str">"Enter the e-mail address you wish to send mail from:\n[Enter] "</span>
  30. <span class="kwd">else</span>
  31. <span class="pln">echo </span><span class="pun">-</span><span class="pln">n </span><span class="pun">-</span><span class="pln">e </span><span class="str">"The address you provided is not valid:\n[Enter] "</span>
  32. <span class="kwd">fi</span>
  33. <span class="pln">read FROM</span>
  34. <span class="pln">echo $FROM </span><span class="pun">|</span><span class="pln"> grep </span><span class="pun">-</span><span class="pln">E </span><span class="str">'^.+@.+$'</span><span class="pun">></span><span class="str">/dev/</span><span class="kwd">null</span>
  35. <span class="kwd">if</span><span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">-</span><span class="pln">eq </span><span class="lit">0</span><span class="pun">]</span>
  36. <span class="kwd">then</span>
  37. <span class="kwd">break</span>
  38. <span class="kwd">fi</span>
  39. <span class="kwd">done</span>
  40. <span class="pln">echo</span>
  41. <span class="com"># 读取用户输入的收件人地址</span>
  42. <span class="kwd">while</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span>
  43. <span class="kwd">do</span>
  44. <span class="kwd">if</span><span class="pun">[</span><span class="pun">!</span><span class="pln"> $TO </span><span class="pun">]</span>
  45. <span class="kwd">then</span>
  46. <span class="pln">echo </span><span class="pun">-</span><span class="pln">n </span><span class="pun">-</span><span class="pln">e </span><span class="str">"Enter the e-mail address you wish to send mail to:\n[Enter] "</span>
  47. <span class="kwd">else</span>
  48. <span class="pln">echo </span><span class="pun">-</span><span class="pln">n </span><span class="pun">-</span><span class="pln">e </span><span class="str">"The address you provided is not valid:\n[Enter] "</span>
  49. <span class="kwd">fi</span>
  50. <span class="pln">read TO</span>
  51. <span class="pln">echo $TO </span><span class="pun">|</span><span class="pln"> grep </span><span class="pun">-</span><span class="pln">E </span><span class="str">'^.+@.+$'</span><span class="pun">></span><span class="str">/dev/</span><span class="kwd">null</span>
  52. <span class="kwd">if</span><span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span><span class="pun">-</span><span class="pln">eq </span><span class="lit">0</span><span class="pun">]</span>
  53. <span class="kwd">then</span>
  54. <span class="kwd">break</span>
  55. <span class="kwd">fi</span>
  56. <span class="kwd">done</span>
  57. <span class="pln">echo</span>
  58. <span class="com"># 读取用户输入的邮件主题</span>
  59. <span class="pln">echo </span><span class="pun">-</span><span class="pln">n </span><span class="pun">-</span><span class="pln">e </span><span class="str">"Enter e-mail subject:\n[Enter] "</span>
  60. <span class="pln">read SUBJECT</span>
  61. <span class="pln">echo</span>
  62. <span class="kwd">if</span><span class="pun">[</span><span class="str">"$SUBJECT"</span><span class="pun">==</span><span class="str">""</span><span class="pun">]</span>
  63. <span class="kwd">then</span>
  64. <span class="pln">echo </span><span class="str">"Proceeding without the subject..."</span>
  65. <span class="kwd">fi</span>
  66. <span class="com"># 读取作为附件的文件名</span>
  67. <span class="pln">echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"Provide the list of attachments. Separate names by space.</span>
  68. <span class="str">If there are spaces in file name, quote file name with \"."</span>
  69. <span class="pln">read att</span>
  70. <span class="pln">echo</span>
  71. <span class="com"># 确保文件名指向真实文件</span>
  72. <span class="pln">attachments</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">check_files </span><span class="str">"$att"</span><span class="pun">)</span>
  73. <span class="pln">echo </span><span class="str">"Attachments: $attachments"</span>
  74. <span class="kwd">for</span><span class="pln"> attachment </span><span class="kwd">in</span><span class="pln"> $attachments</span>
  75. <span class="kwd">do</span>
  76. <span class="pln">ATTACHMENTS</span><span class="pun">=</span><span class="str">"$ATTACHMENTS-a $attachment "</span>
  77. <span class="kwd">done</span>
  78. <span class="pln">echo</span>
  79. <span class="com"># 读取完整的邮件正文</span>
  80. <span class="pln">echo </span><span class="str">"Enter message. To mark the end of message type ;; in new line."</span>
  81. <span class="pln">read line</span>
  82. <span class="kwd">while</span><span class="pun">[</span><span class="str">"$line"</span><span class="pun">!=</span><span class="str">";;"</span><span class="pun">]</span>
  83. <span class="kwd">do</span>
  84. <span class="pln">BODY</span><span class="pun">=</span><span class="str">"$BODY$line\n"</span>
  85. <span class="pln">read line</span>
  86. <span class="kwd">done</span>
  87. <span class="pln">SENDMAILCMD</span><span class="pun">=</span><span class="str">"mutt -e \"set from=$FROM\" -s \"$SUBJECT\" \</span>
  88. <span class="str">$ATTACHMENTS -- \"$TO\" <<< \"$BODY\""</span>
  89. <span class="pln">echo $SENDMAILCMD</span>
  90. <span class="pln">mutt </span><span class="pun">-</span><span class="pln">e </span><span class="str">"set from=$FROM"</span><span class="pun">-</span><span class="pln">s </span><span class="str">"$SUBJECT"</span><span class="pln"> $ATTACHMENTS </span><span class="pun">--</span><span class="pln"> $TO </span><span class="pun"><<<</span><span class="pln"> $BODY</span>

** 脚本输出 **

  1. <span class="pln">$ bash send_mail</span><span class="pun">.</span><span class="pln">sh</span>
  2. <span class="pun">*********************</span>
  3. <span class="pln">E</span><span class="pun">-</span><span class="pln">mail sending script</span><span class="pun">.</span>
  4. <span class="pun">*********************</span>
  5. <span class="typ">Enter</span><span class="pln"> the e</span><span class="pun">-</span><span class="pln">mail address you wish to send mail </span><span class="kwd">from</span><span class="pun">:</span>
  6. <span class="pun">[</span><span class="typ">Enter</span><span class="pun">]</span><span class="pln"> test@gmail</span><span class="pun">.</span><span class="pln">com</span>
  7. <span class="typ">Enter</span><span class="pln"> the e</span><span class="pun">-</span><span class="pln">mail address you wish to send mail to</span><span class="pun">:</span>
  8. <span class="pun">[</span><span class="typ">Enter</span><span class="pun">]</span><span class="pln"> test@gmail</span><span class="pun">.</span><span class="pln">com</span>
  9. <span class="typ">Enter</span><span class="pln"> e</span><span class="pun">-</span><span class="pln">mail subject</span><span class="pun">:</span>
  10. <span class="pun">[</span><span class="typ">Enter</span><span class="pun">]</span><span class="typ">Message</span><span class="pln"> subject</span>
  11. <span class="typ">Provide</span><span class="pln"> the list of attachments</span><span class="pun">.</span><span class="typ">Separate</span><span class="pln"> names </span><span class="kwd">by</span><span class="pln"> space</span><span class="pun">.</span>
  12. <span class="typ">If</span><span class="pln"> there are spaces </span><span class="kwd">in</span><span class="pln"> file name</span><span class="pun">,</span><span class="pln"> quote file name </span><span class="kwd">with</span><span class="str">".</span>
  13. <span class="str">send_mail.sh</span>
  14. <span class="str">Attachments: send_mail.sh</span>
  15. <span class="str">Enter message. To mark the end of message type ;; in new line.</span>
  16. <span class="str">This is a message</span>
  17. <span class="str">text</span>
  18. <span class="str">;;</span>

 

总结

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


via: http://linoxide.com/linux-shell-script/send-email-subject-body-attachment-linux/

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

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

来源:http://linux.cn/article-5502-1.html

相关推荐