Error sending email by calling a bash script

871 Views Asked by At

My sendmail.sh script on Raspbian OS is able to successfully send an email. But when it's called from a python script, I get a "mail: can not send message: process exited with non zero status" error message. I have verified that ssmtp is configured correctly by running sendmail.sh by hand.

sendmail.sh

#!/bin/bash
echo "test" | mail -s "test msg" myemailaddress

permissions on sendmail.sh are 777. sendmail.sh and sendmail.py are in the same directory.

sendmail.py

import os
import subprocess
subprocess.call(['./sendmail.sh'])

The command I use to run the python - sudo python sendmail.py.

I don't understand why the error occurs. Clearly, python is calling sendmail.sh and the script has correct permissions set on it. If run sendmail.sh by hand, the mail is sent correctly.

2

There are 2 best solutions below

2
On

The root cause is the error message given by ssmtp's mail, which is most unhelpful.

A quick googling it reveals http://www.raspberrypi.org/forums/viewtopic.php?t=46218&p=386393 which says the following:

Try running the command with an additional -d parameter to get some more debug information to help determine the cause of the issue:

echo "Test" | mail -d -s "Test" [email protected]

<...>

I checked my error logs, and noticed this:

<date time> raspberrypi sSMTP[3477]: <a bunch of messages, including the error showing the root cause>

0
On

You can try this command:

os.system('./sendmail.sh')