关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » HTML5 »发送电子邮件的4个Linux命令行工具

    发送电子邮件的4个Linux命令行工具

    2015-05-25 00:00:00 出处:linux.cn
    分享

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

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

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

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

    在 Debian / Ubuntu 系统

    apt-get install mutt
    apt-get install swaks
    apt-get install mailx
    apt-get install sharutils

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

    yum install mutt
    yum install swaks
    yum install mailx
    yum install sharutils

    1) 使用 mail / mailx

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

    $ man mail

    第一行看起来是这样的:

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

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

    a) 简单的邮件

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

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

    $ mail user@example.com
    
    HI,
    Good Morning
    How are you
    EOT

    b) 发送有主题的邮件

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

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

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

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

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

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

    e) 发送带附件的邮件

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

    -a选项用于指定附件。

    2) mutt

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

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

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

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

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

    c) 发送带附件的邮件

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

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

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

    3) swaks

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

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

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

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

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

    4) uuencode

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

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

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

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

    #!/bin/bash
    
    FROM=""
    SUBJECT=""
    ATTACHMENTS=""
    TO=""
    BODY=""
    
    # 检查文件名对应的文件是否存在
    function check_files()
    {
    output_files=""
    for file in $1
    do
    if [ -s $file ]
    then
    output_files="${output_files}${file} "
    fi
    done
    echo $output_files
    }
    
    echo "*********************"
    echo "E-mail sending script."
    echo "*********************"
    echo
    
    # 读取用户输入的邮件地址
    while [ 1 ]
    do
    if [ ! $FROM ]
    then
    echo -n -e "Enter the e-mail address you wish to send mail from:/n[Enter] "
    else
    echo -n -e "The address you provided is not valid:/n[Enter] "
    fi
    
    read FROM
    echo $FROM | grep -E '^.+@.+$' > /dev/null
    if [ $  -eq 0 ]
    then
    break
    fi
    done
    
    echo
    
    # 读取用户输入的收件人地址
    while [ 1 ]
    do
    if [ ! $TO ]
    then
    echo -n -e "Enter the e-mail address you wish to send mail to:/n[Enter] "
    else
    echo -n -e "The address you provided is not valid:/n[Enter] "
    fi
    
    read TO
    echo $TO | grep -E '^.+@.+$' > /dev/null
    if [ $  -eq 0 ]
    then
    break
    fi
    done
    
    echo
    
    # 读取用户输入的邮件主题
    echo -n -e "Enter e-mail subject:/n[Enter] "
    read SUBJECT
    
    echo
    
    if [ "$SUBJECT" == "" ]
    then
    echo "Proceeding without the subject..."
    fi
    
    # 读取作为附件的文件名
    echo -e "Provide the list of attachments. Separate names by space.
    If there are spaces in file name, quote file name with /"."
    read att
    
    echo
    
    # 确保文件名指向真实文件
    attachments=$(check_files "$att")
    echo "Attachments: $attachments"
    
    for attachment in $attachments
    do
    ATTACHMENTS="$ATTACHMENTS-a $attachment "
    done
    
    echo
    
    # 读取完整的邮件正文
    echo "Enter message. To mark the end of message type ;; in new line."
    read line
    
    while [ "$line" != ";;" ]
    do
    BODY="$BODY$line/n"
    read line
    done
    
    SENDMAILCMD="mutt -e /"set from=$FROM/" -s /"$SUBJECT/" /
    $ATTACHMENTS -- /"$TO/" <<< /"$BODY/""
    echo $SENDMAILCMD
    
    mutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- $TO <<< $BODY

    ** 脚本输出 **

    $ bash send_mail.sh
    *********************
    E-mail sending script.
    *********************
    
    Enter the e-mail address you wish to send mail from:
    [Enter] test@gmail.com
    
    Enter the e-mail address you wish to send mail to:
    [Enter] test@gmail.com
    
    Enter e-mail subject:
    [Enter] Message subject
    
    Provide the list of attachments. Separate names by space.
    If there are spaces in file name, quote file name with ".
    send_mail.sh
    
    Attachments: send_mail.sh
    
    Enter message. To mark the end of message type ;; in new line.
    This is a message
    text
    ;;

    总结

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

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    Edge浏览器百度被劫持/篡改怎么办,地址后边跟着尾巴#tn=68018901_7_oem_dg

    Google Chrome 在 iPhone 上新增了 Safari 数据导入选项

    Windows 11专业版 KMS工具激活产品密钥的方法

    DEDECMS安全策略官方出品

    Microsoft Text Input Application 可以关闭吗?

    新版本QQ如何关闭自带的浏览器?

    C++编程语言中continue的用法和功能,附举例示范代码

    c++ map 的数据结构、基本操作以及其在实际应用中的使用。

    C语言如何避免内存泄漏、缓冲区溢出、空指针解引用等常见的安全问题

    C语言中的break语句详解

    IT头条

    马斯克2026最新采访总结:2040年,全球机器人数量将突破100亿台

    23:52

    专家解读|规范人工智能前沿业态健康发展的新探索:解读《人工智能拟人化互动服务管理暂行办法》

    00:54

    用至强 6高存力搞定MoE卸载!

    17:53

    美国将允许英伟达向中国“经批准的客户”出售H200 GPU

    02:08

    苹果与微信就15%手续费达成一致?腾讯未置可否

    22:00

    技术热点

    PHP 和 Node.js 的10项对比挑战

    Javascript闭包深入解析及实现方法

    windows 7、windows 8.1手动增加右键菜单功能技巧

    MYSQL出错代码大汇总

    windows 7假死机怎么办 windows 7系统假死机的原因以及解决方法

    Ubuntu(Linux)下配置IP地址的方法

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。