【问题标题】:AWS SES sendmail from CRON Fails来自 CRON 的 AWS SES 发送邮件失败
【发布时间】:2017-05-21 17:16:16
【问题描述】:

我使用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作业是否添加为root用户?
  • 是的@helloV,cron是给root用户的。
  • 你试过了吗? serverfault.com/a/615344

标签: bash amazon-web-services cron amazon-ses


【解决方案1】:

cron 的一个“问题”是缺少环境变量(出于明显的安全原因)。您可能缺少 PATH 和 HOME。可以直接在脚本中定义,也可以在 crontab 文件中定义。

在调用 sendmail 脚本之前将 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us‌​r/bin 添加到 crontab 中,它应该可以工作

#!/bin/bash  
#Adding the path                                                                                                                                                                                     
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us‌​r/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 

您必须进行测试,直到所有必要的变量都按照脚本的要求定义为止。

【讨论】:

  • CRON 工作正常,只是没有发送电子邮件。
  • 我的意思是添加 PATH 将帮助它找到运行 sendmail 命令所需的环境变量。
  • 添加哪个路径? sendmail 的路径在脚本中定义得很清楚。
  • 尝试将PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin添加到cron
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 2018-06-24
  • 2022-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2020-10-20
  • 2017-11-11
相关资源
最近更新 更多