Centos下,通过cron实现邮件的定时发送

一、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 from=个人邮箱
set smtp=smtp.sinohonour.com
set smtp-auth-user=个人邮箱
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" guangdong_du@163.com
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:<guangdong.du@sinohonour.com>
250 Mail Ok
>>> RCPT TO:<guangdong_du@163.com>
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 root@192.168.1.187::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后面添加抄送人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值