Python 邮件发送 MailHelper 本文共有13870个字,关键词: 支持发送文本邮件、HTML邮件、带图片邮件、带附件邮件import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import parseaddr, formataddr from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage class MailHelper: ''' 邮件发送类 Author:梦白沙 ''' def __init__(self, smtp_addr, smtp_port, smtp_user, smtp_pwd, from_addr, from_name): ''' init :param smtp_addr:smtp服务器地址 :param smtp_port: SSL端口,常用465 :param smtp_user: smtp用户名 :param smtp_pwd: smtp密码 :param from_addr: 发件人地址 :param from_name: 发件人称呼 ''' self.smtp_addr = smtp_addr self.smtp_port = smtp_port self.smtp_user = smtp_user self.smtp_pwd = smtp_pwd self.from_addr = from_addr self.from_name = from_name def __format_addr(self, addr): return formataddr(parseaddr(addr)) def send(self, mailto, subject, content, subtype='plain', images_list=[], attach_files=[], mailto_name=''): ''' 邮件发送 :param mailto:收件人,可以为['a@b.com','b@b.com']、a@a.com,b@b.com、a@a.com :param subject: 主题 :param content: 内容 :param subtype: 邮件类别 plain or html default:plain :param images_list: 邮件内容图片列表 :param attach_files: 添加的附件 :param mailto_name: 收件人称呼,如果有多个收件人时,默认为同一个称呼 :return: 成功success ''' recivers = self.__init_receivers(mailto) # if images_list: # msg = MIMEMultipart('related') # else: msg = MIMEMultipart() # msg['To'] = Header(mailto_name, 'utf-8') msg['From'] = self.__format_addr(f'{self.from_name}<{self.from_addr}>') msg['Subject'] = Header(subject, 'utf-8') mailto_list = [] for x in recivers: mailto_list.append(self.__format_addr(f'{mailto_name}<{x}>')) msg['To'] = ';'.join(mailto_list) print(msg['To']) if attach_files: try: for i, x in enumerate(attach_files, 1): file_name = x.replace('\\', '/').split("/")[-1] att = MIMEText(open(x, 'rb').read(), 'base64', 'utf-8') att['Content-Type'] = 'application/octet-stream' att['Content-Disposition'] = 'attachment;filename=' + file_name msg.attach(att) except: print(f'file error -->{x}') pass if images_list: try: msg.attach(MIMEText(content, 'html', 'utf-8')) for i, x in enumerate(images_list, 1): image_data = open(x, 'rb').read() image = MIMEImage(image_data) image.add_header('Content-ID', f'<image{i}>') msg.attach(image) except: print(f'file error -->{x}') pass else: msg.attach(MIMEText(content, subtype, 'utf-8')) try: smtp_obj = smtplib.SMTP_SSL(self.smtp_addr, self.smtp_port) smtp_obj.login(self.smtp_user, self.smtp_pwd) smtp_obj.sendmail(self.from_addr, recivers, msg.as_string()) print('success') except smtplib.SMTPException as e: print(e) finally: smtp_obj.quit() def __init_receivers(self, mailto): receivers = [] if type(mailto) == str: if not ',' in mailto: receivers.append(mailto) else: receivers = [x for x in mailto.split(',')] elif type(mailto) == list: receivers = mailto return receivers def send_plain(self, mailto, subject, content, mailto_name=''): ''' :param mailto: 收件人,可以为['a@b.com','b@b.com']、a@a.com,b@b.com、a@a.com :param subject: 主题 :param content: 内容 :param mailto_name: 收件人称呼,如果有多个收件人时,默认为同一个称呼 :return: ''' return self.send(mailto, subject, content, 'plain', [], [], mailto_name='') def send_html(self, mailto, subject, content, mailto_name): ''' 发送HTML邮件 :param mailto: 收件人,可以为['a@b.com','b@b.com']、a@a.com,b@b.com、a@a.com :param subject: 主题 :param content: 内容 :param mailto_name: 收件人称呼,如果有多个收件人时,默认为同一个称呼 :return: ''' return self.send(mailto, subject, content, 'html', [], [], mailto_name='') def send_with_img(self, mailto, subject, content, images_list=[], mailto_name=''): ''' 发送含图片邮件 :param mailto: 收件人,可以为['a@b.com','b@b.com']、a@a.com,b@b.com、a@a.com :param subject: 主题 :param content: 内容 :param images_list: 图片列表,在文件中可以用cid:imageX 来获取图片 :param mailto_name: 收件人称呼,如果有多个收件人时,默认为同一个称呼 :return: ''' return self.send(mailto, subject, content, 'html', images_list, [], mailto_name) def send_with_attach(self, mailto, subject, content, attach_list=[], mailto_name=''): ''' 发送含附件的邮件 :param mailto: 收件人,可以为['a@b.com','b@b.com']、a@a.com,b@b.com、a@a.com :param subject: 主题 :param content: 内容 :param attach_list: 附件列表 :param mailto_name: 收件人称呼,如果有多个收件人时,默认为同一个称呼 :return: ''' return self.send(mailto, subject, content, 'html', [], attach_list, mailto_name) if __name__ == '__main__': pass 「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」 赞赏 × 梦白沙 (๑>ڡ<)☆谢谢老板~ 1元 2元 5元 10元 50元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 Python 2022-04-25 评论 302 次浏览