2017-01-12 7 views
0

if에 설정된 기준을 충족하지 못한 사람에게 전자 메일을 보냅니다.첨부 파일이있는 전자 메일을 목록/배열에 보내고 목록의 마지막 사람에게만 보냄

기준을 충족하는 파일의 다른 사용자에게 이메일을 보내지 않는 이유를 이해할 수 없습니다.

import smtplib, openpyxl, sys from email.mime.multipart 
import MIMEMultipart from email.mime.text 
import MIMEText from email.mime.base 
import MIMEBase from email 
import encoders  
wb = openpyxl.load_workbook('Book1.xlsx') 
sheet = wb.get_sheet_by_name('Sheet1')  

lastCol = sheet.max_column 
latestMonth = sheet.cell(row=1, column=lastCol).value 

unpaidMembers = {} 
for r in range(2, sheet.max_row + 1): 
    payment = sheet.cell(row=r, column=lastCol).value 
    if payment != 'Y': 
     name = sheet.cell(row=r, column=1).value 
     email = sheet.cell(row=r, column=2).value 
     unpaidMembers[name] = email 

fromaddr = "[email protected]" 

msg = MIMEMultipart() 

msg['From'] = fromaddr 
msg['To'] = email 
msg['Subject'] = "Hi" 

body = "Hello, This is a test message. Please check attachment" 

msg.attach(MIMEText(body, 'plain')) 

filename = "xxxxx.pdf" 
attachment = open("\\\xxxx.pdf","rb") 

part = MIMEBase('application', 'octet-stream') 
part.set_payload((attachment).read()) 
encoders.encode_base64(part) 
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) 

msg.attach(part) 

smtp0bj = smtplib.SMTP('smtp-mail.outlook.com', 587) 
smtp0bj.ehlo() 
smtp0bj.starttls() 
smtp0bj.login(fromaddr, 'xxxx') 
text = msg.as_string() 
smtp0bj.sendmail(fromaddr, email, text) 
smtp0bj.quit() 

답변

0

email은 for 루프의 마지막 값으로 설정하고는 마지막 사람에게 이메일을 보내는 이유입니다됩니다.

... 
... 
recipients = [] 
unpaidMembers = {} 
for r in range(2, sheet.max_row + 1): 
    payment = sheet.cell(row=r, column=lastCol).value 
    if payment != 'Y': 
     name = sheet.cell(row=r, column=1).value 
     email = sheet.cell(row=r, column=2).value 
     unpaidMembers[name] = email 
     recipients.append(email) 

... 
... 
msg["To"] = ", ".join(recipients) 
... 
... 
smtp0bj.sendmail(fromaddr, recipients, text) 

는 SO 자세한 내용은 게시이의 답변에 따라 : How to send email to multiple recipients using python smtplib?

+0

감사

은 당신이해야 할 것은 사용 후 해당 정보를 수신자 목록을 작성하고있다. 그것은 완벽하게 작동했습니다. 그러나 한 번에 한 명의 수신자를 제외하고 모든 사람에게 한 번에 메일을 보내고 싶지 않습니다. 즉 수신자 당 하나의 메일. 나는 당신이 올린 글을 읽을 것입니다. – Sid