# !/usr/bin/env python# -*- coding: utf-8 -*-import csvfrom Email.mime.multipart import mimemultipartfrom email.mime.text import mimetextfrom email.mime.application import mimeapplicationfrom email.utils import commaspace , formatdatefrom email import encodersimport timedef send_mail (Server, fro, to, subject, text, chao): assert type (server) == Dict assert type (To) == list msg = Mimemultipart () msg[' from '] = fro msg[' Subject '] = subject msg[' to '] = commaspace.join (to) # commaspace== ', ' msg[' Cc '] = chao # commaspace== ', ' msg[' Date '] = formatdate (localtime=true) msg.attach (Mimetext (text)) Xlsxpart = mimeapplication (open (' Attachment. docx ', ' RB '). Read ()) xlsxpart.add_ Header (' content-disposition ', ' attachment ', filename= ' attachment. docx ') msg.attach ( Xlsxpart) import smtplib smtp = smtplib. SMTP (server[' name '], server[' Port ') smtp.ehlo () Smtp.starttls () smtp.ehlo () smtp.login (server[' user '], server[' passwd ') smtp.sendmail (fro, to+[chao], msg.as_string ()) smtp.close () if __name__ == ' __main__ ': server = {' Name ': ' xx.163.com ', ' user ': ' xxxxx ', ' passwd ': ' xxx ', ' port ': 25} fro = ' xxxxxxxx ' subject = ' xxxxx ' with open (' 1.csv ', ' U ') as csvfile: # reader = csv. Dictreader (CSVFile) reader = csv.reader (csvfile) l = [] for row in reader: l.append (Row) print ("Start") for i in l: name = i[0] mail = i[2:10] chao = i[1] b = ' Hello! On the occasion of the "Army Day" Army Day, wish your company vigorous development, Army Day happy! ' a = ' Dear {0}: ". Format (name) text = a + b while ' in mail: mail.remove (') to&nbsP;= mail print (' to ', to, ' OK ', ' Chao ', chao, ' OK ') time.sleep (7) send_mail (Server, fro, to, subject, text, chao)
Format of CSV
The first column is the customer name, the second column is the CC person, the third column and the person that follows is the one to send.
msg["to" = Commaspace.join (To) # commaspace== ', ' msg[' Cc '] = Chao # commaspace== ', '
Smtp.sendmail (fro, To+[chao], msg.as_string ())
Special attention is paid to the above, with a CC of 1 people.
CC means to CC, to be aware that the CC is a person or multiple. If it is multiple, you need to follow the to format.
This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1953223
Python bulk Email-plus attachment/CC