【发布时间】:2019-08-01 08:56:37
【问题描述】:
我正在编写一个自动化 Python 脚本。我的目的是将多个唯一附件通过电子邮件发送给多个唯一收件人。例如,我有 1000 条独特的报表,必须通过电子邮件发送给 1000 位独特的客户。我希望我的 Python 脚本能够自动选择附件并将其发送给正确的收件人!
我已经创建了脚本并创建了 pdf 附件并在收件人的每个电子邮件地址之后命名它们,以便我可以使用名称来选择附件并将其与收件人的电子邮件相匹配。 它完美地选择了附件,但它在每次迭代中不断增加对用户的附件的问题..
#1.READING FILE NAMES WHICH ARE EMAIL ADDRESS FOR RECEPIENTS
import os, fnmatch
filePath = "C:/Users/DAdmin/Pictures/"
def readFiles(path):
fileNames =fnmatch.filter(os.listdir(path), '*.pdf')
i=0
pdfFilesNamesOnly=[]
while i < len(fileNames):
s =fileNames[i]
removeThePdfExtension= s[0:-4]
pdfFilesNamesOnly.append(removeThePdfExtension)
i+=1
return pdfFilesNamesOnly
-----------------------------------------------------------
#2.SENDING AN EMAIL WITH UNIQUE ATTACHMENT TO MULTIPLE UNIQUE RECEPIENTS
import smtplib
import mimetypes
from optparse import OptionParser
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
import os,fnmatch
from readFileNames import readFiles
filePath = "C:/Users/DAdmin/Pictures/"
listOfEmails= readFiles(filePath)# Files are named after emails
def sendEmails(listOfFiNames): #sends the email
email = 'goddietshe@gmail.com' # Your email
password = '#####' # Your email account password
send_to_list = listOfEmails# From the file names
#creating a multipart object
subject ="MONTHLY STATEMENTS"
mg = MIMEMultipart('alternative')
mg['From'] = email
mg['To'] = ",".join(send_to_list)
mg['Subject'] = subject
mg.attach(MIMEText("Please receive your monthly statement",'plain'))
# Attaching a file now before emailing. (Where l have a problem)
for i in listOfEmails:
if i in send_to_list:
newFile = (i+'.pdf') # create the name of the attachment to email using the name(which is the email) and will be used to pick the attachment
with open(newFile,'rb') as attachment:
part = MIMEBase('application','x- pdf')
part.set_payload(attachment.read())
attachment.close()
encoders.encode_base64(part)
part.add_header('Content- Disposition','attachment; filename="%s"' % newFile )
mg.attach(part)
text =mg.as_string() # converting the message obj to a string/text obj
server = smtplib.SMTP('smtp.gmail.com', 587) # Connect to the server
server.starttls() # Use TLS
server.login(email, password) # Login to the email server
# this is where it is emailing stuff
server.sendmail(email, i , text) # Send the email
server.quit() # Logout of the email server
print("The mail was sent")
#print("Failed ")
sendEmails(listOfFiNames)
我希望它能自动将每个唯一附件通过电子邮件发送给 1000 个唯一收件人
【问题讨论】:
-
“但它在每次迭代中不断增加对用户的依恋的问题......” - 你能详细说明一下吗?我不明白你的意思。
-
啊,我想我知道了。您正在使用相同的
mg对象,而不是删除以前的附件。 -
使用
mg.set_payload替换内容 - 但请记住,您必须添加文本内容以及新附件! -
感谢您的快速反馈。但是我还是不清楚你刚才说了什么?!