bas*_*ann 8 python embed html-table html-email
我试图嵌入并发送创建的html表pandas .to_html.
我很高兴将df直接发送到电子邮件或文件.
到目前为止,我可以使用以下内容嵌入图像:
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(attachment))
msg.attach(img)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个附加df但不是因为某些原因嵌入的内容.
fp = open(att1, 'r')
img = MIMEText(fp.read(),'html')
fp.close()
img.add_header('Content-ID', '<att1>')
msg.attach(img)
Run Code Online (Sandbox Code Playgroud)
或者,我可以将df数据发送到电子邮件,但是作为无格式文本发送,似乎无法使用带有简单边框的格式化(即)表格来解决这种方法.
df = dFrame(q2)
tbl = '{df}'
tbl = df.to_html(index=False,justify='center')
msgText = MIMEText('<b>%s</b><br><html src="cid:%s"><br>' % (body, tbl), 'html')
msg.attach(msgText)
Run Code Online (Sandbox Code Playgroud)
嵌入式图像的更完整代码,我希望为嵌入html表调整.
def sndFile1():
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
att1 = path + 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart()
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
msgText = MIMEText('<b>%s</b><br><html src="cid:%s"><img src="cid:%s"><img src="cid:%s"><br>' % (body, att1, att2, att3), 'html')
msg.attach(msgText)
fp = open(att1, 'r')
img = MIMEText(fp.read(),'html')
fp.close()
img.add_header('Content-ID', '<att1>')
msg.attach(img)
fp = open(att2, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
fp = open(att3, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.login(myUID,myPASS)
s.sendmail(myEml,myRec, msg.as_string())
Run Code Online (Sandbox Code Playgroud)
...这是我的最终代码,根据 所有工作的解决方案对msgText进行一些小调整!谢谢
def sndFile1():
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
att1 = path + 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart()
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
fp = open(att1, 'r')
html = fp.read()
fp.close()
msgText = MIMEText('<b>%s</b><br><%s><br><img src="cid:%s"><br><img src="cid:%s"><br>' % (body, html, att2, att3), 'html')
msg.attach(msgText)
with open(att2, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
with open(att3, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.login(myUID,myPASS)
s.sendmail(myEml,myRec, msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)
我修改了您提供的片段。主要变化是不使用 html 数据帧作为附件,而是将其插入消息正文中。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
with open('df.html', 'w') as fil:
df.head().to_html(fil)
att1 = 'df.html'
att2 = path + 'Indices.png'
att3 = path + 'Segments.png'
subject = 'Market Update'
body = 'This Weeks Report'
msg = MIMEMultipart('alternative')
msg["To"] = myEml
msg["From"] = myEml
msg["Subject"] = subject
with open(att1, 'r') as fp:
html = fp.read()
msgText = MIMEText('<b>%s</b><br>%s<img src="cid:%s"><br><img src="cid:%s"><br>' % (body, html, att2, att3), 'html')
msg.attach(msgText)
with open(att2, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att2))
msg.attach(img)
with open(att3, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(att3))
msg.attach(img)
s = smtplib.SMTP_SSL(mySMTP, smtpPORT)
s.ehlo()
s.starttls()
s.login(username,password)
s.sendmail(me,you, msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)