使用Python将内联HTML发送到Gmail
我的目标是发送一封带有购买收据的电子邮件。我有一个.html文件,其中包含要作为收据发送的html。根据我所遵循的教程,我编写了以下代码:使用Python将内联HTML发送到Gmail,python,email,smtp,Python,Email,Smtp,我的目标是发送一封带有购买收据的电子邮件。我有一个.html文件,其中包含要作为收据发送的html。根据我所遵循的教程,我编写了以下代码: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText host = "smtp.gmail.com" port = 587 username = "****@gmail.com&quo
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
host = "smtp.gmail.com"
port = 587
username = "****@gmail.com"
password = "*********"
from_email = username
to_list = ["*****@gmail.com"]
try:
email_conn = smtplib.SMTP(host, port)
email_conn.ehlo()
email_conn.starttls()
email_conn.login(username,password)
the_msg = MIMEMultipart("alternative")
the_msg['Subject'] = "Hello there!"
the_msg["From"] = from_email
plain_txt = "Testing the message"
html_txt = """\
------some big html code-------
part_1 = MIMEText(plain_txt,'plain')
part_2 = MIMEText(plain_txt, "html")
the_msg.attach(part_1)
the_msg.attach(part_2)
email_conn.sendmail(from_email, to_list, the_msg.as_string())
email_conn.quit()
except smtplib.SMTPException:
print("Error sending message.")
但是,我在运行代码时遇到了问题。错误是
第27行文件send_email.py中的非ASCII字符“\xc2”,但未声明编码;有关详细信息,请参阅
我在网上搜索并添加了
#-*-编码:utf-8-*-
在我的.py
文件的顶部。现在,我可以发送邮件,但我没有收到任何嵌入在邮件中的收据。有人能帮忙吗?我正在使用Python2.x