定时发送邮件

本文介绍如何在Linux系统上配置并使用mailutils和ssmtp发送邮件,包括安装、配置、测试及定时发送邮件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.安装
sudo apt-get install mailutils
sudo apt-get install ssmtp

2.修改配置
1)修改文件‘/etc/ssmtp/ssmtp.conf’
---------------------------------------------------------------------------
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=user

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail.domain.com

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=gmail.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

AuthUser=xxx@xxx.com
AuthPass=xxx
AuthMethod=LOGIN
------------------------------------------------------------------------------

其中hostname是邮件发送者的主机名(发送者: user@hostname),  AuthuXXX是邮件登录相关的, mailhub是邮件服务器地址。

2)修改文件 ‘/etc/ssmtp/revaliases’
---------------------------------------------------------------------
# sSMTP aliases
#
# Format:   local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.

joans:xxx@xxx.com:mailhub
----------------------------------------------------------------------

配置linux账户使用的帐号,第一个是linux帐号名, 第二个是发送邮件帐号,第三个是邮件服务器地址。

3.测试
echo "content test" | mail -s "subject test" -t xxx@xxx.com xxx2@xxx.com
发送标题为 "subject test", 内容为 "content test"的邮件到xxx@xxx.com、xxx2@xxx.com这两个邮件地址。


4.定时发送
第三步测试通过之后,创建一个脚本来专门发送邮件,然后在cron中定时执行这个脚本就行了。
1)创建~/mail-cron-test, chmod 755 ~/mail-cron-test
----------------------------------------------------
#!/bin/sh
subject="cron test"
content="cron content test"

mailto=xxx@yahoo.com.cn
remail=xxx1@gmail.com

echo "$content" | mail -s "$subject" -t $remail $mailto
----------------------------------------------------
2)修改 /etc/crontab, 每天17点发送邮件(即执行上面那个脚本)
-----------------------------------------------------------------------
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
30 19   * * 1-5     joans   ~/mail-cron-test
#
--------------------------------------------------------------------------
说明:
m   : 表示分钟
h   : 表示小时
dom : 表示日期(0~31)
mon : 表示月份
dow : 表示星期(0~6)
user: linux用户
command : 时间到之后执行的命令




5.调试
1)发送邮件可以执行 ‘tail -f /var/log/mail.log‘查看邮件的输出信息。
2)调试cron, 把每个字段都为'*', 然后每分钟就会执行一次脚本,在脚本里输出信息到一个文件即可。




========================================================================================

使用mutt + msmtp 发送邮件:

# mutt config @/etc/Muttrc
# Add these lines :
#   #========== add by xxx ==========
#   set sendmail="/usr/bin/msmtp"
#   set use_from="yes"
#   set realname="xxxx"
#   set from="xxx"
#   set envelope_from=yes
#   #=========== Fix encode programe =========
#   set charset="utf-8"
#   #set send_charset="gb2312"
#   set send_charset="utf-8"
#   set locale="zh_CN.UTF-8"
#   set content_type="text/html\;charset=utf-8"

# msmtp config @~/.msmtprc
#   account default
#   host mail.163.cn
#   from xxxg@163.cn
#   auth login
#   user xxx@163.cn
#   password XXX
#   logfile ~/.msmtp.log


t=`date  +%Y-%m-%d`
m=`date  +%m`

MAIL=mutt

echo "$content" | $MAIL -s "$subject" -c $remail $mailto





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值