python发邮件,密送多人,带附件,中文正文,收信信箱从excel中获取

python发邮件,密送多人,带附件,中文正文,收信信箱从excel中获取,第1张

概述python发邮件,密送多人,带附件中文正文,收信信箱从excel中获取

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#脚本文件用utf-8格式编码import os  import smtplib  import mimetypes  import xlrd from email.MIMEMultipart import MIMEMultipart  from email.MIMEBase import MIMEBase  from email.MIMEText import MIMEText  from email.MIMEAudio import MIMEAudio  from email.MIMEImage import MIMEImage  from email import encodersfrom email.utils import parseaddr,formataddrfrom email.header import headerfrom email.Encoders import encode_base64from email.utils import COMMASPACEsubject = u'团队工作简报第144期' #邮件主题content_file_path = u"E:\send_Dian.txt".encode("gb2312") #邮件正文内容,用utf-8无bom格式编码path = u"E:\测试邮箱列表.xls".encode("gb2312") #发送邮件列表file_path = u"E:\Newsletter_20150916_147.pdf".encode("gb2312") #附件email_name = u'E-mail Address' #发送邮件列表中邮箱信息列头content_file = open(content_file_path)content = content_file.read()def _format_addr(s):    name,addr = parseaddr(s)    return formataddr(( \        header(name,'utf-8').encode(),\        addr.encode('utf-8') if isinstance(addr,unicode) else addr))def sendMail(gmailUser,gmailPassword,subject,text,other,*attachmentfilePaths):       recipIEnt = []      cmail = []        msg = MIMEMultipart()      msg['From'] = _format_addr(u'Newsletter Dian <%s>' % gmailUser)     msg['To'] = COMMASPACE.join(recipIEnt)    msg['Cc'] = COMMASPACE.join(cmail)    msg['Subject'] = header(subject,'utf-8').encode()      msg.attach(MIMEText(content,'plain','utf-8'))        for attachmentfilePath in attachmentfilePaths:          msg.attach(getAttachment(attachmentfilePath))        mailServer = smtplib.SMTP('mail.hust.edu.cn',25)  #QQ邮箱需要改为smtp.qq.com    mailServer.ehlo()      mailServer.starttls()      mailServer.ehlo()      mailServer.login(gmailUser,gmailPassword)      mailServer.sendmail(gmailUser,recipIEnt+cmail+other,msg.as_string())      mailServer.close()        print "Sent  email  to  ",other   def getAttachment(attachmentfilePath):      ContentType,enCoding = mimetypes.guess_type(attachmentfilePath)        if ContentType is None or enCoding is not None:          ContentType = 'application/octet-stream'        mainType,subType = ContentType.split('/',1)      file = open(attachmentfilePath,'rb')        if mainType == 'text':          attachment = MIMEText(file.read())      elif mainType == 'message':          attachment = email.message_from_file(file)      elif mainType == 'image':          attachment = MIMEImage(file.read(),_subType=subType)      elif mainType == 'audio':          attachment = MIMEAudio(file.read(),_subType=subType)      else:          attachment = MIMEBase(mainType,subType)      attachment.set_payload(file.read())      encode_base64(attachment)          file.close()        attachment.add_header('Content-disposition','attachment',filename=os.path.basename(attachmentfilePath))      return attachment  def OneUsrSendMail(gmailUser,sheetNumFrom,sheetNumTo):    other = []    emailCnt = 0    #other数组里面是当前密送邮件列表,emailCnt记录提取到该密送邮件中的第几个邮箱    book = xlrd.open_workbook(path)    for sheetmun in range(sheetNumFrom-1,sheetNumTo):      #假设一共有23张表格,则应该0=<range<24      sh = book.sheet_by_index(sheetmun)      nrows = sh.nrows      clox = sh.row_values(0).index(email_name)             #根据每张表格第一行的数据取出E-mail Address所对应的列数      for i in range(1,nrows):        cell_value = sh.cell_value(i,clox)        other.append(cell_value)                            #依次取出每一行的E-mail Address,压入密送邮箱地址数组other        emailCnt = emailCnt+1        if emailCnt == 40:                                  #QQ邮箱能否收到邮件,设定每40个邮箱地址发送一份密送邮件            sendMail(gmailUser,content,file_path) #发送带附件的邮件并将计数清零,重新发送新的密送邮件            emailCnt = 0            other = []    if emailCnt != 0:                                       #最后的一封有可能不是40个地址一起发送的,要补充上去        sendMail(gmailUser,file_path)OneUsrSendMail('[email protected]','xxxxxxxx',1,3) #账号,密码,从第1张sheet发到第3张

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python发邮件,密送多人,带附件,中文正文,收信信箱从excel中获取全部内容,希望文章能够帮你解决python发邮件,密送多人,带附件,中文正文,收信信箱从excel中获取所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1199126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存