用 smtpmail.el 发送邮件

启动一个本地 SMTP Server,为了测试:

~ $ python3 -m smtpd -c DebuggingServer -n localhost:1025

发送邮件:

(require 'smtpmail)

(with-temp-buffer
  (insert "To: [email protected]\n"
          "From: [email protected]\n"
          "\n"
          "GOOD NIGHT.\n")
  (let ((smtpmail-smtp-server "localhost")
        (smtpmail-smtp-service 1025)
        (smtpmail-stream-type nil))
    (smtpmail-via-smtp '("[email protected]") (current-buffer))))

本地 SMTP Server 会显示发送成功:

~ $ python3 -m smtpd -c DebuggingServer -n localhost:1025
---------- MESSAGE FOLLOWS ----------
b'To: [email protected]'
b'From: [email protected]'
b'X-Peer: ::1'
b''
b'GOOD NIGHT.'
------------ END MESSAGE ------------

用一个真实的 SMTP Server。由于 SMTP 的密码貌似必须写到 ~/.authinfo 中(比如可惜):

machine smtp.fastmail.com login [email protected] password xxxxxxxxx

发送邮件:

(let ((smtpmail-smtp-server "smtp.fastmail.com")
      (smtpmail-smtp-service 465)
      (smtpmail-stream-type 'ssl))
  (with-temp-buffer
    (insert "To: [email protected]\n"
            "From: [email protected]\n"
            "\n"
            "GOOD NIGHT.\n")
    (smtpmail-via-smtp '("[email protected]") (current-buffer))))

检查邮箱,发送成功。

4 个赞

所以这是准备要把邮件客户端完全移到emacs里的节奏?

平时从 Emacs 中发邮件(比如 M-x report-emacs-bug)就用的是 smtpmail.el,我主要是想从编程的角度来试试这个库,而不是用户(M-x compose-mail)的角度。