Change SMTP to HTML in ReconcileCompress script

517
1
Jump to solution
03-14-2012 10:06 AM
RickBokern
New Contributor II
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!
0 Kudos
1 Solution

Accepted Solutions
RickBokern
New Contributor II
Here is the code that answers my question. It comes from the author of the ReconcileCompress script. The only issue I have encountered is that the entire message comes as one paragraph. I haven't yet taken the time to see about breaking it into separate parargraphs to suit my purpose.

msgText = compressHeader
msgText += '<p><b>%s</b><p>' % (compressLog) #using string substitution to insert my string variable between the html tags.
msgText += reconcileHeader
msgText += '<p><i>%s</i><p>' % (reconcileLog)




My test script below:
============================
compressHeader = 'Compress Results'
compressLog = 'Compress was successful'
reconcileHeader = 'Reconcile Results'
reconcileLog = 'Reconcile was successful'

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "me@esri.com"
you = "you@esri.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Reconcile and Post"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
msgText = compressHeader
msgText += '<p><b>%s</b><p>' % (compressLog)
msgText += reconcileHeader
msgText += '<p><i>%s</i><p>' % (reconcileLog)


# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(msgText, '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)

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

View solution in original post

0 Kudos
1 Reply
RickBokern
New Contributor II
Here is the code that answers my question. It comes from the author of the ReconcileCompress script. The only issue I have encountered is that the entire message comes as one paragraph. I haven't yet taken the time to see about breaking it into separate parargraphs to suit my purpose.

msgText = compressHeader
msgText += '<p><b>%s</b><p>' % (compressLog) #using string substitution to insert my string variable between the html tags.
msgText += reconcileHeader
msgText += '<p><i>%s</i><p>' % (reconcileLog)




My test script below:
============================
compressHeader = 'Compress Results'
compressLog = 'Compress was successful'
reconcileHeader = 'Reconcile Results'
reconcileLog = 'Reconcile was successful'

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "me@esri.com"
you = "you@esri.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Reconcile and Post"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
msgText = compressHeader
msgText += '<p><b>%s</b><p>' % (compressLog)
msgText += reconcileHeader
msgText += '<p><i>%s</i><p>' % (reconcileLog)


# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(msgText, '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)

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