用 Emacs 从命令行发邮件

2017-06-01

命令行下能发邮件的工具不少,Emacs 自带 SMTP 支持,在命令行下也能发邮件:

$ df -h | emacs -batch -l mail.el --eval '(send-mail "mail@xuchunyang.me" "磁盘使用率报告")'
Parsing /Users/xcy/.mailrc...
Parsing /Users/xcy/.mailrc... done
Mark set
Mark set
Sending...
Mark set
Mark set
Sending via mail...
Opening STARTTLS connection to ‘smtp.exmail.qq.com:25’...
Opening STARTTLS connection to ‘smtp.exmail.qq.com:25’...done
Sending email 
Sending email done
Sending...done

新建文件 mail.el 并在其中设置名字、邮箱地址以及 SMTP 地址。函数 send-mail 从参数表接受收件人地址和邮件主题、从 STDIN 获得邮件正文:

(setq user-full-name       "Chunyang Xu"
      user-mail-address    "mail@xuchunyang.me"
      smtpmail-smtp-server "smtp.exmail.qq.com"
      send-mail-function   'smtpmail-send-it)

(defun send-mail (to subject)
  (compose-mail to subject)
  (insert-file-contents "/dev/stdin")
  (message-send))

SMTP 密码放在 ~/.authinfo~/.authinfo.gpg 中:

machine smtp.exmail.qq.com login mail@xuchunyang.me password NOSECRET

Tags:

加载 Disqus 评论