来自 CRON 的 AWS SES sendmail 失败
AWS SES sendmail from CRON Fails
使用 Integrating Amazon SES with Sendmail 我将 SES 配置为允许它从经过验证的电子邮件地址发送电子邮件。我能够使用经过验证的电子邮件地址从命令行成功发送电子邮件:
sudo /usr/sbin/sendmail -f from@example.com to@example.com < file_to_send.txt
接下来我设置一个 bash 脚本来收集一些每日报告信息。
#!/bin/bash
# copy the cw file
cp /var/log/cwr.log /cwr_analysis/cwr.log
# append the cw info to the subject file
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log > /cwr_analysis/daily.txt
# send the mail
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt
如果我从命令行手动 运行 bash 脚本,报告将按原样收集并通过电子邮件发送。我更改了文件的权限以允许它由 root 执行(类似于 AWS 实例上的其他 CRON 作业):
-rwxr-xr-x 1 root root 375 Jan 6 17:37 cwr_email.sh
问题
我设置了一个 CRON 作业并将其设置为每 5 分钟 运行 以进行测试(脚本设计为在生产开始后每天 运行 一次):
*/5 * * * * /home/ec2-user/cwr_email.sh
bash 脚本复制并正确附加 daily.txt 文件,但 不发送 电子邮件。电子邮件假脱机中没有退回或任何其他错误。
我今天大部分时间都在寻找答案,但许多搜索都陷入了死胡同,几乎没有关于使用 CRON 通过 AWS SES 发送电子邮件的信息。
我该如何解决这个问题?
cron 的一个 "problem" 是缺少环境变量(出于明显的安全原因)。您可能缺少 PATH 和 HOME。您可以直接在脚本中或在 crontab 文件中定义它们。
在调用 sendmail 脚本之前将 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
添加到 crontab,它应该可以工作
#!/bin/bash
#Adding the path
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# copy the cw file
cp /var/log/cwr.log /cwr_analysis/cwr.log
# append the cw info to the subject file
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log > /cwr_analysis/daily.txt
# send the mail
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt
您必须进行测试,直到所有必要的变量都按照脚本的要求进行定义。
使用 Integrating Amazon SES with Sendmail 我将 SES 配置为允许它从经过验证的电子邮件地址发送电子邮件。我能够使用经过验证的电子邮件地址从命令行成功发送电子邮件:
sudo /usr/sbin/sendmail -f from@example.com to@example.com < file_to_send.txt
接下来我设置一个 bash 脚本来收集一些每日报告信息。
#!/bin/bash
# copy the cw file
cp /var/log/cwr.log /cwr_analysis/cwr.log
# append the cw info to the subject file
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log > /cwr_analysis/daily.txt
# send the mail
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt
如果我从命令行手动 运行 bash 脚本,报告将按原样收集并通过电子邮件发送。我更改了文件的权限以允许它由 root 执行(类似于 AWS 实例上的其他 CRON 作业):
-rwxr-xr-x 1 root root 375 Jan 6 17:37 cwr_email.sh
问题
我设置了一个 CRON 作业并将其设置为每 5 分钟 运行 以进行测试(脚本设计为在生产开始后每天 运行 一次):
*/5 * * * * /home/ec2-user/cwr_email.sh
bash 脚本复制并正确附加 daily.txt 文件,但 不发送 电子邮件。电子邮件假脱机中没有退回或任何其他错误。
我今天大部分时间都在寻找答案,但许多搜索都陷入了死胡同,几乎没有关于使用 CRON 通过 AWS SES 发送电子邮件的信息。
我该如何解决这个问题?
cron 的一个 "problem" 是缺少环境变量(出于明显的安全原因)。您可能缺少 PATH 和 HOME。您可以直接在脚本中或在 crontab 文件中定义它们。
在调用 sendmail 脚本之前将 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
添加到 crontab,它应该可以工作
#!/bin/bash
#Adding the path
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# copy the cw file
cp /var/log/cwr.log /cwr_analysis/cwr.log
# append the cw info to the subject file
cat /cwr_analysis/subject.txt /cwr_analysis/cwr.log > /cwr_analysis/daily.txt
# send the mail
/usr/sbin/sendmail -f from@example.com to@example.com < /cwr_analysis/daily.txt
您必须进行测试,直到所有必要的变量都按照脚本的要求进行定义。