sendmail是linux系統中一個郵箱系統,在系統中配置好sendmail就可以直接使用它來發送郵箱。
sendmail的配置文件
/etc/mail/sendmail.cf :Sendmail的主配置文件; /etc/mail/access :中繼訪問控制; /etc/mail/domaintable ;域名映射; /etc/mail/local-host-names ;本地主機別名; /etc/mail/mailertable :為特定的域指定特殊的路由規則; /etc/mail/virtusertable :虛擬域配置。
中繼的配置:是指一台服務器接受並傳遞源地址和目的地址都不是本服務器的郵件。
在兩個文件中進行設置:
/etc/mail/relay-domains
/etc/mail/access。
一、安裝軟件
[root@VM_0_10_centos ~]# yum -y install sendmail sendmail-cf
啟動saslauthd服務進行SMTP驗證(默認是安裝的,如果沒有,就手動安裝)
[root@VM_0_10_centos ~]# systemctl restart saslauthd
二、郵件服務配置
1)需關閉防火牆
[root@VM_0_10_centos ~]# systemctl stopfirewalld [root@VM_0_10_centos ~]# systemctl status firewalld
2)配置Senmail的SMTP認證
將下面兩行內容前面的dnl去掉。在sendmail文件中,dnl表示該行為注釋行,是無效的,因此通過去除行首的dnl字符串可以開啟相應的設置行。
[root@VM_0_10_centos ~]# vim /etc/mail/sendmail.mc TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
3) 設置Sendmail服務的網絡訪問權限(如果是直接本機調用,可以不用操作,采用默認的127.0.0.1。不過最后還是改成0.0.0.0)
將127.0.0.1改為0.0.0.0,意思是任何主機都可以訪問Sendmail服務。
如果僅讓某一個網段能夠訪問到Sendmail服務,將127.0.0.1改為形如192.168.1.0/24的一個特定網段地址。
[root@VM_0_10_centos ~]# vim /etc/mail/sendmail.mc DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl
4)生成配置文件
Sendmail的配置文件由m4來生成,m4工具在sendmail-cf包中。如果系統無法識別m4命令,說明sendmail-cf軟件包沒有安裝
# 修改之前先備份 [root@VM_0_10_centos ~]# cp -r /etc/mail/sendmail.cf /etc/mail/sendmail.cf.bak [root@VM_0_10_centos ~]# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
5)啟動服務(如果發現sendmail dead but subsys locked,那就執行"service postfix status"查看postfix是否默認開啟了,如果開啟的話,就關閉postfix,然后再啟動或重啟sendmail服務即可。)
[root@VM_0_10_centos ~]# systemctl restart sendmail [root@VM_0_10_centos ~]# systemctl restart saslauthd [root@VM_0_10_centos ~]# systemctl status postfix(如果是開啟的,將status改為stop即可)
將服務加入到開機自啟
[root@VM_0_10_centos ~]# systemctl enable sendmail.service [root@VM_0_10_centos ~]# systemctl enable saslauthd.service Created symlink from /etc/systemd/system/multi-user.target.wants/saslauthd.service to /usr/lib/systemd/system/saslauthd.se rvice. [root@VM_0_10_centos ~]# systemctl list-unit-files | grep sendmail sendmail.service enabled [root@VM_0_10_centos ~]# systemctl list-unit-files | grep saslauthd saslauthd.service enabled
三、測試發送郵箱
1)第一種方式:安裝sendmail即可使用。
# 默認好像已經安裝 [root@VM_0_10_centos ~]# yum -y install mailx
PS:創建一個郵件內容文件,然后發郵件(注意-s參數后的郵件標題要用單引號,不能使用雙引號,否則發郵件會失敗!)
[root@VM_0_10_centos ~]# echo 'This is test mail' > /root/testmail.txt [root@VM_0_10_centos ~]# cat /root/testmail.txt This is test mail [root@VM_0_10_centos ~]# mail -s 'Test Mail' qqMail@qq.com < /root/testmail.txt
PS:我這邊使用的是qq.com,發送郵件並未接收到,可能是需要在qq郵箱開啟授權碼。然后在服務器上進行配置才行,操作如下
先開啟授權碼:
往下滑動,開啟簽名兩個服務表示已經開啟了smtp功能。點擊生 成授權碼 按照提示繼續操作就可以獲取授權碼了
發送之后點擊我已發送,生成授權碼即可。
編輯配置文件:
[root@VM_0_10_centos ~]# vi /etc/mail.rc set from=qqMail@qq.com # 對方收到郵件時顯示的發件人 set smtp=smtps://smtp.qq.com:465 # 第三方發郵件的smtp服務器地址 set smtp-auth-user=qqMail@qq.com # 第三方發郵件的用戶名 set smtp-auth-password=*********** # 授權碼 set ssl-verify=ignore set nss-config-dir=/root/.certs/ set smtp-auth=login # SMTP的認證方式
# set smtp-use-starttls # 如果配置這個會報錯SSL/TLS handshake failed: SSL received a malformed Alert record
加入上面配置保存以后,重啟郵件服務:
# systemctl restart sendmail # systemctl restart saslauthd
運行以下命令測試發送郵件,如果沒有意外,我們的郵箱很快就能收到從服務器發過來的測試郵件了。
在這里報錯:
# 創建/root/.certs目錄 # mkdir -p /root/.certs [root@VM_0_10_centos .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/, /-END CERTIFICATE-/p' >qq.crt
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA verify return:1 depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = Secure Site CA G2 verify return:1 depth=0 C = CN, ST = Guangdong, L = Shenzhen, O = Tencent Technology (Shenzhen) Company Limited, OU = \E4\BC\81\E4\B8\9A\E 5\BE\AE\E4\BF\A1\E4\BA\A7\E5\93\81\E9\83\A8, CN = *.exmail.qq.comverify return:1 DONE [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs/ -i qq.crt [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs/ -i qq.crt [root@VM_0_10_centos .certs]# certutil -L -d /root/.certs/ Certificate Nickname Trust Attributes SSL,S/MIME,JAR/XPI GeoTrust SSL CA C,, [root@VM_0_10_centos .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt Notice: Trust flag u is set automatically if the private key is present.
這樣配置還是沒有解決,查看日志/var/log/mailog(可能是主機沒有解析到的原因)
解決:設置/etc/hosts添加如下內容,然后保存退出,重啟服務
好吧,還是沒解決
后面按照這個博客的試了下,也出現了問題https://blog.csdn.net/qq_43277505/article/details/85337072
意思是:來自地址的501郵件必須與授權用戶相同,查看mail.rc配配置文件發現是from后面的qq.com寫成了163.com
繞了這么大圈終於解決了,開森
這里是直接輸入,然后結合管道符發送,還可以從文件讀取(-s表示主題)
# mail -s 'Test Mail' **@qq.com < /root/testmail.txt
[root@VM_0_10_centos .certs]# echo "測試阿里雲465smtp端口發信是否ok!,如果收到了此封郵件則說明ok了!" | mail -v -s 'test ' **@qq.comResolving host smtp.qq.com . . . done. Connecting to 14.18.245.164:465 . . . connected. Comparing DNS name: "pop.qq.com" Comparing DNS name: "dav.qq.com" Comparing DNS name: "ex.qq.com" Comparing DNS name: "smtp.qq.com" SSL parameters: cipher=AES-128, keysize=128, secretkeysize=128, issuer=CN=Secure Site CA G2,OU=www.digicert.com,O=DigiCert Inc,C=US subject=CN=pop.qq.com,OU=R&D,O=Tencent Technology (Shenzhen) Company Limited,L=Shenzhen,ST=Guangdong,C=CN 220 smtp.qq.com Esmtp QQ Mail Server >>> EHLO VM_0_10_centos 250-smtp.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME >>> AUTH LOGIN 334 VXNlcm5hbWU6 >>> MjM2MDQxNTg3MUBxcS5jb20= 334 UGFzc3dvcmQ6 >>> ZndkY29va2JvdWV5ZWJhaQ== 235 Authentication successful >>> MAIL FROM:<**@qq.com> 250 Ok >>> RCPT TO:<**@qq.com> 250 Ok >>> DATA 354 End data with <CR><LF>.<CR><LF> >>> . 250 Ok: queued as >>> QUIT 221 Bye
如果是發送給多個郵件,就使用-c參數,如下:
# echo "This is test mail" | mail -s 'test' -c **@qq.com **@163.com
參考博客:
https://www.cnblogs.com/kevingrace/p/6143977.html
https://blog.csdn.net/qq_43277505/article/details/85337072(推薦)