從excel中讀取用戶數據發送email
阿新 • • 發佈:2018-06-17
img info 發送郵件 可恥 mage color problem item sendmai
from openpyxl import load_workbook import smtplib from email.mime.text import MIMEText wb = load_workbook("H:\項目\Test\發送郵件\email1.xlsx") sheet = wb.get_sheet_by_name(‘Sheet1‘) lastCol = sheet.max_column lastestMonth = sheet.cell(row =1,column =lastCol).value unpadiMembers = {} for r in range(2,sheet.max_row+1): payment = sheet.cell(row=r ,column=lastCol) if payment != "": name = sheet.cell(row=r ,column=1).value email = sheet.cell(row=r ,column=2).value if email != None: unpadiMembers[name]= email smtpObj = smtplib.SMTP() smtpObj.connect(‘smtp.163.com‘,) smtpObj.login(‘[email protected]‘,‘huyang123‘) for name,email in unpadiMembers.items(): body =‘{}黨費什麽時候交{}‘.format(name,lastestMonth) print(‘Sending email to {}‘.format(email)) msg = MIMEText(body,‘html‘,‘utf-8‘) msg[‘Form‘] = ‘[email protected]‘ msg[‘To‘] = email msg[‘Subject‘] = ‘懶惰是可恥的‘ sendemailStatus = smtpObj.sendmail(‘[email protected]‘,‘[email protected]‘,msg.as_string()) if sendemailStatus !={}: #不空代表有錯誤 print(‘There was a problem sending email to %s‘%sendemailStatus) smtpObj.quit()
測試結果
從excel中讀取用戶數據發送email