#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2014
    Posts
    1
    Rep Power
    0

    Question Python Sendmail dropping Subject and To lines


    Hi all,
    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:

    Code:
    import smtplib
    
    
    SERVER = "relay.work.com.au"
    FROM = "me@work.com.au"
    TO = "me@work.com.au"
    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: %s
    To: %s
    Subject: %s
    
    %s
    """ % (FROM, TO, SUBJECT, MSG)
    
    # Send the email
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, MESSAGE)
    server.quit()
    Any suggestions would be greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    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('author@example.com', ['recipient@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 12:29 PM.

IMN logo majestic logo threadwatch logo seochat tools logo