0

i would like to pass the variable from my shell script script.sh to python pythonEmail.py. The script works but the email message do not contains the sys.argv (abc.txt)/($file). Im using python 2.7 too.

script.sh:

#!/bin/bash
file=abc.txt
echo "the following" ${file} "are missing"
pythonEmail.py $file

pythonEmail.py:

#!/usr/bin/python
import smtplib
import sys
sender = '[email protected]'
receivers = ['[email protected]']
message = """From: myself<[email protected]>
To: [email protected]
Subject: missing files
%s""" % (sys.argv[1])
try:
   smtpObj = smtplib.SMTP("localhost")
   smtpObj.sendmail(sender, receivers, message)
   print ("Successfully sent email")
except SMTPException:
   print ("Error: unable to send email")
Alan Chu
  • 15
  • 2
  • 7

1 Answers1

1

You don't need Python to send mail.

#!/bin/bash
file="abc.txt"

sendmail -oi -t <<:
From: myself <[email protected]>
To: [email protected]
Subject: missing files

the following $file are missing
:

The immediate problem in your code is that you don't have an empty line between the headers and the body (we call this a "neck").

If you insist on using Python, do you want to pass the input as command-line arguments, or as standard input, or some unholy mix of both?

echo "the following $file are missing" |
(cat <<\:; cat -
From: myself <[email protected]>
To: [email protected]
Subject: missing files

:
) |
sendmail -oi -t

where of course if you really insist, you can replace the part after echo with the equivalent Python code if you like.

Using Python here does bring some benefits if you use its email library instead of try to glue together a valid email message from some ASCII strings, especially if your message isn't always going to be guaranteed to be pure English ASCII from the 1960s.

My suggestion would be to pass a list of file names as command-line arguments, and put the message formatting in the Python script where you have already hard-coded the subject, recipient, etc.

#!/bin/bash
file="abc.txt"
pythonEmail.py "$file"

pythonEmail.py, pretty much directly from https://docs.python.org/3/library/email.examples.html

#!/usr/bin/python
import smtplib
from email.message import EmailMessage
import sys

sender = '[email protected]'
receivers = ['[email protected]']
message = EmailMessage()
message['from'] = 'myself <[email protected]>'  # notice space before <
message['to'] = ', '.join(receivers)
message['subject'] = 'missing files'
# Collect the command-line arguments, one per line
body = ['the following %s are missing' % file for file in sys.argv[1:]]
message.set_content('\n'.join(body))

try:
   smtpObj = smtplib.SMTP("localhost")
   smtpObj.send_message(sender, receivers, message)
   print ("Successfully sent email")
except SMTPException:
   print ("Error: unable to send email")

If you want to read the message body from standard input,

body = [line for line in sys.stdin]

Some of this will require downgrading if you insist on using Python 2.7; but you really should be figuring out how to migrate to Python 3 if you are writing new code. The email library was overhauled in Python 3.6; it's not hard to find examples of how to use the older email.message.Message class if you absolutely need to travel back in time. Expect uncurable plague and bad smells. Even older versions of email should know how to properly encode text with accents or other "modern" typography like curly quotes (and attach HTML etc if you need to) which is hard to do properly and elegantly from a shell script.

tripleee
  • 175,061
  • 34
  • 275
  • 318