我从python脚本发送电子邮件时遇到了一个奇怪的问题。
这个完美无缺:
msg = MIMEText(body)
msg['Subject'] = 'Booking confirmation %s, %s.'% (tourname, tourdatetime.strftime('%d %b %Y, %H:%M'))
msg['From'] = FROMADDRESS
msg['To'] = email
msg['Cc'] = OWNERADDRESS
s = smtplib.SMTP(MAILSERVER)
s.sendmail(FROMADDRESS, [email, OWNERADDRESS], msg.as_string())
s.quit()
收到的电子邮件完全符合我的预期。
在同一台服务器上,以下代码段无法正常工作:电子邮件会发送给正确的收件人,附件就在那里,但邮件中的“收件人:”和“抄送:”标头丢失了接收。主题,正文和发自:设置正确。
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = FROMADDRESS
msg['To'] = data['email']
msg['Cc'] = OWNERADDRESS
msg.attach(MIMEText(body))
part = MIMEBase('application', "octet-stream")
part.set_payload(
file(
os.path.join(
directory, '../images', 'meetingpoint_%s.jpg'% (data['tourid'], ))
).read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % ('meetingpoint_%s.jpg'% (data['tourid'], ), ))
msg.attach(part)
smtp.sendmail(FROMADDRESS, [data['email'], OWNERADDRESS], msg.as_string())