从excel中读取用户数据发送email
Posted huyangblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从excel中读取用户数据发送email相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
在 MATLAB App Designer 中将数据从 excel 读取到表中
python读取excel一例-------从工资表逐行提取信息