python电子邮件多个接收者,多个错误附件

2024-09-30 14:16:54 发布

您现在位置: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文件上下文:

安永电子邮件 sss111@aaa sss777@bbb ddd666@sss ddd444@ccc


Tags: infromimportsendfor附件pdfemail
1条回答
网友
1楼 · 发布于 2024-09-30 14:16:54

代码为每个附件发送一封电子邮件。通过删除邮件发送代码,将为按“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)

可能需要更改,我不知道,因为问题中没有提供数据帧的内容

相关问题 更多 >

    热门问题