使用Python将内联HTML发送到Gmail

时间:2019-03-11 06:08:03

标签: python email smtp

我的目标是发送包含购买收据的电子邮件。我有一个 .html 文件,该文件具有要作为收据发送的html。在我遵循的教程中,我编写了以下代码:

mysql -u username -p    
mysql> use db_name;
mysql> source path-to/file.sql;

但是,我在运行代码时遇到问题。错误显示

  

第27行的send_email.py文件中的非ASCII字符'\ xc2',但未声明编码;有关详情,请参见http://python.org/dev/peps/pep-0263/

我在线搜索并添加了

  

import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText host = "smtp.gmail.com" port = 587 username = "techieguy188@gmail.com" password = "Araf920sb" from_email = username to_list = ["bistasulove@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.")

在我的# -*- coding: utf-8 -*-文件顶部的

。现在,我可以发送邮件了,但是邮件中没有任何嵌入式收据。有人可以帮忙吗?我正在使用Python 2.x

0 个答案:

没有答案