Python电子邮件以HTML格式mimelib [英] Python Email in HTML format mimelib

查看:221
本文介绍了Python电子邮件以HTML格式mimelib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送一个在Pandas Python中创建的两个数据帧,作为从python脚本发送的电子邮件中的html格式。

I am trying to send two dataframes created in Pandas Python as a html format in an email sent from the python script.

我想写一个文本和表并重复此两个数据帧,但脚本不能附加多个html块。
代码如下:

I want to write a text and the table and repeat this for two more dataframes but the script is not able to attach more than one html block. The code is as follows:

import numpy as np
import pandas as pd
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

sender = "blabla@gmail.com"
recipients = ['albalb@gmail.com']
msg = MIMEMultipart('alternative')
msg['Subject'] = "This a reminder call " + time.strftime("%c")
msg['From'] = sender
msg['To'] = ", ".join(recipients)

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df[['SYMBOL','ARBITRAGE BASIS %']].to_html()

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

username = 'blabla@gmail.com'
password = 'blahblah'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(sender, recipients, msg.as_string())
server.quit()        
print("Success")

我正在收到一封电子邮件,只是最后一部分作为电子邮件正文中格式化的html表。第1部分文字未出现。什么问题?

I am getting an email with just the last part as a formatted html table in the email body. The part 1 text is not appearing. What's wrong?

推荐答案

使用 yagmail (全面披露:我是维护者/开发人员):

Use yagmail (full disclose: I'm the maintainer/developer):

import time
import yagmail
yag = yagmail.SMTP(username, password)

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = df[['SYMBOL','ARBITRAGE BASIS %']].to_html()

yag.send('albalb@gmail.com', "This a reminder call " + time.strftime("%c"), [text,html])

yagmail存在,使我们很容易使用附件,图像和html等邮件发送电子邮件。

yagmail exists to make it really easy for us to send emails using attachments, images, and html among other things.

开始使用

pip install yagmail

这篇关于Python电子邮件以HTML格式mimelib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆