码迷,mamicode.com
首页 > 其他好文 > 详细

从excel中读取用户数据发送email

时间:2018-06-17 19:47:22      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:text   mime   技术   dem   xls   nbsp   bubuko   exce   UI   

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(hy17611499228@163.com,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] = hy17611499228@163.com
    msg[To] = email 
    msg[Subject] = 懒惰是可耻的
    sendemailStatus = smtpObj.sendmail(hy17611499228@163.com,hy17611499228@163.com,msg.as_string())
    if sendemailStatus !={}:  #不空代表有错误
        print(There was a problem sending email to %s%sendemailStatus)
        
smtpObj.quit()        

技术分享图片

测试结果

技术分享图片

 

从excel中读取用户数据发送email

标签:text   mime   技术   dem   xls   nbsp   bubuko   exce   UI   

原文地址:https://www.cnblogs.com/huyangblog/p/9193411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!