I am trying to attach a txt file to my email I am creating in python. Anyone know how I can attach a txt file to the email?
<SPAN class="comment token">#....snip</SPAN>
outFile <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\Users\\TestFile.text"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"w"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># return all rows</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'*'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>expression<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># return specific rows</SPAN>
<SPAN class="comment token">#with arcpy.da.SearchCursor(fc, [class_field, name_field], where_clause=expression) as cursor:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">## Print the name of the residential road</SPAN>
<SPAN class="comment token">#print('{0}, {1}'.format(row[0], row[1]))</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
zval <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{0}, {1}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
outFile<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>zval <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">)</SPAN>
outFile<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#===SET EMAIL To AND From==============================</SPAN>
FROMADDR <SPAN class="operator token">=</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-email-small" href="mailto:me@somewherea.gov" rel="nofollow noopener noreferrer" target="_blank">me@somewherea.gov</A><SPAN>"</SPAN></SPAN>
TOADDRS <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-email-small" href="mailto:me@somewherea.gov" rel="nofollow noopener noreferrer" target="_blank">me@somewherea.gov</A><SPAN>"</SPAN></SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#===BUILD EMAIL========================================</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">msgEmail</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
msg <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Test"</SPAN>
server <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'smtp0.sitevision.com'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">25</SPAN><SPAN class="punctuation token">)</SPAN>
server<SPAN class="punctuation token">.</SPAN>set_debuglevel<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
server<SPAN class="punctuation token">.</SPAN>ehlo<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> TOADDRS<SPAN class="punctuation token">,</SPAN> msg<SPAN class="punctuation token">)</SPAN>
server<SPAN class="punctuation token">.</SPAN>quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="string token">"Email Sent but Import Aborted"</SPAN>
<SPAN class="comment token">#===PROCESS EMAIL======================================</SPAN>
getMsgP <SPAN class="operator token">=</SPAN> msgEmail<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> getMsgP<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>