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
[email protected]
AuthPass=xxx
AuthMethod=LOGIN
------------------------------------------------------------------------------
其中hostname是邮件发送者的主机名(发送者: user@hostname), AuthuXXX是邮件登录相关的, mailhub是邮件服务器地址。
2)修改文件 ‘/etc/ssmtp/revaliases’
---------------------------------------------------------------------
# sSMTP aliases
#
# Format: local_account:outgoing_address:mailhub
#
# Example: root:
[email protected]:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
joans:
[email protected]:mailhub
----------------------------------------------------------------------
配置linux账户使用的帐号,第一个是linux帐号名, 第二个是发送邮件帐号,第三个是邮件服务器地址。
3.测试
echo "content test" | mail -s "subject test" -t
[email protected] [email protected]
发送标题为 "subject test", 内容为 "content test"的邮件到
[email protected]、
[email protected]这两个邮件地址。
4.定时发送
第三步测试通过之后,创建一个脚本来专门发送邮件,然后在cron中定时执行这个脚本就行了。
1)创建~/mail-cron-test, chmod 755 ~/mail-cron-test
----------------------------------------------------
#!/bin/sh
subject="cron test"
content="cron content test"
[email protected]
[email protected]
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 [email protected]
# auth login
# user [email protected]
# password XXX
# logfile ~/.msmtp.log
t=`date +%Y-%m-%d`
m=`date +%m`
MAIL=mutt
echo "$content" | $MAIL -s "$subject" -c $remail $mailto