Mac OS X 下用命令行發送郵件

msmtp 是個 smtp 客戶端。若是隻是用命令行發送郵件,並不須要能接受郵件,用它正好。用 Sendmail、Postfix 的話,就有些殺雞用牛刀的感受,並且它們配置起來真很讓人痛苦。php

安裝 msmtp

$ brew install msmtp # 若是沒有 brew,請去 http://brew.sh/ 安裝下linux

配置

~/.mailrcshell

<!-- lang: shell -->
set sendmail=/usr/local/bin/msmtp

~/.msmtprcwindows

<!-- lang: shell -->
# Use an external SMTP server with insecure authentication.
# (manually choose an insecure authentication method.)
# Note that the password contains blanks.

defaults

####################################################################
# A sample configuration using Gmail
####################################################################

# account name is "gmail".
# You can select this account by using "-a gmail" in your command line.
account gmail
host smtp.gmail.com
tls on
tls_certcheck off
port 587
auth login
from yourname@gmail.com
user yourname
password yourpassword

# If you don't use any "-a" parameter in your command line,
# the default account "gmail" will be used.
account default: gmail

~/.msmtprc 須要設置正確的訪問權限安全

<!-- lang: shell -->
$ chmod 600 ~/.msmtprc

試用

<!-- lang: shell -->
$ echo "Hello world" | mail -s "msmtp test at `date`" yourfriend®@gmail.com

還想說的

上面的 ~/.msmtprc 使用明文存放密碼、非加密方式驗證,雖方便但不安全。想要更安全,可參看 Dropping sendmail-postfix for msmtpapp

若是想在 Linux、Windows 上作一樣的事情,可參看How to send emails with msmtp on Windows or Linux or Mac OS Xpost

文章還參考了

How to send an email from command line?ui

MSMPTQuickStartthis

Archlinux msmtpgoogle

相關文章
相關標籤/搜索