LINUX下通過外部SMTP發郵件

方式一

修改/etc/mail.rc,增加一下類似N行,指定外部的smtp服務器地址、帳號密碼等。

# vi /etc/mail.rc  
set [email protected]
set smtp=smtp.qq.com  
set smtp-auth-user=123456
set smtp-auth-password=sbsbsbsb
set smtp-auth=login  

然後通過命令來發送郵件

echo hello word | mail -s " title" [email protected]

方式二

安裝Postfix :

yum install postfix mail cyrus-sasl-* -y

配置Postfix:

vi /etc/postfix/main.cf

(Postfix主要配置文件,再其末尾添加以下配置)

#指定默認的郵件發送服務器
relayhost = [smtp.163.com]:25
#激活sasl認證
smtp_sasl_auth_enable = yes
#指定sasl密碼配置文件
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
#非匿名登錄
smtp_sasl_security_options = noanonymous
#指定認證類型 (提示:需要yum安裝cyrus-sasl-*組件,否則發郵件時會報錯:no mechanism available)
smtp_sasl_type = cyrus
#linux用戶與發件人的對應關係配置文件
sender_canonical_maps = hash:/etc/postfix/sender_canonical 

vi /etc/postfix/sasl_passwd

(郵箱賬號和密碼文件,每行一個。 創建好後需要使用postmap命令使配置文件生效)

[smtp.163.com]:25 [email protected]:my163Password

postmap /etc/postfix/sasl_passwd

vi /etc/postfix/sender_canonical

(linux用戶和發件人對應關係,每行一個)

root [email protected]

postmap /etc/postfix/sender_canonical

重啓Postfix:

service postfix restart

嘗試發送郵件:

echo "hello world" |mail -s test [email protected]

(可以用 mailq 命令查看發送隊列,清空mailq隊列 : postsuper -d ALL )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章