发送带有python MIME的pdf附件的电子邮件失败



我使用MIME发送带有附件的电子邮件,代码运行时没有错误,但没有发送电子邮件。我使用编解码器编码来防止先前的错误(MIMEText不能识别文档中的字节,没有这种编码)- UnicodeDecodeError: 'utf8'编解码器不能解码字节0x9c。我在python 3.6.4和Windows 10中运行它

import smtplib
import os, sys
import xlrd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import codecs
gmail_user = input("Your gmail: ")
gmail_password = input("Your password: ")
print("sending...")
example = "email test"
msg = MIMEMultipart()
msg['Subject'] = sheet.cell_value(l,1)
msg['From'] = gmail_user
msg['To'] = email_reciver
body = MIMEText(texto)
msg.attach(body)
pod = os.getcwd() + "doc.pdf"
with codecs.open(pod, 'r', encoding='utf-8', errors='ignore') as fp:
record = MIMEText(fp.read())
record['Content-Disposition'] = 'attachment; filename="doc.pdf"'
msg.attach(record)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
print(l)
server.sendmail(gmail_user, email_reciver, msg.as_string())
server.close()
except:
print ("something went wrong")

我不明白发生了什么变化,但我正在运行一个测试:

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
print(l)
r = server.sendmail(gmail_user, email_reciver, msg.as_string())
print("__________________")
print(r)
server.close()

它起作用了。r输出是{},如果我去掉print("_____________")print(r),它就停止工作了。代码

中没有其他更改

相关内容

  • 没有找到相关文章

最新更新