一、mail配置
郵件發送客戶端使用postfix,一般centos系統安裝時會默認安裝,若未安裝可以使用yum安裝:
yum install -y postfix
確認postfix是否安裝,可以通過rpm命令:
[root@localhost test]# rpm -qa postfix
postfix-2.10.1-6.el7.x86_64
服務是否有啓動可以通過查看25端口,或者通過service查看服務狀態實現:
[root@localhost test]# lsof -i:25
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
master 10072 root 13u IPv4 25752490 0t0 TCP localhost:smtp (LISTEN)
master 10072 root 14u IPv6 25752491 0t0 TCP localhost:smtp (LISTEN)
[root@localhost test]# service postfix status
Redirecting to /bin/systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2018-12-29 11:20:05 CST; 3h 11min ago
Main PID: 10072 (master)
服務啓動
service postfix start
設置服務開機啓動
chkconfig postfix on
配置添加,以163郵箱爲例。編輯/etc/mail.rc,在文件末尾添加如下內容:
vim /etc/mail.rc
set [email protected]
set smtp=smtp.sinohonour.com
set [email protected]
set smtp-auth-password=deccloud.28
set smtp-auth=login
測試時,發現附件名存在亂碼問題,修改mail.rc中的sendcharsets字段如下:
set sendcharsets=utf-8
測試:
[root@localhost test]# echo "test" |mail -v -s "test" [email protected]
Resolving host smtp.sinohonour.com . . . done.
Connecting to 42.120.219.29:smtp . . . connected.
220 smtp.aliyun-inc.com MX AliMail Server(10.147.41.143)
>>> EHLO localhost
250-smtp.aliyun-inc.com
250-STARTTLS
250-8BITMIME
250-AUTH=PLAIN LOGIN XALIOAUTH
250-AUTH PLAIN LOGIN XALIOAUTH
250-PIPELINING
250 DSN
>>> AUTH LOGIN
334 dXNlcm5hbWU6
>>> Z3Vhbmdkb25nLmR1QHNpbm9ob25vdXIuY29t
334 UGFzc3dvcmQ6
>>> ZGVjY2xvdWQuMjg=
235 Authentication successful
>>> MAIL FROM:<[email protected]>
250 Mail Ok
>>> RCPT TO:<[email protected]>
250 Rcpt Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 Data Ok: queued as freedom
>>> QUIT
221 Bye
二、定時任務配置
本文目的主要是爲了解決自己週報、月報總是選錯附件問題,因此設置了定時任務,定期從指定目錄中選擇文件作爲附件發送。
定時任務通過cron實現,定時調用自己編寫的郵件發送腳本。
cron啓動
service crond start
配置
[root@localhost test]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#1 * * * * root rsync -avz --progress --password-file=/etc/rsync-pass [email protected]::cinms /tmp/rsync-test/keepalived
0 23 * * 5 root run-parts /etc/cron.weekly
0 23 28 * * root run-parts /etc/cron.monthly
表示每週六、每月28號的23:00時間分別執行/etc/cron.weekly,/etc/cron.monthly目錄下腳本
發送郵件時添加附件和抄送人命令如下:
echo "" |mail -s "週報" -a "$file_name" -c $cc_addr1 -c $cc_addr2 $to_addr
-a後面添加附件名
-c後面添加抄送人