Ubuntu 下使用 mutt 和 msmtp 發送 Gmail 郵件


參考:
      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 的配置。

對於 mutt,還有很多需要配置,比如對多個郵件帳號的支持、分類文件夾等,這些會在后面的使用過程中逐漸完善。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM