Jay Blanchard
Jay Blanchard

Reputation: 34426

AWS SES sendmail from CRON Fails

Using Integrating Amazon SES with Sendmail I configured SES to allow it to send emails from a verified email address. I was able to successfully send email from the command line using the verified email address:

sudo /usr/sbin/sendmail -f [email protected] [email protected] < file_to_send.txt

Next I setup a bash script to gather some daily report information.

#!/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 [email protected] [email protected] < /cwr_analysis/daily.txt   

If I run the bash script manually from the command line the report is gathered and emailed as it should be. I changed the permissions on the file to allow it to be executed by root (similar to other CRON jobs on the AWS instance):

-rwxr-xr-x 1 root     root       375 Jan  6 17:37 cwr_email.sh

PROBLEM

I setup a CRON job and set it to run every 5 minutes for testing (the script is designed to be run once per day once production starts):

*/5 * * * * /home/ec2-user/cwr_email.sh

The bash script copies and then appends the daily.txt file properly but does not send the email. There is no bounce in the email spool or any other errors.

I have spent the better part of today searching for an answer and many of the searches end up on dead-ends with little to no information about using a CRON to send email via AWS SES.

How can I fix this issue?

Upvotes: 1

Views: 3084

Answers (1)

Karan Shah
Karan Shah

Reputation: 1324

One "problem" with cron is that lack of environment variables (for obvious security reasons). You are probably missing PATH and HOME. You can define those in the script directly or in the crontab file.

Add PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us‌​r/bin to the crontab before you call the sendmail script and it should work

#!/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 [email protected] [email protected] < /cwr_analysis/daily.txt 

You'll have to test until all the necessary variables are defined as required by the script.

Upvotes: 5

Related Questions