如何让crontab给我发送电子邮件?
如何让crontab通过其作业输出向我发送电子邮件? 我在工作上方有MAILTO=redacted@yahoo.com.au
,但它不起作用。
我知道我需要使用像Postfix或Sendmail这样的东西,但对于我的生活,我无法找到如何实现这一目标。 我搜索并搜索了一个关于设置Postfix的简单指南,但它们都非常复杂,并希望您设置一个完整的服务器来发送和接收电子邮件并了解所有条款(例如,什么是域名)外发电子邮件?)。
我想做的就是让crontab给我发电子邮件。 为什么这么难?
附加信息:
我的crontab文件如下所示:
MAILTO=redacted@yahoo.com.au 1 0 * * * ~/Desktop/toskymesh.sh 59 6 * * * ~/Desktop/tooptus.sh 0 3 * * * snapraid sync
更多附加信息:
ps -ef | grep '[s]endmail'
的输出 ps -ef | grep '[s]endmail'
root 6840 1370 0 10:26 ? 00:00:00 sendmail: MTA: accepting connections
我肯定知道cron正在工作,但我正在用echo Test | mail -s Test redacted@yahoo.com.au
mail命令 echo Test | mail -s Test redacted@yahoo.com.au
无论如何echo Test | mail -s Test redacted@yahoo.com.au
。
最后我使用了sSMTP
。 它比Postfix
或sendmail
简单得多,并且工作得很漂亮。
为了将来的参考,这里是如何使用sSMTP与雅虎邮件(不要担心,它远比它看起来复杂):
-
使用Synaptic下载ssmtp。 或者你可以运行
sudo apt-get install ssmtp
。 -
在/etc/ssmtp/ssmtp.conf中打开配置文件。
-
使配置看起来像这样:
root=[yourRealEmail@yahoo.com.au] mailhub=smtp.mail.yahoo.com:587 FromLineOverride=YES UseSTARTTLS=YES AuthUser=[yourRealEmail@yahoo.com.au] AuthPass=[yourRealYahooPassword] TLS_CA_File=~/cert.pem
-
使用OpenSSL创建cert.pem文件。 我使用命令
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 9999 -nodes
( 这里有更多信息)。 你可以把文件粘贴到任何地方,但我只是把它放在〜/。 无论您将它放在何处,请确保将TLS_CA_File=
行指向正确的位置。 -
打开文件
/etc/ssmtp/revaliases
并添加行[yourPCUsername]:[yourRealEmail@yahoo.com.au]:smtp.mail.yahoo.com:587
。 如果你以root身份运行,我认为你需要添加另一行代替你的名字’root’。 -
就是这样,你很高兴! 要测试,最简单的方法(IMO)是创建一个包含以下内容的文件:
To: [yourRealEmail@yahoo.com.au] From: "whateverYaWant" <[yourRealEmail@yahoo.com.au]> Subject: Some Notifying Email MIME-Version: 1.0 Content-Type: text/plain Body of your email goes here! Hello world!
- 保存并关闭文件,然后检查你没有安装真正的sendmail ,运行
sendmail -V
– 应该说’sSMTP’。 - 最后,运行
cat fileWithEmailInIt.txt | sendmail -i -t
cat fileWithEmailInIt.txt | sendmail -i -t
,然后等待几秒钟(10-30)并检查你的电子邮件!
显然,将[yourRealEmail@yahoo.com.au]
替换为您的电子邮件(不带括号)和[yourRealYahooPassword]
和您的Yahoo Mail密码(再次,不带括号)。
附加说明:如果您在使用Gmail时遇到问题,请尝试此答案的第 1选项(感谢Ben Creasy提供该信息)。
安装Postfix。 它比大多数其他软件包更复杂,但它仍然不复杂 。
sudo apt-get install postfix
选择“Internet站点”,然后接受所有默认值。 然后我们只需要停止外部连接,将其转变为“ 空客户端 ”。 运行: sudoedit /etc/postfix/main.cf
并找到inet_interfaces
设置(接近结尾)并将其更改为loopback-only
,如下所示:
inet_interfaces = loopback-only
最后用sudo /etc/init.d/postfix restart
重新启动Postfix(重装不会)。
您现在有一个不会为外部计算机中继电子邮件的Postfix安装,它只接受127.0.0.1上的连接(以及IPv6的:: 1)。
另外,你的cron行可能无法正常工作,因为你正在使用非相对路径和带有Bash替换的路径sh
不理解~
并且它可能没有正确的PATH
集。 所以替换它们(我只是猜测实际的路径):
1 0 * * * /home/clonkex/Desktop/toskymesh.sh 59 6 * * * /home/clonkex/Desktop/tooptus.sh 0 3 * * * /usr/bin/snapraid sync
如果您的脚本需要从特定目录运行,请确保它们进入正确的目录。 不要以为cron
会在正确的位置,因为它可能不会。
我对exim4运气很好。
sudo apt-get install alpine exim4 mailutils eximon4 spf-tools-perl swaks
(alpine只是我喜欢使用的邮件客户端)
在那之后,我跑了
sudo dpkg-reconfigure exim4-config
并按照提示进行操作。 这个页面: https : //help.ubuntu.com/community/Exim4也非常有帮助。 让我跑了大概10分钟。
从sSMTP手册页: “它不做别名,必须在用户代理或mailhub上完成。它也不会尊重.forwards,必须在接收主机上完成。它尤其不会传递给管道。 “
因此,如果您希望在外部电子邮件中接收发送到root的所有消息,那么使用sSMTP是一个坏主意,因为它不支持别名。
相反,您可以使用postfix。 它仍然非常简单。 以下是如何将它与gmail一起用作发送消息的smtp:
sudo apt-get install postfix mailutils
一般邮件配置类型: 卫星系统
系统邮件名称:邮件服务器的首选完全限定名称,例如mail.example.com
SMTP中继主机(空白无): [smtp.gmail.com]:587
添加到/etc/postfix/main.cf:
smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous
创建/ etc / postfix / sasl / sasl_passwd
[smtp.gmail.com]:587 emailtouseforsending@gmail.com:password
然后:
sudo postmap /etc/postfix/sasl/sasl_passwd sudo chown -R root:postfix /etc/postfix/sasl sudo chmod 750 /etc/postfix/sasl sudo chmod 640 /etc/postfix/sasl/sasl_passwd*
编辑/ etc / aliases并添加:
root: email@example.com
最后,运行:
sudo newaliases sudo systemctl stop postfix.service sudo systemctl start postfix.service
现在,您可以测试重定向是否有效:
echo "Test to root." | mail -s "Test message to root" root
希望能帮助到你。