python email message for errors

4962
4
02-03-2016 05:57 PM
KathleenWallis
Occasional Contributor II

For years I had all my python jobs set up to send me an email error message if the python job did not complete successfully.
We just upgraded out outlook to Outlook 365.
My python code always looked like

def reportError( msg 😞   

    SERVER = "nms1.cityofnapa.org"

    FROM = "johndoe@cityofnapa.org"

    TO = "johndoe@cityofnapa.org"

    SUBJECT = socket.gethostname() + " - compress ERROR"

    TEXT = msg

   

    # Prepare actual message

    message = """\

    From: %s

    To: %s

    Subject: %s %s """ % (FROM, TO , SUBJECT, "Text")

    message = "From: %s\nTo: %s\nSubject: %s\n\n%s" % ( FROM, TO, SUBJECT, TEXT )    

    # Send the mail

    server = smtplib.SMTP(SERVER)

    server.sendmail(FROM, TO, message)

    server.quit()

    print msg

    print "error"

-----------------------------------------------------------

My IT department sent out a message saying that the new outlook 365 server is now

So I changed the top line of my code to

SERVER = "cityofnapa-org.mail.protection.outlook.com"


But now the error emails never get sent out.
Any ideas? 

Any help is appreciated.

KW

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

I dont know about the SERVER name change, could you try the new formatting syntax ( I just assigned a, b, c, d to the variables

>>> frmt = "From: {}\nTo: {}\nSubject: {}\n\n{}"
>>> print(frmt.format( FROM, TO, SUBJECT, TEXT ))
From: a
To: b
Subject: c


d

if you are having problems with the string representation ofthe variables, try the raw format

string formatting  {} is the same as {!s:}

raw formatting {!r:} is used since some objects have different string andobject representations when printed.

0 Kudos
AliciaSoto
Occasional Contributor

This worked for me. Might not help since your IT dept changed the server and port.

server = smtp.office365.com

port = 587

0 Kudos
BlakeTerhune
MVP Regular Contributor

This is my basic outline for emails on our scheduled task scripts.

from email.mime.multipart import MIMEMultipart  ## Build email parts
from email.mime.text import MIMEText  ## Record email body type
import smtplib  ## Send email message
from time import strftime
import os

def main():
    # Email variables
    addr_from = "PythonScript@mydomain.com"
    addr_to = ["somename@mydomain.com"]
    addr_cc = []
    addr_bcc = []  ## BCC is not used in the MIMEMultipart message but will be sent with SMTP
    recipients = addr_to+addr_cc+addr_bcc
    ## Create email message container and set message parts
    msg_root = MIMEMultipart('alternative')
    msg_root['From'] = addr_from
    msg_root['To'] = ", ".join(addr_to)
    msg_root['Cc'] = ", ".join(addr_cc)
    msg_root['Subject'] = os.path.basename(__file__)

    # Opening lines of email message body
    msg = "{scriptName} ran on {hostComputer}".format(
        scriptName = os.path.basename(__file__),
        hostComputer = gethostname()
    )
    istring = " Started {} ".format(strftime("%Y-%m-%d %H:%M:%S"))
    msg += "\n\n{:~^64}\n\n".format(istring)

    # #
    # Do some stuff
    # and update email with msg += "something\n"
    # don't forget the new line
    # #

    # Close and send the email
    msg_body = MIMEText(msg, 'plain')  ## Record the MIME type of email message body as plain text
    msg_root.attach(msg_body)  ## Attach msg_body content to msg_root container
    s = smtplib.SMTP('cityofnapa-org.mail.protection.outlook.com')
    s.set_debuglevel(1)  ## Print smtp debug info in Python interpreter (optional)
    s.sendmail(addr_from, recipients, msg_root.as_string())
    s.quit()


if __name__ == '__main__':
    main()
0 Kudos
KathleenWallis
Occasional Contributor II

Thanks guys - I will give this a try.
Katy

0 Kudos