广告

本站里的文章大部分经过自行整理与测试

2017年1月2日星期一

Linux - ssmtp - 用 GMAIL SMTP 送信

1. 安装

$ su

Ubuntu
$ apt install ssmtp mailutils mpack mutt

Fedora
$ dnf install ssmtp mailx mutt

CentOS
$ yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ yum install ssmtp mailx mutt

* mpack / mailx / mutt 可选择只安装一个

2. 设置 - SSMTP

$ gedit /etc/ssmtp/ssmtp.conf

root=user@gmail.com
mailhub=smtp.gmail.com:587
hostname=ubuntu1610
FromLineOverride=YES
AuthUser=user@gmail.com
AuthPass=password
UseSTARTTLS=YES
UseTLS=YES

CentOS
$ alternatives --config mta

选 /usr/sbin/sendmail.ssmtp

3. 设置 - GOOGLE

i) Settings - Forwarding and POP/IMAP - IMAP Access - Enable IMAP
https://mail.google.com/mail/u/0/#settings/fwdandpop

ii) 修改 AuthPass=password
Sign-in & security - Signing in to Google - 2-Step Verification - ON
Sign-in & security - Signing in to Google - App passwords
修改以上 AuthPass=App passwords

Sign-in & security - Signing in to Google - 2-Step Verification - Off
Sign-in & security - Connected apps & sites - Allow less secure apps: ON
修改以上 AuthPass=Gmail passwords

https://myaccount.google.com/security?utm_source=OGB#signin

4. 测试

$ echo "This is a test" | ssmtp recipient@yahoo.com

$ ssmtp recipient@yahoo.com < /home/user/content.txt

$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com
$ echo "This is a test" | mail -s "Subject" recipient@yahoo.com -A /home/user/content.txt

$ echo "This is a test" | mailx -s "Subject" recipient@yahoo.com -a /home/user/content.txt
$ echo "This is a test" | mutt -s "Subject" recipient@yahoo.com -a /home/user/content.txt

$ mpack -s "Test File" /home/user/content.txt recipient@yahoo.com

5. 增强安全设置

$ groupadd ssmtp
$ chown :ssmtp /etc/ssmtp/ssmtp.conf
$ chown :ssmtp /usr/sbin/ssmtp
$ chmod 640 /etc/ssmtp/ssmtp.conf
$ chmod g+s /usr/sbin/ssmtp

没有评论:

发表评论