Python bulk Email-plus attachment/CC

Source: Internet
Author: User
Tags assert

# !/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)



    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.