使用内置模块email发送邮件的方法
import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import formataddr from utils.common_logger import logger class EmailSender: def __init__(self, smtp_server='smtp的server地址'): self.smtp_server = smtp_server def send_mail(self, to, cc, subject, body, importance=None): # 创建 MIMEText 对象 sender = '发送人邮箱' message = MIMEText(body, "html", 'utf-8') message['From'] = formataddr((str(Header('邮件标题', 'utf-8')), sender)) message['To'] = ", ".join(to) message['Cc'] = ", ".join(cc) message['Subject'] = Header(subject, 'utf-8') # 如果配置了 importance,则设置邮件的重要性 if importance: message['Importance'] = importance # 合并收件人和抄送人列表 all_recipients = to + cc try: # 连接到 SMTP 服务器并发送邮件 smtp_obj = smtplib.SMTP(self.smtp_server) smtp_obj.sendmail(sender, all_recipients, message.as_string()) print("Email sent successfully!") except smtplib.SMTPException as e: logger.error('邮件发送失败了,原因为:{}'.format(e)) def code_check_in_style(self, to, cc): subject = '邮件标题' body = """ <html> <body> 书写你的邮件内容 </body> </html> """ self.send_mail(to, cc, subject, body, importance='High') # 使用示例 if __name__ == "__main__": to = ['收件人邮箱'] cc = ['抄送人邮箱'] email_sender = EmailSender() email_sender.code_check_in_style(to, cc)
本文来自博客园,作者:wellplayed,转载请注明原文链接:https://www.cnblogs.com/wellplayed/p/18259945
标签:
django
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南