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?
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.
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.
<SPAN class="keyword token">import</SPAN> smtplib <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>mime<SPAN class="punctuation token">.</SPAN>text <SPAN class="keyword token">import</SPAN> MIMEText arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"Enter Your Folder Path"</SPAN> listFiles <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># me is the sender's email address</SPAN> <SPAN class="comment token"># you is the recipient's email address</SPAN> msg <SPAN class="operator token">=</SPAN> MIMEText<SPAN class="punctuation token">(</SPAN>text<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'plain'</SPAN><SPAN class="punctuation token">)</SPAN> msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'From'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> me msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'To'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> you msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Subject'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Email"</SPAN> <SPAN class="comment token"># email body</SPAN> text <SPAN class="operator token">=</SPAN> listFiles <SPAN class="comment token"># Send the message via SMTP server</SPAN> s <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'smtp.escondido.org'</SPAN><SPAN class="punctuation token">)</SPAN> s<SPAN class="punctuation token">.</SPAN>sendmail<SPAN class="punctuation token">(</SPAN>me<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>you<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> msg<SPAN class="punctuation token">.</SPAN>as_string<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> s<SPAN class="punctuation token">.</SPAN>quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I just want the returned list of files displayed in the body.
import smtplibfrom 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'] = memsg['To'] = youmsg['Subject'] = "Email"
# email bodytext=' body'
# Send the message via SMTP servers=smtplib.SMTP('smtp.escondido.org')s.sendmail(me, [you], msg.as_string())s.quit()
What kind of information are you trying to send in the email? Can you also share the code you are working with?
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.
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 - Stack Overflow
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!
<SPAN class="keyword token">import</SPAN> smtplib <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>MIMEMultipart <SPAN class="keyword token">import</SPAN> MIMEMultipart <SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>MIMEText <SPAN class="keyword token">import</SPAN> MIMEText <SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>MIMEBase <SPAN class="keyword token">import</SPAN> MIMEBase <SPAN class="keyword token">from</SPAN> email <SPAN class="keyword token">import</SPAN> encoders lyrfile <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\data\streets.lyr"</SPAN> result <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>lyrfile<SPAN class="punctuation token">)</SPAN> fromaddr <SPAN class="operator token">=</SPAN> <SPAN class="string token">"YOUR EMAIL"</SPAN> toaddr <SPAN class="operator token">=</SPAN> <SPAN class="string token">"EMAIL ADDRESS YOU SEND TO"</SPAN> msg <SPAN class="operator token">=</SPAN> MIMEMultipart<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'From'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> fromaddr msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'To'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> toaddr msg<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Subject'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SUBJECT OF THE EMAIL"</SPAN> body <SPAN class="operator token">=</SPAN> <SPAN class="string token">'{} has {} records'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>lyrfile<SPAN class="punctuation token">,</SPAN> result<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> msg<SPAN class="punctuation token">.</SPAN>attach<SPAN class="punctuation token">(</SPAN>MIMEText<SPAN class="punctuation token">(</SPAN>body<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'plain'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> filename <SPAN class="operator token">=</SPAN> <SPAN class="string token">"NAME OF THE FILE WITH ITS EXTENSION"</SPAN> attachment <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"PATH OF THE FILE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"rb"</SPAN><SPAN class="punctuation token">)</SPAN> part <SPAN class="operator token">=</SPAN> MIMEBase<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'application'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'octet-stream'</SPAN><SPAN class="punctuation token">)</SPAN> part<SPAN class="punctuation token">.</SPAN>set_payload<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>attachment<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> encoders<SPAN class="punctuation token">.</SPAN>encode_base64<SPAN class="punctuation token">(</SPAN>part<SPAN class="punctuation token">)</SPAN> part<SPAN class="punctuation token">.</SPAN>add_header<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Content-Disposition'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"attachment; filename= %s"</SPAN> <SPAN class="operator token">%</SPAN> filename<SPAN class="punctuation token">)</SPAN> msg<SPAN class="punctuation token">.</SPAN>attach<SPAN class="punctuation token">(</SPAN>part<SPAN class="punctuation token">)</SPAN> server <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'smtp.gmail.com'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">587</SPAN><SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>starttls<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>login<SPAN class="punctuation token">(</SPAN>fromaddr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"YOUR PASSWORD"</SPAN><SPAN class="punctuation token">)</SPAN> text <SPAN class="operator token">=</SPAN> msg<SPAN class="punctuation token">.</SPAN>as_string<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>sendmail<SPAN class="punctuation token">(</SPAN>fromaddr<SPAN class="punctuation token">,</SPAN> toaddr<SPAN class="punctuation token">,</SPAN> text<SPAN class="punctuation token">)</SPAN> server<SPAN class="punctuation token">.</SPAN>quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.