標籤:postfix gmail
起因:
[[email protected] ~]# echo 123|mail -s "123" [email protected]
[[email protected] ~]# smtp-server: 530 5.7.0 Must issue a STARTTLS command first. 71sm664699pfs.63 - gsmtp
"/root/dead.letter" 11/317
. . . message not sent.
之前一直使用的qq郵箱給自己發送郵件,做資料備份用,今天想把qq郵箱換成gmail郵箱,結果出現了上面的錯誤,網上尋找了下資料終於得以解決,但中途坑比較多,總結一下方便日後遇坑。
為方便查看,用紅色表示輸入的命令,藍色表示輸入的內容
1、在/etc/postfix/main.cf檔案末尾添加如下欄位。
vi /etc/postfix/main.cf
smtp_sasl_security_options = noanonymous
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl/passwd
2、配置SASL認證
建立passwd檔案並添加帳號密碼資訊
mkdir -p /etc/postfix/sasl
vi /etc/postfix/sasl/passwd
[smtp.gmail.com]:587 你的郵箱:應用專用密碼
注意郵箱帳號和密碼之間的“:",不要漏掉。例如:我公司購買的google郵箱服務,我產生的密碼是123qwe則配置[smtp.gmail.com]:587 [email protected]:123qwe
由於google的安全限制,使用非web方式使用需要設定【應用專用密碼】
應用專用密碼產生網站:https://security.google.com/settings/security/apppasswords
3、變更檔許可權
chmod 600 /etc/postfix/sasl/passwd
4、建立尋找表
postmap /etc/postfix/sasl/passwd
5、產生CA認證
cd /etc/pki/tls/certs
make 你的名字.pem #例如:我叫小明,則 make xiaoming.pem
umask 77 ; \
PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \
/usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \
cat $PEM1 > xiaoming.pem ; \
echo "" >> xiaoming.pem ; \
cat $PEM2 >> xiaoming.pem ; \
rm -f $PEM1 $PEM2
Generating a 2048 bit RSA private key
....................................+++
....................................................................+++
writing new private key to ‘/tmp/openssl.gHSN9F‘
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.‘, the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:gongsi
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server‘s hostname) []:xiaoming
Email Address []:[email protected]
cp /etc/pki/tls/certs/zhaoxinxing.pem /etc/postfix/cacert.pem
7、重啟postfix服務
/etc/init.d/postfix restart
8、測試、完成
把/etc/hosts檔案作為附件發送到自己的gmail郵箱。
echo “mail test”|mail -a /etc/hosts -s "hosts" [email protected]
本文出自 “meiyou” 部落格,請務必保留此出處http://324703.blog.51cto.com/314703/1732878
CentOS下通過postfix使用自己的gmail郵箱發送郵件