参考 :
发送邮件简单入门 ( 以qq邮箱,163邮箱为例 ) :https://blog.csdn.net/qq_38661599/article/details/81013834
smtp ssl 模式 发送邮件 与 附件:https://www.cnblogs.com/SunshineKimi/p/10629342.html
Python3 使用 SMTP 发送带附件邮件:https://www.jb51.net/article/142231.htm
还可以使用 scrapy.mail 模块发送邮件:https://blog.csdn.net/you_are_my_dream/article/details/60868329
把代码中这个几设置成你自己的参数:
self._smtp_host = "smtp.mxhichina.com" # 设置 服务器
self._smtp_port = 465 # 设置 端口
self._email_address = "xxxxxxxx@xxx.com" # 用户名
self._email_password = "xxxxxxxxxxxxxxx" # 口令
完整代码( send_email_attach.py )
# -*- coding: utf-8 -*-
# @Author :
# @File : temp.py
# @Software: PyCharm
# @description : XXXimport smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import optparseclass SendEmail(object):def __init__(self):super(SendEmail, self).__init__()# 第三方 SMTP 服务self._smtp_host = "smtp.mxhichina.com" # 设置 服务器self._smtp_port = 465 # 设置 端口# 默认的发送邮件的邮箱地址和密码。# 当没有传递发送者的邮箱地址和密码时,使用默认的邮箱地址和密码发送self._email_address = "xxxxxxxx@xxx.com" # 用户名self._email_password = "xxxxxxxxxxxxxxx" # 口令self.frm = Noneself.pwd = Noneself.to = Noneself.email_title = Noneself.email_content = Noneself.attach_path = Noneself.attach_path_list = Nonepassdef set_args(self, frm=None, pwd=None, to=None, email_title=None, email_content=None, attach_path=None):"""设置参数:param frm: 发送者邮箱地址:param pwd: 发送者邮箱密码:param to: 接收者邮箱地址,多个接收者时以逗号','分割:param email_title: 邮件标题:param email_content:邮件内容:param attach_path: 附件路径,多个附件时以逗号','分割:return:"""if frm:self.frm = frmif not pwd:raise Exception('设置邮箱密码')else:self.pwd = pwdelse:self.frm = self._email_addressself.pwd = self._email_passwordself.to = toself.email_title = email_titleself.email_content = email_contentself.attach_path = attach_path# 把逗号分割的附件路径变成 listif self.attach_path is not None:self.attach_path_list = self.attach_path if ',' not in self.attach_path else self.attach_path.split(',')def send_email(self):multi_part = MIMEMultipart()multi_part['From'] = self.frmmulti_part['To'] = self.tomulti_part['Subject'] = Header(self.email_title, "utf-8")# 添加 邮件 内容msg = self.email_contentemail_body = MIMEText(msg, 'plain', 'utf-8')multi_part.attach(email_body)# 添加附件if isinstance(self.attach_path_list, str):# 只有一个附件attach = MIMEText(open(self.attach_path, 'rb').read(), 'base64', 'utf-8')attach["Content-Type"] = 'application/octet-stream'# filename not strictattach_file_name = self.attach_path_list.split('/')[-1]attach["Content-Disposition"] = 'attachment; filename="{0}"'.format(attach_file_name)multi_part.attach(attach)elif isinstance(self.attach_path_list, list):# 多个附件for item in self.attach_path_list:attach = MIMEText(open(item, 'rb').read(), 'base64', 'utf-8')attach["Content-Type"] = 'application/octet-stream'# filename not strictattach_file_name = item.split('/')[-1]attach["Content-Disposition"] = 'attachment; filename="{0}"'.format(attach_file_name)multi_part.attach(attach)# ssl 协议安全发送smtp_server = smtplib.SMTP_SSL(host=self._smtp_host, port=self._smtp_port)try:smtp_server.login(self.frm, self.pwd)smtp_server.sendmail(self.frm, self.to, multi_part.as_string())except smtplib.SMTPException as e:print("send fail", e)else:print("send success")finally:try:smtp_server.quit()except smtplib.SMTPException:print("quit fail")else:print("quit success")if __name__ == '__main__':parse = optparse.OptionParser(usage='"usage : %prog [options] arg1,arg2"', version="%prog 1.2")parse.add_option('-t', '--to', dest='to', action='store', type=str, metavar='to',help='接收者的邮箱地址, 多个接收者时以逗号 "," 分隔')parse.add_option('-f', '--from', dest='frm', type=str, metavar='from',help='发送者的邮箱地址')parse.add_option('-p', '--pwd', dest='pwd', type=str, metavar='pwd',help='发送者的邮箱密码')parse.add_option('-T', '--title', dest='email_title', type=str, metavar='title',help='邮件标题')parse.add_option('-C', '--content', dest='email_content', type=str, metavar='content',help='邮件内容')parse.add_option('-A', '--attach', dest='attach_path', type=str, metavar='attach',help='邮件的附件路径, 多个附件时以逗号 "," 分隔')parse.add_option('-v', help='help')options, args = parse.parse_args()temp_send = SendEmail()temp_send.set_args(frm=options.frm, pwd=options.pwd, to=options.to,email_title=options.email_title,email_content=options.email_content,attach_path=options.attach_path)temp_send.send_email()
可以执行 :python3 send_email_attach.py -h 查看帮助
发送邮件:
邮箱截图