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

相关文章
相关标签/搜索