0
votes
    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()

hi i have a problem with the email with attachment for statement.

the results of below def mail(), multiple emails was sent to one person. (<- this is a error) I want to send a each specific reciever with specific multiple attachments only once

why multiple email sented with diffrent number of attachements to one person. the reciever have the 2 emails with a 1 attachment and, simultaneouly 2 attachment. I want to send a email containg 2 attachments please help me.

*CF) path containg thoes files: ['2221_sss_love.pdf', '2221_sss_happy.pdf', '2221_ddd_sad.pdf', '2221_ddd_lucky.pdf', 'con1.xlsx']

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


*CF) con1.xlsx file contenxt:

unyong email sss 111@aaa sss 777@bbb ddd 666@sss ddd 444@ccc

1

1 Answers

0
votes

The code is sending an email for each attachment. By dedenting the mail-sending code, an email will be sent for each set of attachments grouped by 'ddd' or '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()

The recipient selection code

recipients = df.loc[unyong_nm[i],'email']
send_to = ",".join(recipients)

might need to be changed, I can't tell because the contents of the dataframe aren't provided in the question.