zoukankan      html  css  js  c++  java
  • 在命令行上 Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件

    在命令行写email from ubuntu

    参考:
         http://www.habadog.com/2011/11/23/send-mail-with-msmtp-mutt-linux
         http://www.absolutelytech.com/2010/07/17/howto-configure-msmtp-to-work-with-gmail-on-linux
    msmtp 是一款专门负责邮件发送的客户端软件,基于GPL发布,支持TLS/SSL、DNS模式、IPv6、服务器端认证、多用户等特性。
    其主页是 msmtp.sourceforge.net:
         Msmtp is an SMTP client with a sendmail compatible interface ...
    安装
         sudo apt-get install mutt msmtp

    如需要对TLS/SSL的支持,还需要安装 GnuTLS 或者 OpenSSL,官方推荐 GnuTLS (https://help.ubuntu.com/community/GnuTLS):
         sudo apt-get install gnutls-bin

    另外,使用 Gmail 需要安装 ca-certificates:
         sudo apt-get install ca-certificates
    msmtp 有 3 中工作模式:
         Sendmail mode (default)
         Server information mode
         Remote Message Queue Starting mode
    对于发送邮件来说,使用 Sendmail mode (default) 就可以了:
          In  the  default sendmail mode, msmtp reads a mail from standard input and sends it to an SMTP server for delivery.

    配置 msmtp
    安装完后,编辑 msmtp 配置文件 ~/.msmtprc

    复制代码

        defaults
        tls on
        tls_starttls on
        tls_trust_file /etc/ssl/certs/ca-certificates.crt
     
        account default
        host smtp.gmail.com
        port 587
        auth on
        user username@gmail.com
        password mypass
        from username@gmail.com
        logfile ~/msmtp.log

    复制代码

    把 username@gmail.com 和 mypass 替换成实际的邮箱和密码就可以了,这里密码是明文。注意,发送邮件默认是 25 端口,但是 Gmail 邮件服务商用的不是 25 端口,而是 465 或 587 端口。如果不设置端口,发送邮件会报错 "Must issue a STARTTLS command first"。logfile 如果设置的时候要注意设置访问权限,保证当前用户可写。

    保存后,修改访问权限
         chmod 0600 .msmtprc
    如果不这样,会报错:
         must have no more than user read/write permissions

    测试发送邮件:
         msmtp XXX@example.com
    输入任意字符,然后按Ctrl+D退出,查看邮件是否收到。可以查看 logfile 里面的发送日志记录。
    配置 mutt
    查看 msmtp 安装路径
         $ which msmtp
         /usr/bin/msmtp
    编辑 mutt 配置文件 ~/.muttrc

    复制代码

        set sendmail="/path/to/msmtp"
        set use_from=yes
        set realname="Your Name"
        set from=you@example.com
        set envelope_from=yes
        set editor="vim"

    复制代码

    后面就可以用 mutt 写邮件后通过 msmtp 发送了。

    小结

    对于 msmtp 的详细介绍,可以参考 http://msmtp.sourceforge.net/documentation.html 或者 man msmtp。
    文档里面提供了配置示例,包括 msmtp 配合 mutt 的配置。

  • 相关阅读:
    [SQLSERVER2005 ERROR]"附加数据库 对于 服务器“GNPC”失败"
    家用视频监控设备
    Layer Two Tunneling Protocol "L2TP"
    [转]char类型和string类型(C++,C#)
    Layer 2 Tunneling Protocol
    坚持坚持。。。
    转载几篇有用的文章
    常用的SQL语句(转载)
    python判断一个字符是否是xml合法字符
    搜索引擎推荐
  • 原文地址:https://www.cnblogs.com/chjbbs/p/6389261.html
Copyright © 2011-2022 走看看