python - python电子邮件多个接收者,错误的多个附件
问题描述
def mail():
import os
import pandas as pd
import smtplib
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email.mime.multipart import MIMEMultipart
from email import encoders
from PyQt5.QtCore import QDate, Qt
path = 'C:/Users/user/Desktop/pdf/'
contact = 'con1.xlsx'
df = pd.read_excel(str(path)+contact, endcoding ='utf8')
df.set_index('unyong', inplace = True)
now = QDate.currentDate()
filenm = [f for f in os.listdir(path) if f.endswith('.pdf')]
unyong_nm = []
for w in filenm:
a = w.find('_')
b = w.rfind('_')
unyong_nm.append(w[a+1:b])
unyong_nm = list(set(unyong_nm))
for i in range(0,len(unyong_nm)):
send_from = 'ss@ddd'
recipients = df.loc[unyong_nm[i],'email']
send_to = ",".join(recipients)
attach = [s for s in filenm if s.find(unyong_nm[i]) >-1 ]
username = 'sss@ssss'
password = 'sss'
subject = ('111'+now.toString('yyyy.MM')+'_'+unyong_nm[i]+str(i+1))
text = ('hi')
msg = MIMEMultipart()
msg['From'] = send_from
msg['To']= send_to
msg['Subject'] = subject
msg['Date']=formatdate(localtime=True)
filename_match = [s for s in filenm if s.find(unyong_nm[i]) >-1 ]
for file in filename_match:
part =MIMEBase('application','octet-stream')
part.set_payload(open(str(path)+file, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment', filename =file)
msg.attach(part)
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP("smtp.sssss.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username,password)
mailServer.sendmail(send_from, send_to, msg.as_string())
mailServer.close()
嗨,我对带有声明附件的电子邮件有疑问。
下面 def mail() 的结果,多封邮件被发送给一个人。(<- 这是一个错误)我只想发送一个带有特定多个附件的每个特定接收者一次
为什么多封电子邮件以不同数量的附件发送给一个人。收件人有 2 封带有 1 个附件的电子邮件,同时有 2 个附件。我想发送一封包含 2 个附件的电子邮件,请帮助我。
*CF) 包含文件的路径:['2221_sss_love.pdf', '2221_sss_happy.pdf', '2221_ddd_sad.pdf', '2221_ddd_lucky.pdf', 'con1.xlsx']
*结果 unyong_nm = ['sss','ddd'] filenm = ['2221_sss_love.pdf', '2221_sss_happy.pdf', '2221_ddd_sad.pdf', '2221_ddd_lucky.pdf']
*CF) con1.xlsx 文件内容:
unyong 电子邮件 sss 111@aaa sss 777@bbb ddd 666@sss ddd 444@ccc
解决方案
该代码正在为每个附件发送一封电子邮件。通过删除邮件发送代码,将为按“ddd”或“sss”分组的每组附件发送一封电子邮件。
for file in filename_match:
part = MIMEBase("application", "octet-stream")
part.set_payload(open(file, "rb").read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment", filename=file)
msg.attach(part)
msg.attach(MIMEText(text))
# Send mail outside the file grouping loop
mailServer = smtplib.SMTP("localhost", 1025)
mailServer.ehlo()
mailServer.sendmail(send_from, send_to, msg.as_string())
mailServer.close()
收件人选择代码
recipients = df.loc[unyong_nm[i],'email']
send_to = ",".join(recipients)
可能需要更改,我无法确定,因为问题中未提供数据框的内容。
推荐阅读
- javascript - 将 Promise 用于不被 JavaScript 调用的函数
- matlab - 如何使用 Matlab 在单个概率图中使用不同的标记颜色?
- c++ - std::byte 不是“std”的成员
- python - 如何退货?
- javascript - 使用数组和基本 js 知识创建 SPA 井字游戏
- typescript - 强制字符串类型成为 TypeScript 中字符串数组的一部分
- php - HTML/PHP 按月用颜色分组表
- java - Kubernetes 上的 Apache Ignite 与 TcpDiscoverySharedFsIpFinder:集群似乎解体了
- .net-core - 带有 .Net Core 的 AWS Secret Manager 引发套接字异常
- android-studio - 将 build.gradle 从 2.3.3 更新到 3.4.1,但我仍然收到重新“编译”被“实现”替换的警告?