""" 1. 发送邮件的几个步骤: 1)与邮件服务器建立会话连接 2)指定用户的登录 3)发送邮件 2. 一个标准邮件包含: 1)邮件头:标题;收件人、发送人、抄送cc、密送bcc、时间 2)邮件体:正文、附件 3. 常见的 MIME 格式 ------------------------------------------------- 文件格式 | 对应的 MIME 格式 (可通过抓包查看) ------------------------------------------------- html text/html xml text/xml text text/plain 图片 image/jpg、image/png、image/jpeg... 应用程序 application/ms-word、application/ms-excel、application/flash... 二进制流 application/octet-stream 4. 实际 MIME 内容包含:Content-ID、Content-Description、Content-Text、Content-Type、Content-Disposition等 """
import smtplib import sys from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from Public_Layer.Read_Ini import ReadIni from Get_Path import GET_PATH class SendEmail(object): # 完成邮件用户信息的初始化 def __init__(self): __read_ini = ReadIni() __ini_path = GET_PATH + "\Public_Layer\Ini\MailIni.ini" # 读取邮件配置信息,并以 dict 存储 self.get_email_config = __read_ini.read_ini(__ini_path, "mail_config") self.get_cc = self.get_email_config["cc"] self.get_bcc = self.get_email_config["bcc"] # 处理收件人信息 def to_addrs(self): try: get_to_addr = self.get_email_config['to_addrs'] # 收件人列表,将 to_addr,cc, bcc 的所有收件人整合在一个列表中 # 完成后,将列表传给 SMTP().sendmail() 方法中的 to_addrs,真实发送给列表中的收件人 get_to_addrs_list = get_to_addr.split(",") if len(self.get_cc) != 0: get_to_addrs_list += self.get_cc.split(",") if len(self.get_bcc) != 0: get_to_addrs_list += self.get_bcc.split(",") return get_to_addrs_list except: print("收件人信息处理失败") # 处理发件人信息 def from_addr(self): try: get_from_addr = self.get_email_config['from_addr'] # 将邮箱中的 发件人,由原来的”我“,显示成为常规的邮箱名 return get_from_addr.split("@")[0] + "<%s>" % get_from_addr except: print("发件人信息处理失败") # 创建一个完整的邮件 def create_email(self, subject, message, report_name, report_path): # 创建 MIMEMultipart 对象 email = MIMEMultipart() # 1)创建 email 的头,下面的 from,to,cc,bcc 的部分仅仅是显示 email["Subject"] = subject email["From"] = self.from_addr() # 将收件人从 list 转成 string 格式 email["To"] = self.get_email_config["to_addrs"] email["Cc"] = self.get_cc email["Bcc"] = self.get_bcc # 2)添加 email 正文 email.attach(self.create_email_payload(message)) # 3)添加 email 附件 email.attach(self.create_email_attachment(report_name, report_path)) return email # 创建邮件附件 def create_email_attachment(self, report_name, report_path): with open(report_path, mode="rb") as fp: email_attach = MIMEText(fp.read(), _subtype="base64", _charset="utf-8") email_attach["Content_Type"] = "application/octet-stream" email_attach.add_header(_name='Content-Disposition', _value='attachment', filename='%s' % report_name) return email_attach # 创建邮件文本 def create_email_payload(self, message): return MIMEText(_text=message, _charset="utf-8") # 创建会话、登录并发送邮件 def send_email(self, subject, message, report_name, report_path): try: # 创建 SMTP 会话连接对象 get_smtp = smtplib.SMTP(host=self.get_email_config["mail_server"]) # SMTP 邮件登录 get_smtp.login(user=self.get_email_config["mail_user"], password=self.get_email_config["mail_password"]) # 发送邮件 # !!此处的 from_addr,to_addrs 是真实的发件人和收件人,与 create_email() 方法的不一样,后者仅是显示 get_smtp.sendmail(from_addr=self.from_addr(), to_addrs=self.to_addrs(), msg=self.create_email(subject, message, report_name, report_path).as_string() ) except: print(sys.exc_info()) if __name__ == '__main__': send_email = SendEmail() send_email.send_email("CRM test report", "message", "CRM_Project20221011114522.html", 'C:\\Users\\Google_he\\PycharmProjects\\trunk\\Lesson_FullStack_TD\\CRM_Project\\Main_Layer' '\\Report\\CRM_Project20221011114522.html')
标签:addr,get,Python,self,report,email,邮件 From: https://www.cnblogs.com/bruce-he/p/16782736.html