python邮件发送pdf_Python电子邮件PDF:一些PDF文件被破坏

我正在尝试将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}$

提前谢谢!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值