AnsweredAssumed Answered

Change SMTP to HTML in ReconcileCompress script

Question asked by rbokern on Mar 14, 2012
Latest reply on Apr 30, 2012 by rbokern
I am relatively new to Python and do not quite know how to code this. The script is the Reconcile Compress Notify by Russellb, found in the Gallery. I have expanded the scope on it and it is working fine and sending me emails (SMTP) while I test. I would, however, like to send it as HTML but do not know how to capture the messages into the body of the email. Below is the end of the code that sends the gathered messages from messageForEmail for SMTP. I have found an example in the Python Docs for HTML and placed it at the end but it doesn't include the messages. I added the HTML code at the bottom. Any guidance is helpful.

finally:
    #Create a helper function for sending email.
    def sendEmail(subject, emailmessage):
        # take the email list and use it to send an email to connected users.
        SERVER = "mailserver"
        FROM = "Python Admin <admin@esri.com>"
        TO = 'user@esri.com'
        SUBJECT = subject
        MSG = emailmessage

        # Prepare actual message
        MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
        """ % (FROM, TO, SUBJECT, MSG)
        #Connect to the server
        server = smtplib.SMTP(SERVER)
        # Send the mail
        server.sendmail(FROM, TO, MESSAGE)
        #Disconnect from the server.
        server.quit()
        #multiple examples for sending emails.
        #http://docs.python.org/library/email-examples.html#email-examples

    #Send a summary using the send email function and the message that has been created.

    sendEmail('Compress summary', messageForEmail)
###################################################HTML Email
## Option 2 as html
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
FROM = "Rick Bokern <Rick.Bokern@allencounty.us>"
TO = "Rick Bokern <rick.bokern@co.allen.in.us>"
SERVER = "********"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Automated Reconcile & Post Results for SDE10 Test Geodatabases"
msg['From'] = FROM #me
msg['To'] = TO #you

# Create the body of the message (a plain-text and an HTML version).
#text = "Hi Rick!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
##MESSAGE = """\
##<html>
##  <head></head>
##  <body>
##    <p>Hi Rick!<br>
##       How are you?<br>
##       Here is the <a href="http://www.python.org">link</a> you wanted.
##    </p>
##  </body>
##  Powered by <i>iMap</i>
##</html>
##"""
##"""
messageForEmail = """\
<html>
  <head></head>
  <body>
    <p>%s<br>
       It worked Rick!<br>
    </p>
  </body><br>
  Powered by <i>iMap</i>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
#part1 = MIMEText(text, 'plain')
part2 = MIMEText(messageForEmail, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
#msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP(SERVER)
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(FROM, TO, msg.as_string())
s.quit()


Thanks in advance!

Outcomes