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 = 'me@test.com'
receivers = ['you@test.com']
message = """From: myself<me@test.com>
To: you@test.com
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
  • Looks like you’re missing the f-string. Perhaps this line should be: `f'{sys.argv[1]}'`. – S3DEV Oct 08 '20 at 09:36
  • have tried `message = (f'From: myself\n' f'To: you@test.com\n' f'Subject: missing files\n' f'{sys.argv[1]}')` but it says there is syntax error after `message = ('From: myself\n'` on '. I am using python 2.7 too. – Alan Chu Oct 08 '20 at 09:39
  • Try: ```message = ('From: myself\n' 'To: you@test.com\n' 'Subject: missing files\n' + sys.argv[1])``` – mananony Oct 08 '20 at 09:42
  • 2.7? Mate, upgrade; 2.7 is out of support. That aside, 2.7 supports the `.format()` function on strings. Example: `'file: {}'.format(sys.argv[1])`. – S3DEV Oct 08 '20 at 09:44
  • Didn't you ask the same question a couple of days ago? – tripleee Oct 08 '20 at 10:24

1 Answers1

1

You don't need Python to send mail.

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

sendmail -oi -t <<:
From: myself <me@test.com>
To: you@test.com
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 <me@test.com>
To: you@test.com
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 = 'me@test.com'
receivers = ['you@test.com']
message = EmailMessage()
message['from'] = 'myself <me@test.com>'  # 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