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

    参考:
            http://www.cnblogs.com/refrag/archive/2012/11/28/2793533.html     

          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 ,root 用户编辑/etc/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 , root 用户编辑/etc/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 发送了。

    测试下

     //带附件
     # echo "邮件内容123456" | mutt -s "邮件标题测试邮件" -a /scripts/test.txt  test@126.com
    
    //不代附件
      # echo "邮件内容123456" | mutt -s "邮件标题测试邮件"  test@126.com

    小结

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

    对于 mutt,还有很多需要配置,比如对多个邮件帐号的支持、分类文件夹等,这些会在后面的使用过程中逐渐完善。

  • 相关阅读:
    JQ库函数记忆要点
    PHP运算符:算数运算符、逻辑运算符、三目运算符、位运算符、字符串运算符。
    ThinkPHP函数详解:F方法(快速缓存方法)
    AndroidStudio项目提交(更新)到github最详细步骤
    git项目管理及fatal: remote origin already exists.解决方法
    git项目管理及fatal: remote origin already exists.解决方法
    git分支(存在意义和使用方法)
    RecyclerView
    Git Windows版本
    chmod 777 -R on existing path getting chmod: -R: No such ...
  • 原文地址:https://www.cnblogs.com/likwo/p/3160923.html
Copyright © 2011-2022 走看看