python邮件多收件人、多附件出错的问题

问题描述 投票:0回答:1
    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) path containg thoes files:[2221_sss_love.pdf, '2221_sss_happy.pdf, '2221_ddd_sad.pdf'.

*2221_sss_love.pdf', '2221_sss_happy.pdf', '2221_ddd_sad.pdf', '2221_ddd_lucky.pdf', 'con1.xlsx'] 的路径。

*resultunyong_nm = ['sss','ddd']filenm = ['2221_sss_love.pdf', '2221_sss_happy.pdf', '2221_ddd_sad.pdf', '2221_ddd_lucky.pdf']。


*CF)con1.xlsx文件contenxt。

unyong邮箱sss 111@aaasss 777@bbbddd 666@sssddd 444@ccc。

python email attachment
1个回答
0
投票

这段代码是为每个附件发送一封邮件。 通过演绎邮件发送代码,将为每一组按'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)

可能需要修改,我无法判断,因为问题中没有提供数据框的内容。

© www.soinside.com 2019 - 2024. All rights reserved.