我正在尝试将PDF文件附加到电子邮件中。在
对于一个PDF(打印成PDF的Word文档),它可以工作(收件人在Outlook中打开它没有问题)。在
但是对于其他的pdf(除了大了几kb之外,看起来都一样),它们会被破坏。在
Here是要使用的示例,该示例失败(已损坏)。在import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.utils import formatdate
from email import encoders
attachment_path=r'C:\Directory'+'\\'
login='login'
password='password'
part=MIMEBase('application',"octet-stream")
def message(attachment): #attachment is just the PDF file name
fromaddr = "example@example.com"
cc=fromaddr
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = "example@example.com"
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = "Subject"
body='''
I hope everything is going well.
'''
msg.attach(MIMEText(body, 'html'))
part.set_payload(open(attachment_path+attachment,'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(attachment_path+attachment)))
msg.attach(part)
mail=smtplib.SMTP('Server',587)
mail.ehlo()
mail.starttls()
mail.login(login,password)
mail.sendmail(fromaddr,[toaddr,cc],msg.as_string())
我尝试使用以下代码代替base64编码,但没有成功:
^{pr2}$
提前谢谢!在