gpt4 book ai didi

python - 未收到 SMTPlib 附件

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:22 26 4
gpt4 key购买 nike

我一直在使用基于 http://datamakessense.com/easy-scheduled-emailing-with-python-for-typical-bi-needs/ 上的代码片段的代码通过我公司的邮箱给客户发送PDF附件。我们通过一个电子邮件地址(“no-reply@companyname.com”)一次发送大约 100 封这样的电子邮件,对于发送的每封电子邮件,我也会将密件抄送副本发送到一个内部电子邮件地址(“reports@公司名称.com”)。

有时(大约 100 次中有 5 次),客户报告没有收到附件。有时根本不显示,有时显示时带有红色问号。但是,BCC 副本总是有附件没有问题,进入发送帐户,电子邮件的发送副本总是显示附件,也没有问题。未收到附件的客户电子邮件中没有明显的相似之处(例如共享域;事实上,大多数是@gmail.com)。没有要报告的异常或错误。一切看起来都在正常工作。

这是我第一次使用 MIME 或通过 Python 自动发送电子邮件,但它在 98% 的时间里都能正常工作这一事实让我感到困惑。是否有可能发生这种情况的已知原因?也许我没有正确设置类型?或者我应该为 Gmail 使用 MIME 做些什么?

这是我的代码:

wdir = 'PDFs\\'
filelist = []
for file in os.listdir(wdir):
if file.endswith('.pdf'):
filelist += [wdir + file] # sending all of the PDFs in a local directory

email = {}
rf = wdir + 'Reports_data.csv' # get email addresses for customers by ID (row[2])
with open(rf, 'rbU') as inf:
read = csv.reader(inf)
read.next()
for row in read:
email[row[2]] = row[3]

hfi = open('HTML\\email.html', 'rb') # the HTML for the email body, itself
htmltxt = hfi.read()
hfi.close()


class Bimail:
def __init__(self, subject, recipients):
self.subject = subject
self.recipients = recipients
self.htmlbody = ''
self.sender = "foo@bar.com"
self.senderpass = 'password'
self.attachments = []

def send(self):
msg = MIMEMultipart('alternative')
msg['From'] = self.sender
msg['Subject'] = self.subject
msg['To'] = self.recipients[0]
msg.preamble = "preamble goes here"
if self.attachments:
self.attach(msg)
msg.attach(MIMEText(self.htmlbody, 'html'))
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(self.sender, self.senderpass)
s.sendmail(self.sender, self.recipients, msg.as_string())
s.quit()

def htmladd(self, html):
self.htmlbody = self.htmlbody + '<p></p>' + html

def attach(self, msg):
for f in self.attachments:
ctype, encoding = mimetypes.guess_type(f)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fn = f.replace(wdir, '')
fp = open(f, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fn)
attachment.add_header('Content-ID', '<{}>'.format(f)) # or should this be format(fn)?
msg.attach(attachment)

def addattach(self, files):
self.attachments = self.attachments + files


if __name__ == '__main__':
for fi in filelist:
code = fi.split('_')[1].split('\\')[1] # that "ID" for email is in the filename
addr = email[code]
mymail = Bimail(('SUBJECT HERE'), [addr, 'reports@ourcompany.com'])
mymail.htmladd(htmltxt)
mymail.addattach([fi])
mymail.send()

最佳答案

试试这个代码块:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

fromaddr = "from@yourcompany.com"
password = "password"
toaddr = "to@yourcompany.com"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Report"

body = "Hi, have a look at the Report"

msg.attach(MIMEText(body, 'plain'))

filename = "Report.pdf"
attachment = open("Report.pdf", "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

对我有用

关于python - 未收到 SMTPlib 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40688768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com