您好,登錄后才能下訂單哦!
# !/usr/bin/env python # -*- coding: UTF-8 -*- import csv from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.utils import COMMASPACE, formatdate from email.mime.base import MIMEBase from email import encoders import time def 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) xlsxpart = MIMEBase("application", "msword") xlsxpart.set_payload(open(u'通知.docx','rb').read(), 'utf-8') xlsxpart.add_header('Content-Disposition', u'p_w_upload', filename="通知.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("開始") for i in l: name = i[0] mail = i[2:10] chao = i[1] b = ''' 您好! 值此“八一”建軍節之際,祝愿貴公司蓬勃發展,建軍節快樂! ''' a = "尊敬的{0}:".format(name) text = a + b while '' in mail: mail.remove('') to = mail print('to', to, 'ok', 'chao', chao, 'ok') time.sleep(7) send_mail(server, fro, to, subject, text, chao)
1.csv 的格式
第一列是客戶名稱,第二列是抄送的人,第三列和后面的是要發送的人。
msg['To'] = COMMASPACE.join(to) # COMMASPACE==', ' msg['Cc'] = chao # COMMASPACE==', '
smtp.sendmail(fro, to+[chao], msg.as_string())
需要特別注意的是上面,抄送的是 1個人。
Cc 是指要抄送,要注意抄送的是一個人還是多個。如果是多個,需要按照to的格式。
本腳本有BUG,就是收附件的時候,如果附件是中文名字,手機客戶端收的時候會顯示一個 未命名。電腦客戶端沒有問題。
暫未解決。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。