资料
https://www.thepythoncorner.com/2019/10/the-detailed-guide-on-sending-emails-from-your-python-app/
https://blog.mailtrap.io/sending-emails-in-python-tutorial-with-code-examples/
https://realpython.com/python-send-email/
http://www.pybloggers.com/2018/12/sending-emails-with-python/
https://www.runoob.com/python3/python3-smtp.html
https://docs.python.org/3/library/email.examples.html#email-examples
https://docs.python.org/3.7/library/email.message.html
https://glowingpython.blogspot.com/2011/05/how-to-use-strings-template.html
https://www.python.org/dev/peps/pep-0292/
企业邮箱常见的错误提示
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
163
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
https://github.com/leemunroe/responsive-html-email-template
终版发邮件
(联系人contacts.csv、多语言模板mail_template_x.html、多个不同类型的附件)文件/E:. │ python-send-mail.py │ ├─attachment │ Ansible From Beginner to Pro.pdf.pdf │ 鼠年.jpg │ ├─contact │ contacts.csv │ ├─mail_template │ mail_template_cn.html │ mail_template_en.html │ mail_template_jp.html │ └─subject subjects.txtcontacts.csv name,email,country DT,2818686037@qq.com,CN JOLIN,jk.d@aisino.sh.cn,JP admin,solopythonlearning@gmail.com,XX mail_template.txt : thanks for your order! We are processing it now and will contact you soon.
mail_template_en.html<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <p>Hi $PERSON_NAME:<br> thanks for your order! We are <strong>processing</strong> it now and will contact you soon.</p> <p> <a href="https://blog.example.com/send-email-python/">How to Send Emails with Python?</a> </p> </body> </html>
mail_template_cn.html<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <p>$PERSON_NAME,您好 :<br> 感谢您的订单!我们正在<strong>处理</strong>它,并将尽快与您联系。</p> <p> <a href="https://blog.example.com/send-email-python/">如何使用Python发送电子邮件?</a> </p> </body> </html>
mail_template_jp.html<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <p>こんにちは$PERSON_NAME:<br> ご注文ありがとうございます! 現在<strong>処理</strong>中です。間もなくご連絡いたします。</p> <p> <a href="https://blog.example.com/send-email-python/">Pythonでメールを送信する方法は?</a> </p> </body> </html>
subjects.txt{"CN": "订单确认", "JP": "注文確認", "EN": "Order confirmation"}
源码 mail.py 名称中不能出现导入的类名譬如python-send-email.py
import os, csv, json, smtplib from os.path import join, dirname, abspath, basename from string import Template from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders smtp_host = 'smtp.aisino.sh.cn' port = 25 # 发件人邮箱账号和密码 MY_ADDRESS = 'jk.d@aisino.sh.cn' PASSWORD = 'xxx' # 抄送:收件人能看到抄送人,抄送人也能看到收件人 # 英文是cc(Carbon copy) CC = '744351600@qq.com' # 密送:收件人和抄送人都看不到密送人,密送人能看到收件人和抄送人 # 英文是bcc(blind carbon copy) BCC = 'jk.d@aisino.sh.cn' # 邮件计数 count = 0 # 根据当前目录获取联系人、邮件模板、附件、主题目录 base_dir = dirname(abspath(__file__)) contact_path = join(base_dir, 'contact') template_path = join(base_dir, 'mail_template') attachment_path = join(base_dir, 'attachment') subject_path = join(base_dir, 'subject') def get_contacts(filename): """ 返回三个列表名称:names、emails、countrys 从文件名指定的文件中读取 """ names = [] emails = [] countrys = [] with open(filename, mode='r', encoding='utf-8') as contacts_file: reader = csv.reader(contacts_file)# 调试 #for row in reader: # print(row)# 跳过标题行 next(reader) for name, email, country in reader: names.append(name) emails.append(email) countrys.append(country) return names, emails, countrys def read_template(filename): """ 返回包含以下内容的Template对象 文件名指定的文件 """ with open(filename, mode='r', encoding='utf-8') as template_file: template_file_content = template_file.read() return Template(template_file_content) def add_attachment(filelist): """ 返回一个附件列表:parts """ parts = [] for filename in filelist: # 附件位于该Python脚本的目录中 # 以二进制模式打开附件 with open(filename, mode="rb") as attachment: # 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件 part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) # base64编码 encoders.encode_base64(part) # 添加头参数,gbk支持中文附件名 part.add_header( 'Content-Disposition', # f"attachment; filename= {filename}", 'attachment', filename=('gbk', '', basename(filename)) ) parts.append(part) return parts def get_subject(filename): """ 返回一个主题字典:subject """ with open(filename, mode='r', encoding='utf-8') as subject_file: subject = json.load(subject_file) return subject def main(): # 获取联系人信息 names, emails, countrys = get_contacts(join(contact_path, 'contacts.csv')) # 获取主题 SUBJECT = get_subject(join(subject_path, 'subjects.txt')) # message_template = read_template('mail_template.html') # 添加任意类型(pdf、office、rar、zip、jpg等)附件并存放入列表list_attachment,支持中文 # attachment_list = ['Ansible From Beginner to Pro.pdf', 'year-of-the-rat-2020.jpg'] attachment_list = [] for root, dirs, files in os.walk(attachment_path, topdown=False): # print(root,dirs,files) for name in files: print(join(root,name)) attachment_list.append(join(root,name)) parts = add_attachment(attachment_list) # 设置SMTP服务器 with smtplib.SMTP(smtp_host, port) as smtp_server: smtp_server.login(MY_ADDRESS, PASSWORD) # 为每个联系人发送电子邮件 for name, email, country in zip(names, emails, countrys): # 创建一条message message = MIMEMultipart() # 根据country字段选择对应的邮件模板和主题 if country == 'CN': message_template = read_template(join(template_path,'mail_template_cn.html')) message['Subject'] = SUBJECT['CN'] elif country == 'JP': message_template = read_template(join(template_path,'mail_template_jp.html')) message['Subject'] = SUBJECT['JP'] else : message_template = read_template(join(template_path,'mail_template_en.html')) message['Subject'] = SUBJECT['EN'] # 将实际的人名添加到消息模板中 msg = message_template.substitute(PERSON_NAME=name.title()) # 如果模板read_template('mail_template.txt')则用 msg = 'Hi ' + name + message_template.substitute(PERSON_NAME=name.title()) # 打印出邮件正文 print(msg) # 设置message的参数:发件人、收件人、抄送、密送 message['From'] = MY_ADDRESS message['To'] = email message['Cc'] = CC message['Bcc'] = BCC # message['Subject'] = SUBJECT # 添加message正文 message.attach(MIMEText(msg, 'html')) # 如果模板read_template('mail_template.txt')则用message.attach(MIMEText(msg, 'plain')) # 在邮件中添加附件并将其转换为字符串 for part in parts: message.attach(part) text = message.as_string() # 通过先前设置的服务器发送邮件 smtp_server.sendmail(MY_ADDRESS, [email, CC, BCC], text) # 不用text的话,也可以这样发邮件smtp_server.send_message(message) print(f'\nEmail successfully sent to {name}:{email}!') print('-'*100) global count count += 1 print(f'本次共发送 {count} 封邮件!') if __name__ == '__main__': main()
-----------------------------------------------------
import os, csv, smtplib from os.path import join, dirname, abspath from string import Template from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders smtp_host = 'smtp.aisino.sh.cn' port = 25 # 邮箱账号和密码 MY_ADDRESS = 'jk.d@aisino.sh.cn' PASSWORD = 'xxx' # 抄送地址 CC = '744351600@qq.com' # 邮件主题 SUBJECT = {'CN':"订单确认", 'JP':"注文確認", 'EN':"Order confirmation"} # 根据当前目录获取联系人、模板、附件目录 base_dir = dirname(abspath(__file__)) contact_path = join(base_dir, 'contact') template_path = join(base_dir, 'mail_template') attachment_path = join(base_dir, 'attachment') def get_contacts(filename): """ 返回三个列表名称:names、emails、countrys 从文件名指定的文件中读取 """ names = [] emails = [] countrys = [] with open(filename, mode='r', encoding='utf-8') as contacts_file: reader = csv.reader(contacts_file) # 跳过标题行 next(reader) for name, email, country in reader: names.append(name) emails.append(email) countrys.append(country) return names, emails, countrys def read_template(filename): """ 返回包含以下内容的Template对象 文件名指定的文件 """ with open(filename, 'r', encoding='utf-8') as template_file: template_file_content = template_file.read() return Template(template_file_content) def add_attachment(filelist): """ 返回一个附件列表 parts """ parts = [] for filename in filelist: # 附件位于该Python脚本的目录中 # 以二进制模式打开附件 with open(filename, "rb") as attachment: # 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件 part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # base64编码 encoders.encode_base64(part) # 添加头参数,gbk支持中文附件名 part.add_header( "Content-Disposition", # f"attachment; filename= {filename}", "attachment", filename=('gbk', '', os.path.basename(filename)) ) parts.append(part) return parts def main(): # 读取联系人、邮件模板、附件 names, emails, countrys = get_contacts(join(contact_path, 'contacts.csv')) # message_template = read_template('mail_template.html') # 添加任意类型(pdf、office、rar、zip、jpg等)附件并存放入列表list_attachment,支持中文 # attachment_list = ['Ansible From Beginner to Pro.pdf', 'year-of-the-rat-2020.jpg'] attachment_list = [] for root, dirs, files in os.walk(attachment_path, topdown=False): # print(root,dirs,files) for name in files: print(join(root,name)) attachment_list.append(os.path.join(root,name)) parts = add_attachment(attachment_list) # 设置SMTP服务器 with smtplib.SMTP(smtp_host, port) as smtp_server: smtp_server.login(MY_ADDRESS, PASSWORD) # 为每个联系人发送电子邮件 for name, email, country in zip(names, emails, countrys): # 创建一条message message = MIMEMultipart() # 根据country选择对应的邮件模板和主体 if country == 'CN': message_template = read_template(join(template_path,'mail_template_cn.html')) message['Subject'] = SUBJECT['CN'] elif country == 'JP': message_template = read_template(join(template_path,'mail_template_jp.html')) message['Subject'] = SUBJECT['JP'] else : message_template = read_template(join(template_path,'mail_template_en.html')) message['Subject'] = SUBJECT['EN'] # 将实际的人名添加到消息模板中 msg = message_template.substitute(PERSON_NAME=name.title()) # 如果模板read_template('mail_template.txt')则用 msg = 'Hi ' + name + message_template.substitute(PERSON_NAME=name.title()) # 打印出邮件正文 print(msg) # 设置message的参数:发件人、收件人、抄送 message['From'] = MY_ADDRESS message['To'] = email message['Cc'] = CC # message['Subject'] = SUBJECT # 添加message正文 message.attach(MIMEText(msg, 'html')) # 如果模板read_template('mail_template.txt')则用message.attach(MIMEText(msg, 'plain')) # 在邮件中添加附件并将其转换为字符串 for part in parts: message.attach(part) text = message.as_string() # 通过先前设置的服务器发送邮件 smtp_server.sendmail(MY_ADDRESS, [email, CC], text) # 不用text的话,也可以这样发邮件smtp_server.send_message(message) print(f'\nEmail successfully sent to {name}:{email}!') print('-'*100) if __name__ == '__main__': main() --------------------------------------------------------
import smtplib,csv
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
smtp_host = 'smtp.aisino.sh.cn'
port = 25
# 邮箱账号和密码
MY_ADDRESS = 'jk.d@aisino.sh.cn'
PASSWORD = 'xxx'
# 抄送地址
CC = '744351600@qq.com'
# 邮件主题
SUBJECT = {'CN':"订单确认",
'JP':"注文確認",
'EN':"Order confirmation"}
def get_contacts(filename):
"""
返回三个列表名称:names、emails、countrys
从文件名指定的文件中读取
"""
names = []
emails = []
countrys = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
reader = csv.reader(contacts_file)
# 跳过标题行
next(reader)
for name, email, country in reader:
names.append(name)
emails.append(email)
countrys.append(country)
return names, emails, countrys
def read_template(filename):
"""
返回包含以下内容的Template对象
文件名指定的文件
"""
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def add_attachment(filelist):
"""
返回一个附件列表 parts
"""
parts = []
for filename in filelist:
# 附件位于该Python脚本的目录中
# 以二进制模式打开附件
with open(filename, "rb") as attachment:
# 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# base64编码
encoders.encode_base64(part)
# 添加头参数,gbk支持中文附件名
part.add_header(
"Content-Disposition",
"attachment",
filename=('gbk', '', filename)
# f"attachment; filename= {filename}",
)
parts.append(part)
return parts
def main():
# 读取联系人、邮件模板、附件
names, emails, countrys = get_contacts('contacts.csv')
# message_template = read_template('mail_template.html')
# 添加任意类型(pdf、office、rar、zip、jpg等)附件并存放入列表list_attachment,支持中文
attachment_list = ['电子发票平台使用手册.pdf', '01-安-电子发票平台服务及架构演进-20190711.pptx']
parts = add_attachment(attachment_list)
# 设置SMTP服务器
with smtplib.SMTP(smtp_host, port) as smtp_server:
smtp_server.login(MY_ADDRESS, PASSWORD)
# 为每个联系人发送电子邮件
for name, email, country in zip(names, emails, countrys):
# 创建一条message
message = MIMEMultipart()
# 根据country选择对应的邮件模板和主体
if country == 'CN':
message_template = read_template('mail_template_cn.html')
message['Subject'] = SUBJECT['CN']
elif country == 'JP':
message_template = read_template('mail_template_jp.html')
message['Subject'] = SUBJECT['JP']
else :
message_template = read_template('mail_template_en.html')
message['Subject'] = SUBJECT['EN']
# 将实际的人名添加到消息模板中
msg = message_template.substitute(PERSON_NAME=name.title())
# 如果模板read_template('mail_template.txt')则用 msg = 'Hi ' + name + message_template.substitute(PERSON_NAME=name.title())
# 打印出邮件正文
print(msg)
# 设置message的参数:发件人、收件人、抄送
message['From'] = MY_ADDRESS
message['To'] = email
message['Cc'] = CC
# message['Subject'] = SUBJECT
# 添加message正文
message.attach(MIMEText(msg, 'html'))
# 如果模板read_template('mail_template.txt')则用message.attach(MIMEText(msg, 'plain'))
# 在邮件中添加附件并将其转换为字符串
for part in parts:
message.attach(part)
text = message.as_string()
# 通过先前设置的服务器发送邮件
smtp_server.sendmail(MY_ADDRESS, [email, CC], text)
# 不用text的话,也可以这样发邮件smtp_server.send_message(message)
print(f'\nEmail successfully sent to {name}:{email}!')
print('-'*100)
if __name__ == '__main__':
main()
发送简单邮件
import smtplib smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定发件人和收件人的电子邮件地址 sender = "jk.d@aisino.sh.cn" receiver = "2818686037@qq.com" # meesage中的From、To顺序不能变且中间没空格,有空格则会把From、To写到信中,Subject可在它们前面或者后面,使用至少一个换行符(\ n)将主题与消息正文分开,本例使用了两个换行符,并使用'f'自动在文本中插入变量
message = f"""\ From: {sender} To: {receiver} Subject: Hi there This is my first message with Python.""" # 使用上面指定的凭据发送邮件 with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender, receiver, message) print('Sent')
发信给多个收件人
Python脚本所在的文件夹中contacts.csv内容如下#name,email DT,2818686037@qq.com JOLIN,jk.d@aisino.sh.cn
import csv, smtplib smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定发件人的电子邮件地址 sender = "jk.d@aisino.sh.cn" # meesage中的From、To顺序不能变且中间没空格,有空格则会把From、To写到信中,Subject可在它们前面或者后面,使用至少一个换行符(\ n)将主题与消息正文分开 message = """Subject: Order confirmation From: {sender} To: {recipient} Hi {name}: thanks for your order! We are processing it now and will contact you soon""" with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) with open("contacts.csv") as file: reader = csv.reader(file) next(reader) # it skips the header row for name, email in reader: server.sendmail( sender, email, message.format(name=name, recipient=email, sender=sender) ) print(f'Sent to {name}')
添加HTML内容
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定发件人和收件人的电子邮件地址 sender_email = "jk.d@aisino.sh.cn" receiver_email = "2818686037@qq.com" message = MIMEMultipart("alternative") message["Subject"] = "multipart test3" message["From"] = sender_email message["To"] = receiver_email # 编写纯文本部分 text = """\ Hi, Check out the new post on our blog blog: How to Send Emails with Python? https://blog.example.com/send-email-python/ Feel free to let us know what content would be useful for you!""" # 编写HTML部分 html = """\ <html> <body> <p>Hi,<br> Check out the new post on our blog blog: </p> <p><a href="https://blog.example.com/send-email-python/">How to Send Emails with Python?</a></p> <p> Feel free to <strong>let us</strong> know what content would be useful for you!</p> </body> </html> """ # 将两个部分都转换为MIMEText对象,并将它们添加到MIMEMultipart消息中 part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") # message.attach(part1) message.attach(part2) # 使用smtplib.SMTP("smtp.yourserver.com",25)作为服务器发送电子邮件:server.login(登录名和密码) with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender_email, receiver_email, message.as_string()) print('Sent')
添加附件如PDF文件的示例
import smtplib # 导入对应的模块 from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定主题、发件人和收件人的电子邮件地址 subject = "An example of boarding pass" sender_email = "jk.d@aisino.sh.cn" receiver_email = "2818686037@qq.com" message = MIMEMultipart() message["From"] = sender_email message["To"] = receiver_email message["Subject"] = subject # 将正文添加到电子邮件 body = "This is an example of how you can send a boarding pass in attachment with Python" message.attach(MIMEText(body, "plain")) # 添加任意类型(pdf、word、excel、jpg等)的附件 filename = "Ansible From Beginner to Pro.pdf" # 以二进制模式打开PDF文件 # 我们假设该PDF文件位于您从中运行Python脚本的目录中 with open(filename, "rb") as attachment: # 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件 part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # base64编码 encoders.encode_base64(part) # 添加标题 part.add_header( "Content-Disposition", f"attachment; filename= {filename}", ) # 在邮件中添加附件并将其转换为字符串 message.attach(part) text = message.as_string() # 发送您的电子邮件 with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender_email, receiver_email, text) print('Sent')
发送多个附件
import smtplib # 导入对应的模块 from email import encoders from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定主题、发件人和收件人的电子邮件地址 subject = "An example of boarding pass" sender_email = "jk.d@aisino.sh.cn" receiver_email = "2818686037@qq.com" message = MIMEMultipart() message["From"] = sender_email message["To"] = receiver_email message["Subject"] = subject # 将正文添加到电子邮件 body = "This is an example of how you can send a boarding pass in attachment with Python" message.attach(MIMEText(body, "plain")) # 添加任意类型(pdf、word、excel、jpg等)的附件存放入列表 list = ["Ansible From Beginner to Pro.pdf","currency.xlsx"] for filename in list: # 以二进制模式打开附件 # 我们假设该附件位于您从中运行Python脚本的目录中 with open(filename, "rb") as attachment: # 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件 part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # base64编码 encoders.encode_base64(part) # 添加标题 part.add_header( "Content-Disposition", f"attachment; filename= {filename}", ) # 在邮件中添加附件并将其转换为字符串 message.attach(part) text = message.as_string() # 发送您的电子邮件 with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender_email, receiver_email, text) print('Sent')
发送嵌入图片
在电子邮件消息中包含图像的三种常见方法是:base64图像(嵌入式嵌入),CID附件(作为MIME对象嵌入)和链接的图像# 首先导入需要的库 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import base64 smtp_server = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 login = "jk.d@aisino.sh.cn" password = "xxx" # 指定主题、发件人和收件人的电子邮件地址 subject = "inline embedding" sender_email = "jk.d@aisino.sh.cn" receiver_email = "2818686037@qq.com" message = MIMEMultipart("alternative") message["Subject"] = subject message["From"] = sender_email message["To"] = receiver_email # 样本图像文件与您从中运行Python脚本的目录相同 encoded = base64.b64encode(open("example.jpg", "rb").read()).decode() html = f"""\ <html> <body> <img src="data:image/jpg;base64,{encoded}"> </body> </html> """ part = MIMEText(html, "html") message.attach(part) # 发送您的电子邮件 with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender_email, receiver_email, message.as_string()) print('Sent')
发邮件给多人并带附件
import smtplib,csv from string import Template from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders smtp_host = "smtp.aisino.sh.cn" port = 25 # 邮箱账号和密码 MY_ADDRESS = 'jk.d@aisino.sh.cn' PASSWORD = 'xxx' # 邮件主题 SUBJECT = "Order confirmation33" def get_contacts(filename): """ 返回两个列表名称:names 和 emails 从文件名指定的文件中读取 """ names = [] emails = [] with open(filename, mode='r', encoding='utf-8') as contacts_file: reader = csv.reader(contacts_file) # 跳过标题行 next(reader) for name, email in reader: names.append(name) emails.append(email) return names, emails def read_template(filename): """ 返回包含以下内容的Template对象 文件名指定的文件 """ with open(filename, 'r', encoding='utf-8') as template_file: template_file_content = template_file.read() return Template(template_file_content) def add_attachment(filelist): """ 返回一个附件列表 parts """ parts = [] message = MIMEMultipart() for filename in filelist: # 附件位于该Python脚本的目录中 # 以二进制模式打开附件 with open(filename, "rb") as attachment: # 内容类型“应用程序/八位字节流”表示MIME附件是二进制文件 part = MIMEBase("application", "octet-stream") part.set_payload(attachment.read()) # base64编码 encoders.encode_base64(part) # 添加标题 part.add_header( "Content-Disposition", f"attachment; filename= {filename}", ) parts.append(part) return parts def main(): # 读取联系人、邮件模板、附件 names, emails = get_contacts('contacts.csv') message_template = read_template('mail_template.txt') # 添加任意类型(pdf、word、excel、jpg等)的附件存放入列表 list_attachment = ["Ansible From Beginner to Pro.pdf", "currency.xlsx"] parts = add_attachment(list_attachment) # 设置SMTP服务器 with smtplib.SMTP(smtp_host, port) as smtp_server: smtp_server.login(MY_ADDRESS, PASSWORD) # 为每个联系人发送电子邮件 for name, email in zip(names, emails): # 创建一条message message = MIMEMultipart() # 将实际的人名添加到消息模板中 msg = 'Hi ' + name + message_template.substitute(PERSON_NAME=name.title()) # 打印出邮件正文 print(msg) # 设置message的参数:发件人、收件人、抄送、主题 message['From'] = MY_ADDRESS message['To'] = email message['Cc'] = '2818686037@qq.com' message['Subject'] = SUBJECT # 添加message正文 message.attach(MIMEText(msg, 'plain')) # 在邮件中添加附件并将其转换为字符串 for part in parts: message.attach(part) text = message.as_string() # 通过先前设置的服务器发送邮件 smtp_server.sendmail(MY_ADDRESS, email, text) # 不用text的话,也可以这样发邮件smtp_server.send_message(message) print(f'\nEmail successfully sent to {name}!') print('-'*100) if __name__ == '__main__': main()
评论
发表评论