August 6th, 2014, 01:21 AM
Python Sendmail dropping Subject and To lines
I was hoping someone might please be able to assist me on something which has been causing me a headache for a while!
I have a nightly maintenance script written in python which goes through compresses a database, rebuilds indexes etc.
I'm trying to get an email notification output whether fail or success.
I can get the sendmail script to work as a standalone file, however as soon as I embed this within the maintenance script, it drops the Subject line and To line which kind of defeats what I'm trying to do.
I have tried numerous ways to do it but I'm still having a headache.
My sendmail script is:
Any suggestions would be greatly appreciated.
SERVER = "relay.work.com.au"
FROM = "firstname.lastname@example.org"
TO = "email@example.com"
SUBJECT = "The Script Has Completed"
MSG = "This is an auto generated Message.\n\rThe Reconcile & Compress script has completed.\n\n"
# Prepare actual message
MESSAGE = """\
""" % (FROM, TO, SUBJECT, MSG)
# Send the email
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
August 6th, 2014, 11:50 AM
There is no way to tell what happens when you run this from another program, which we don't have, (and you should use a try/except to capture the exact error so you then know more about the problem). There are 3 things that I might suggest
1. call this program from the other program instead of including it, using try/except blocks.
2. Every sendmail example that I have seen uses a dictionary returned from MIMEText example 1, example 2 Also take note of the brackets around recipient used in both examples, (I assume this allows for a list of multiple recipients), i.e from the first example
server.sendmail('firstname.lastname@example.org', ['email@example.com'], msg.as_string())
3. use subprocess.call or popen from the calling program and run in a separate process to eliminate conflicts
Last edited by dwblas; August 8th, 2014 at 11:29 AM.