1

Shell code:

msg="body of the mail"

echo "$msg" | mailx -s "ERROR" udhai

Python Code:

msg="body of the mail"

subprocess.call(msg + " | mailx -s 'ERROR:' udhai",shell=True)

When I execute my shell script, I am receiving email to the udhai account with both the message (body of the mail) and the subject("ERROR:").

But in my python code, I am receiving email only with the subject.

How can I receive the email with the subject and message.

Taku
  • 28,570
  • 11
  • 65
  • 77
sabarish s
  • 49
  • 6
  • 1
    can you try: `subprocess.call("echo '"+ msg + "' | mailx -s 'ERROR:' udhai",shell=True)` and let me know if it works? If not have a look at the pipeline usage in python: https://stackoverflow.com/questions/13332268/python-subprocess-command-with-pipe?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Allan May 02 '18 at 06:19
  • Thanks. it works. Thanks again – sabarish s May 02 '18 at 08:45
  • It works in python interpreter mode . But while executing my python script, i am getting below error . /bin/sh: -c: line 0: unexpected EOF while looking for matching `'' /bin/sh: -c: line 1: syntax error: unexpected end of file Let me check for any syntax error problem and i will get back to you – sabarish s May 02 '18 at 08:53
  • hmmm strange! Yeah let me know if you are still stuck normally it should work. – Allan May 02 '18 at 08:54
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Please see [the relevant help-center article][1] [1]: http://stackoverflow.com/help/someone-answers – Allan May 02 '18 at 08:54
  • It worked. i made some syntax mistake. Sorry for that . Thanks for your help and Advice. I will follow it – sabarish s May 02 '18 at 09:37

1 Answers1

2

You can try to replace your process call by:

subprocess.call("echo '"+ msg + "' | mailx -s 'ERROR:' udhai",shell=True)

otherwise you can change it in the following way:

ps = subprocess.Popen(('echo', msg), stdout=subprocess.PIPE)
output = subprocess.check_output(('mailx', "-s 'ERROR:' udhai"), stdin=ps.stdout)
ps.wait()

see Python subprocess command with pipe

Allan
  • 11,650
  • 3
  • 27
  • 49