Select to view content in your preferred language

How to display Python results into Email Body.

10708
9
09-19-2019 09:16 AM
DevinUnderwood3
Regular Contributor

I use from email.mime.text import MIMEText to use typed text into the email body.

How do I pass the python returned results into the body of an email?

0 Kudos
9 Replies
Jeremy_Moore
MVP Alum

Here is the reference for the python email package which also handles MIME:

19.1. email — An email and MIME handling package — Python 3.4.10 documentation 

Here are some examples:

19.1.14. email: Examples — Python 3.4.10 documentation 

Here is a working script example that I found on stackoverflow:

MIMEMultipart, MIMEText, MIMEBase, and payloads for sending email with file attachment in Python - S... 

In the code from stackoverflow, you would substitute the text for "body =" with any information from arcpy. For example if you wanted to know the feature count of a feature class or layer and you wanted to email your self the count. You would do something like what is below ( I have no idea if this works, I just quickly hacked it together). You will also need to learn how to use SMTP lib so you can connect, login and send emails. Take the code below and see if you can get it to send an email. 

Have Fun!

import smtplib
import arcpy
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

lyrfile = r"C:\data\streets.lyr"
result = arcpy.GetCount_management(lyrfile)

fromaddr = "YOUR EMAIL"
toaddr = "EMAIL ADDRESS YOU SEND TO"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"

body = '{} has {} records'.format(lyrfile, result[0])

msg.attach(MIMEText(body, 'plain'))

filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
0 Kudos
DevinUnderwood3
Regular Contributor

Thanks for your response, I have already successfully sent an email, but with just typed text.

I don't think your example will work since it refers to attachments which I don't want.

I tested this out and it does not work, I still receive a blank email body.

0 Kudos
Jeremy_Moore
MVP Alum

What kind of information are you trying to send in the email? Can you also share the code you are working with?

0 Kudos
DevinUnderwood3
Regular Contributor

import smtplib
from email.mime.text import MIMEText


arcpy.ListFiles()

# me is the sender's email address
# you is the recipient's email address


msg = MIMEText(text, 'plain')
msg['From'] = me
msg['To'] = you
msg['Subject'] = "Email"

# email body
text=' body'


# Send the message via SMTP server
s=smtplib.SMTP('smtp.escondido.org')
s.sendmail(me, [you], msg.as_string())
s.quit()

0 Kudos
DevinUnderwood3
Regular Contributor

I just want the returned list of files displayed in the body.

0 Kudos
Jeremy_Moore
MVP Alum

I edited your code a little bit, I saw some errors. Try this code below and let me know if it works. If it does then we can do some text formatting so it looks pretty.

import smtplib
import arcpy
from email.mime.text import MIMEText

arcpy.env.workspace = r"Enter Your Folder Path"
listFiles = arcpy.ListFiles()

# me is the sender's email address
# you is the recipient's email address
msg = MIMEText(text, 'plain')
msg['From'] = me
msg['To'] = you
msg['Subject'] = "Email"

# email body
text = listFiles

# Send the message via SMTP server
s = smtplib.SMTP('smtp.escondido.org')
s.sendmail(me, [you], msg.as_string())
s.quit()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DevinUnderwood3
Regular Contributor

Thanks for your advice.

I just needed to included the email parameters into my list files loop function.

I am one step closer to success. I only get a result of the last item in the specified folder.

I need to modify how I am looping the folder and how it passes to the text to loop also.

0 Kudos
DevinUnderwood3
Regular Contributor

Update: I know receive separate emails for each listed file. Now, if can combine all of these emails into just one.

0 Kudos
LeoLiu1
Frequent Contributor

Doesn't arcpy.ListFiles() return you all the files' names in your folder as a single string ?

0 Kudos