我正在努力弄清楚下面的代码出了什么问题。我正在尝试发送 html 邮件。
NOW = datetime.datetime.now()
def sendEmail(msg):
global NOW
global SENDER
global EMAILTARGET
today = "%s/%s/%s" % (NOW.month,NOW.day,NOW.year)
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "SAR Data Report - %s" % today
msg['From'] = SENDER
msg['To'] = EMAILTARGET
chunk = MIMEText(msg, 'html')
msg.attach(chunk)
s = smtplib.SMTP('localhost')
s.sendmail(SENDER, EMAILTARGET, msg.as_string())
s.quit()
上面的代码给了我以下错误:
Traceback (most recent call last):
File "./html_mail.py", line 295, in <module>
sendEmail(html)
File "./html_mail.py", line 245, in sendEmail
chunk = MIMEText(msg, 'html')
File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
self.set_payload(_text, _charset)
File "/usr/lib/python2.7/email/message.py", line 226, in set_payload
self.set_charset(charset)
File "/usr/lib/python2.7/email/message.py", line 268, in set_charset
cte(self)
File "/usr/lib/python2.7/email/encoders.py", line 73, in encode_7or8bit
orig.encode('ascii')
AttributeError: MIMEMultipart instance has no attribute 'encode'