无法在python中将html文件内容作为电子邮件正文发送

时间:2017-03-28 22:25:42

标签: python html smtp

我有一个html文件,其内容我想使用python在电子邮件正文中发送。下面是python脚本:

import smtplib,email,email.encoders,email.mime.text,email.mime.base
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import codecs

msg = MIMEMultipart()
# me == my email address
# you == recipient's email address
me = "a@b.com"
you = "b@a.com"


# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('mixed')
msg['Subject'] = "Automation Testing"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
f = codecs.open('/Users/test.htm')
html =  f.read()

part2 = MIMEText(html, 'html')


# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.

msg.attach(part2)

composed = msg.as_string()

fp = open('msgtest.txt', 'w')
fp.write(composed)

# Credentials (if needed)  
# The actual mail send  
server = smtplib.SMTP('smtp.a.com', 25)  
server.starttls()
server.sendmail(me, you, composed)  
server.quit()  
fp.close()

但是,它只发送一封没有html内容的空电子邮件。请告诉我如何解决这个问题。

0 个答案:

没有答案