Linux使用命令发送邮件

Linux下可以使用mail和sendmail命令进行邮件发送,这两个命令各有特点,下面就来看看:

  • 发送纯文本格式普通邮件
    • mail命令
      mail命令非系统自带,需安装:

      [[email protected] ~]# yum install -y mailx

      命令格式:

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

      常用参数有: -s 指定邮件标题;-a 指定邮件附件;-r 指定发件人邮箱地址。
      邮件内容可以将文件内容重定向进来,也可以使用前一个命令的输出值,例如:

      [[email protected] ~]# echo "This is Test Mail 1" > mailtext
      [[email protected] ~]# mail -s "Test Mail 1" -r [email protected] [email protected] < mailtext
      [[email protected] ~]# echo "This is Test Mail 2" | mail -s "Test Mail 2" -r [email protected] [email protected]

      然后我们就可以看到这两封邮件:

    • sendmail命令
      sendmail命令需要建立发送邮件的格式文件:

      [[email protected] ~]# cat > mailto << EOF 
      From: nobody <[email protected]>
      To: Master <[email protected]>
      Subject: Sendmail test
      Content-Type: text/plain; charset=utf-8
      
      This is Mail body
      EOF

      发送邮件:

      [[email protected] ~]# sendmail -t < mailto

      查看:

  • 发送带HTML格式邮件:
    • mail命令:
      mail命令发送的邮件Content-Type固定是文本格式的,但可以通过在标题处做点小动作来添加一个新的Content-Type,使得部分邮件客户端(如foxmail)认为这是一封HTML格式邮件:
      邮件正文:

      [[email protected] ~]# cat > mailtext << EOF
      <h3>Mail Command Html Test</h3>
      <table style="width: 100%" border="1">
      <tr bgcolor="#99ccff">
      <th style="width: 100px">Sequentia</th>
      <th>The monitored URL</th>
      <th style="width: 150px">Http Status Code</th>
      <th>Company Name/Position</th>
      </tr>
      EOF
      [[email protected] ~]# mail -s "$(echo -e "mail_html\nContent-Type: text/html; charset=utf-8")" -r [email protected] [email protected] < mailtext

      使用echo -e  \n,在邮件标题后输入了个回车,然后添加了个Content-Type。查看:

      注意:使用此方法是占用了标题位的长度,故邮件标题不可设置太长;另此方法是多添加了Content-Type,故只有部分客户端识别。可以将邮件导出,查看源本:


      使用Win10自带的客户端无法识别:

    • sendmail命令:
      使用sendmail命令则不会出现上述情况,因为可以在文件中指定Content-Type:

      [[email protected] ~]# cat > mailto << EOF                           
      From: nobody <[email protected]>
      To: Master <[email protected]>
      Subject: Sendmail HTML
      Content-Type: text/html; charset=utf-8
      
      <h3>Mail Command Html Test</h3>
      <table style="width: 100%" border="1">
      <tr bgcolor="#99ccff">
      <th style="width: 100px">Sequentia</th>
      <th>The monitored URL</th>
      <th style="width: 150px">Http Status Code</th>
      <th>Company Name/Position</th>
      </tr>
      EOF
      [[email protected] ~]# sendmail -t < mailto

      查看:


  • 发送带附件的邮件
    • mail命令:
      使用mail命令发送带附件的邮件比较简单,使用-a参数添加文件即可:

      [[email protected] ~]# tar czf att.tar.gz mailtext
      [[email protected] ~]# echo "Mail Test Attachment" | mail -s "Mail Attachment" -r [email protected] -a att.tar.gz [email protected]

    • sendmail命令:
      使用sendmail命令发送附件,需将附件转为base64内容,并添加分隔线:

      [[email protected] ~]# cat > mailto << EOF
      From: nobody <[email protected]>
      To: Master<[email protected]>
      Subject: Sendmail Attachment
      Content-Type: multipart/mixed; boundary="This_is_Cut_Line"
      Mime-Version: 1.0
      
      This is a multi-part message in MIME format.
      
      --This_is_Cut_Line
      Content-Type:text/html; charset=utf-8
      
      Sendmail TEST Attachment!
      
      --This_is_Cut_Line
      Content-Type: text/plain; charset=utf-8
      Content-Transfer-Encoding: base64
      Content-Disposition: attachment; filename="att.tar.gz"
      
      EOF
      [[email protected] ~]# base64 att.tar.gz >> mailto

      Content-Type: multipart/mixed设置邮件内容为混合模式,boundary=”This_is_Cut_Line”设置邮件不同的内容分隔线标识。
      之后不同的内容就以 “空行-分隔标识-Content-Type-空行-内容”的格式来填充即可,发送并查看:

      [[email protected] ~]# sendmail -t < mailto

总结:

  1. mail命令简单实用,如果就发普通文本邮件足已
  2. sendmail命令可以发送复杂形式的图件,且发送邮件更合乎规范

留下评论

error: Content is protected !!