參考文檔
https://mailoutgoing.com/support/mailrelay/sendmail.html
https://www.sendmail.org/~ca/email/auth.html
https://docs.oracle.com/en/operating-systems/oracle-linux/6/admin/configure-sendmail.html
總結
1、ubuntu環境下,sendmail服務位于/etc/init.d/sendmail而非/usr/lib/systemd/system/sendmail.service,它被service命令管理,不過類似這種被service命令管理的大部分服務也可以被systemctl管理
2、一般需要安裝sendmail和m4 sendmail-cf,因為sendmail發送郵件時不需要郵件賬號和密碼,但是需要在/etc/mail/sendmail.mc中配置郵件服務器地址,再通過m4 sendmail.mc >sendmail.cf命令把郵件服務器地址編輯到sendmail.cf
3、配置sendmail服務器的這臺需要發送郵件的機器,服務器名必須帶上.XX.com類似這樣的域名后綴,否則無法把郵件發出去并且會報錯DSN: Service unavailable
4、sendmail的語法可以是如下兩種方式
4.1、(echo From: "WON Notification <notification@XXX.com>";echo To: "lukes.liao@XXXchina.com";echo To: "lukes.liao1@XXXchina.com";echo Subject: "This is test1";echo "The Content is just a test email1")| /usr/sbin/sendmail -t
4.2、sendmail -t < /root/emailtest.txt
/root/emailtest.txt內容如下
From: WON Notification <notification@XXX.com>
To: lukes.liao@XXXchina.com
To: lukes.liao1@XXXchina.com
Subject: "This is test1"
The Content is just a test email1
本實驗以ubuntu22.04為例,郵件服務器mxout.XXX.com可以匿名訪問,所以不需要配置賬號名和密碼
root@FDB001:~# systemctl status sendmail
Unit sendmail.service could not be found.
root@FDB001:~#
root@FDB001:~# ll /etc/mail
ls: cannot access '/etc/mail': No such file or directory
root@FDB001:~#
root@FDB001:~# ll /usr/lib/systemd/system |grep mail
root@FDB001:~# apt-get install sendmail
root@FDB001:~#
root@FDB001:~# systemctl status sendmail
● sendmail.service - LSB: powerful, efficient, and scalable Mail Transport AgentLoaded: loaded (/etc/init.d/sendmail; generated)Active: active (running) since Tue 2024-07-02 03:22:09 UTC; 1min 21s agoDocs: man:systemd-sysv-generator(8)Tasks: 1 (limit: 9388)Memory: 2.9MCPU: 81msCGroup: /system.slice/sendmail.service└─199151 "sendmail: MTA: accepting connections" "" "" "" "" "" "" "" "" "" ""
Jul 02 03:22:07 FDB001 systemd[1]: Starting LSB: powerful, efficient, and scalable Mail Transport Agent...
Jul 02 03:22:07 FDB001 sendmail[199102]: * Starting Mail Transport Agent (MTA) sendmail
Jul 02 03:22:07 FDB001 sendmail[199122]: hostname: Temporary failure in name resolution
Jul 02 03:22:07 FDB001 sendmail[199127]: hostname: Temporary failure in name resolution
Jul 02 03:22:07 FDB001 su[199136]: (to smmsp) root on none
Jul 02 03:22:07 FDB001 su[199136]: pam_unix(su:session): session opened for user smmsp(uid=116) by (uid=0)
Jul 02 03:22:07 FDB001 su[199136]: pam_unix(su:session): session closed for user smmsp
Jul 02 03:22:07 FDB001 sm-mta[199151]: starting daemon (8.15.2): SMTP+queueing@00:10:00
Jul 02 03:22:09 FDB001 sendmail[199102]: ...done.
Jul 02 03:22:09 FDB001 systemd[1]: Started LSB: powerful, efficient, and scalable Mail Transport Agent.
root@FDB001:~#
root@FDB001:~# ll /etc/mail
total 228
drwxr-sr-x 7 smmta smmsp 4096 Jul 2 03:22 ./
drwxr-xr-x 112 root root 4096 Jul 2 03:22 ../
-rw------- 1 root root 4265 Jul 2 03:22 access
-rw-r----- 1 smmta smmsp 12288 Jul 2 03:22 access.db
-rw-r--r-- 1 root root 281 Dec 9 2021 address.resolve
lrwxrwxrwx 1 root smmsp 10 Jul 2 03:22 aliases -> ../aliases
-rw-r----- 1 smmta smmsp 12288 Jul 2 03:22 aliases.db
-rw-r--r-- 1 root root 3226 Jul 2 03:22 databases
-rw-r--r-- 1 root root 5659 Dec 9 2021 helpfile
-rw-r--r-- 1 root smmsp 11 Jul 2 03:22 local-host-names
drwxr-sr-x 2 smmta smmsp 4096 Jul 2 03:22 m4/
-rwxr-xr-- 1 root smmsp 10019 Jul 2 03:22 Makefile*
drwxr-xr-x 2 root root 4096 Jul 2 03:22 peers/
drwxr-xr-x 2 root smmsp 4096 Dec 9 2021 sasl/
-rw-r--r-- 1 root smmsp 64130 Jul 2 03:22 sendmail.cf
-rw-r--r-- 1 root root 12235 Jul 2 03:22 sendmail.conf
-rw-r--r-- 1 root smmsp 4066 Jul 2 03:22 sendmail.mc
-rw-r--r-- 1 root root 148 Dec 9 2021 service.switch
-rw-r--r-- 1 root root 179 Dec 9 2021 service.switch-nodns
drwxr-sr-x 2 smmta smmsp 4096 Jul 2 03:22 smrsh/
-rw-r--r-- 1 root smmsp 44563 Jul 2 03:22 submit.cf
-rw-r--r-- 1 root smmsp 2383 Jul 2 03:22 submit.mc
drwxr-xr-x 2 root smmsp 4096 Dec 9 2021 tls/
-rw-r--r-- 1 root smmsp 0 Jul 2 03:22 trusted-users
root@FDB001:~#
root@FDB001:~# ll /usr/lib/systemd/system |grep mail
root@FDB001:~#
root@FDB001:~# systemctl list-unit-files |grep mail
sendmail.service generated -
root@FDB001:~#
root@FDB001:~# systemctl list-dependencies sendmail --reverse
sendmail.service
● ├─graphical.target
● └─multi-user.target
● └─graphical.target
root@FDB001:~#
root@FDB001:~# service sendmail status
● sendmail.service - LSB: powerful, efficient, and scalable Mail Transport AgentLoaded: loaded (/etc/init.d/sendmail; generated)Active: active (running) since Tue 2024-07-02 03:22:09 UTC; 23min agoDocs: man:systemd-sysv-generator(8)Tasks: 1 (limit: 9388)Memory: 3.6MCPU: 147msCGroup: /system.slice/sendmail.service└─199151 "sendmail: MTA: accepting connections" "" "" "" "" "" "" "" "" "" ""
Jul 02 03:22:07 FDB001 su[199136]: (to smmsp) root on none
Jul 02 03:22:07 FDB001 su[199136]: pam_unix(su:session): session opened for user smmsp(uid=116)>
Jul 02 03:22:07 FDB001 su[199136]: pam_unix(su:session): session closed for user smmsp
Jul 02 03:22:07 FDB001 sm-mta[199151]: starting daemon (8.15.2): SMTP+queueing@00:10:00
Jul 02 03:22:09 FDB001 sendmail[199102]: ...done.
Jul 02 03:22:09 FDB001 systemd[1]: Started LSB: powerful, efficient, and scalable Mail Transpor>
Jul 02 03:37:17 FDB001 sm-mta[200089]: 4623bHbK200089: from=<root@FDB001>, size=452, class>
Jul 02 03:37:18 FDB001 sm-mta[200090]: 4623bHbK200089: to=<root@FDB001>, ctladdr=<root@FRS>
Jul 02 03:37:18 FDB001 sm-mta[200092]: 4623bI5I200092: from=<root@FDB001>, size=448, class>
Jul 02 03:37:18 FDB001 sm-mta[200093]: 4623bI5I200092: to=<root@FDB001>, ctladdr=<root@FRS>
root@FDB001:~#
root@FDB001:~# (echo From: "WON Notification <notification@XXX.com>";echo To: "lukes.liao@XXXchina.com";echo Subject: "This is test1";echo "The Content is just a test email1")| /usr/sbin/sendmail -t
--還是收不到郵件,且service sendmail status看不到任何報錯信息
root@FDB001:~#
root@FDB001:~# apt-get install m4 sendmail-cf
root@FDB001:~# grep SMART_HOST /etc/mail/sendmail.mc
define(`SMART_HOST', `mxout.XXX.com')
root@FDB001:~# cd /etc/mail
root@FDB001:~# m4 sendmail.mc >sendmail.cf
root@FDB001:~#
root@FDB001:~# service sendmail restart
root@FDB001:~#
root@FDB001:~# (echo From: "WON Notification <notification@XXX.com>";echo To: "lukes.liao@XXXchina.com";echo To: "lukes.liao2@XXXchina.com";echo Subject: "This is test1";echo "The Content is just a test email1")| /usr/sbin/sendmail -t
--收不到郵件,但是service sendmail status看有報錯DSN: Service unavailable
root@FDB001:~# vim /etc/hosts
127.0.0.1 FDB001 FDB001.dai.netdai.com
--加上FDB001.dai.netdai.com
root@FDB001:~# vim /etc/hostname
FDB001.dai.netdai.com
--加上.dai.netdai.com
root@FDB001:~#
root@FDB001:~# cat /root/emailtest.txt
From: WON Notification <notification@XXX.com>
To: lukes.liao@XXXchina.com
To: lukes.liao2@XXXchina.com
Subject: "This is test1"
The Content is just a test email1
root@FDB001:~#
root@FDB001:~# sendmail -t </root/emailtest.txt