0

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.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Btw, do use `check_call` rather than `call` unless you check the exit status manually (which, apparently, you don't). – ivan_pozdeev Nov 14 '14 at 03:37
  • 1
    Whatever your problem,**chmod 777 is wrong and dangerous**. Please revert the permissions to something sane immediately (0755 would seem suitable here). – tripleee Nov 14 '14 at 04:01
  • Does the calling script run in a different directory? Then `./sendmail.sh` will be a "file not found" error. (Why so you split the functionality across two separate scripts anyway?) – tripleee Nov 14 '14 at 04:06

2 Answers2

1

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" myemail@mydomain.co.uk

<...>

I checked my error logs, and noticed this:

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

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Thank you for your suggestion of setting debug flag. From the debug I identified the problem. The scripts in this case python and shell are owned by user "pi". However for the work I need to do, python needs to run as sudo, Because shell is being called from within this python script, the shell script was being run as "sudo". Because ssmtp is configured to send email only as a "pi" user, the python script was giving the error on execution of sendmail.sh. – user702886 Nov 14 '14 at 22:16
  • Thank you all who took time to answer the question. Appreciate the help. – user702886 Nov 14 '14 at 22:18
-2

You can try this command:

os.system('./sendmail.sh')
royhowie
  • 11,075
  • 14
  • 50
  • 67
NeoWu
  • 241
  • 2
  • 4