Closed nutcrackerhf closed 5 years ago
I know that to apply CSS in any email, it cannot be <style>
tags, but has to be like <h1 style="color:red">text</h1>
. Maybe that insight helps?
Also, I think you do not want inline... Try it without using yagmail.inline
, and then using an html_string. This will then get parsed as a whole and become the email message. Other option is to pass a html filepath string.
E.g.: yag.send(contents=df_styled_html).
or yag.send(contents='styled_rendered.html')
If you can apply CSS onto this HTML, that's what you probably need. You might want to consider stripping \n
, since they are converted to </br>
Thanks. Python has a built-in html escaper which can be applied to a styled/rendered html object like df_styled_rendered
in the sample code above:
import html
df_styled_html_escaped = html.escape(df_styled_html)
You can then pass df_styled_html_escaped
into the yagmail function using yag.send(contents=df_styled_html_escaped)
.
This function sends an email which begins with the appropriate <style type="text/css">
. Unfortunately it is printed at the top of the email body, rather than interpreted and rendered as styled html.
Just in case it wasn't clear: it is email that does not allow <style>
tags, not yagmail. So the CSS has to be on the nodes themselves rather than in a separate <style>
tag. You could perhaps write a function that applies the styling onto the nodes in your html? Or am I the one misunderstanding what you've tried?
Hi, I have such similar question.
The same html string with css and <style>
tags can be displayed using
msg=MIMEMultipart()
...
part=MIMEText(html_string,'html',_charset="utf-8")
msg.attach(part)
smtpObj.sendmail(_user,to_list,msg.as_string())
But
yag.send(contents=html_string)
doesn't work
like kootenpv wrote - seems like the problem is that style tags are not inline i used 'from premailer import transform' to fix the render() output and it worked example: html=df.round(2).style.bar(subset=['a', 'c'], align='mid', color=['#d65f5f', '#5fba7d']).render() html_inline=transform(html)
Just throwing it out there, this is now also possible:
yag.send(contents="test\ntest", newline_to_break=False)
Closing, but feel free to open.
Hi - thanks for building this module.
Pandas 0.18 includes a style attribute that makes dataframes pretty. When rendered in html for inclusion in an email using render, the dataframe is nicely displayed inline as html and can be saved to an html file on the local path.
yagmail is able to send an email form a notebook, but the resulting message body contains degraded versions of the html dataframe rendering, irrespective of the method with which it was added to the yagmail code.
Code below shows an example of a styled dataframe which looks nice in a notebook. It is then converted to html and attempts are made to embed the html in-line with fidelity to the original styling. All attempts fail.
When df_styled_html is passed, we get a CSS string in the body of the email. Perhaps the email code is not appplying CSS styules to the dataframe natively?
Thanks.