I am trying to write the contents of a csv to the body of an email and of course having issues.
I can list out the contents as such. But looking for a formatted HTML version that I can place in the body of an email...I have been through a dozen of forums and cant seem to find a simple solution and one that works..
csv<SPAN class="punctuation token">.</SPAN>register_dialect<SPAN class="punctuation token">(</SPAN>
<SPAN class="string token">'mydialect'</SPAN><SPAN class="punctuation token">,</SPAN>
delimiter <SPAN class="operator token">=</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">,</SPAN>
quotechar <SPAN class="operator token">=</SPAN> <SPAN class="string token">'"'</SPAN><SPAN class="punctuation token">,</SPAN>
doublequote <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN>
skipinitialspace <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN>
lineterminator <SPAN class="operator token">=</SPAN> <SPAN class="string token">'\r\n'</SPAN><SPAN class="punctuation token">,</SPAN>
quoting <SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>QUOTE_MINIMAL<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n Now the output from a dictionary created from the csv file"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'z_outFileRegion1.csv'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'rb'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> mycsvfile<SPAN class="punctuation token">:</SPAN>
dictofdata <SPAN class="operator token">=</SPAN> csv<SPAN class="punctuation token">.</SPAN>DictReader<SPAN class="punctuation token">(</SPAN>mycsvfile<SPAN class="punctuation token">,</SPAN> dialect<SPAN class="operator token">=</SPAN><SPAN class="string token">'mydialect'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> dictofdata<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'region'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">"\t "</SPAN><SPAN class="operator token">+</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'county'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">"\t "</SPAN><SPAN class="operator token">+</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'ramp'</SPAN><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>
I tried this one but cant get it to work
python - Send the contents of a CSV as a table in an email? - Stack Overflow
Python send email from a csv list - Stack Overflow
Anyone have any easy solution to grab the CSV, format with HTML and attach it to the body of the email?
Below I am attaching the csv to the email but want to display the records in the email itself.
filename <SPAN class="operator token">=</SPAN> <SPAN class="string token">"z_outFileRegion1.csv"</SPAN>
f <SPAN class="operator token">=</SPAN> file<SPAN class="punctuation token">(</SPAN>filename<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> MIMEMultipart<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#body = "This is the message"</SPAN>
<SPAN class="comment token">#content = MIMEText(body, 'plain')</SPAN>
<SPAN class="comment token">#msg.attach(content)</SPAN>
attachment <SPAN class="operator token">=</SPAN> MIMEText<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">.</SPAN>read<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
attachment<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'</SPAN><SPAN class="punctuation token">,</SPAN> filename<SPAN class="operator token">=</SPAN>filename<SPAN class="punctuation token">)</SPAN>
msg<SPAN class="punctuation token">.</SPAN>attach<SPAN class="punctuation token">(</SPAN>attachment<SPAN class="punctuation token">)</SPAN>
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">"Region Report "</SPAN> <SPAN class="operator token">+</SPAN> CurrentDate
server <SPAN class="operator token">=</SPAN> smtplib<SPAN class="punctuation token">.</SPAN>SMTP<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'smtp1.fff'</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>as_string<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
server<SPAN class="punctuation token">.</SPAN>quit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</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>