2017-01-06 18 views
1

使用Integrating Amazon SES with Sendmail我配置了SES以允許它從驗證的電子郵件地址發送電子郵件。來自CRON的AWS SES sendmail失敗

sudo /usr/sbin/sendmail -f [email protected] [email protected] < 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 [email protected] [email protected] < /cwr_analysis/daily.txt 

如果我在命令行手動運行bash腳本的報告收集和發送電子郵件,因爲它應該是。我更改文件的權限,使其能夠以root執行(類似於其他cron作業的AWS實例):

-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發送電子郵件的信息。

我該如何解決這個問題?

+0

是否將cron作業添加爲'root'用戶? – helloV

+0

是@helloV,cron用於root用戶。 –

+0

你有試過嗎? http://serverfault.com/a/615344 –

回答

4

cron的一個「問題」是缺少環境變量(出於明顯的安全原因)。您可能錯過了PATH和HOME。您可以直接在腳本中或在crontab文件中定義這些腳本。

添加PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/us‌​r/bin到crontab調用sendmail的腳本之前,它應該工作

#!/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 

你必須測試,直到所要求的腳本,所有必要的變量定義。

+0

CRON正在工作,它只是不發送電子郵件。 –

+0

我的意思是添加PATH將幫助它找到運行sendmail命令所需的env變量。 –

+0

添加哪個PATH?腳本中的sendmail路徑非常清晰。 –