import smtplib
import time
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.header import Header
from email.utils import formataddr
from email.utils import parseaddr
from email import encoders
class Email_Send():
def __init__(self, host, port, ssl = 'NOT_SSL', debug_lv = 2, is_log = 'disable'):
self.__debug_lv = debug_lv #调试等级 0级不打印,1级打印简单信息,2级打印附件信息,3级开启smtplib内部打印
self.__is_log = True if is_log == 'enable' else False #开启日志记录模式,成功记录和错误记录分开保存,方便排查
self.__log_content = []
self.print_and_log('*'*50, 1)
self.print_and_log('Email_Host:{0};\nport:{1};\nSSL:{2};\ndebug_level:{3};\nlog:{4};'.format(host, port, ssl, self.__debug_lv,'Open' if self.__is_log else 'Close'), 1)
self.print_and_log('*'*50, 1)
try:
if ssl == 'SSL':
self.print_and_log('{0}正在连接服务器(SSL)...'.format(time.strftime("[%Y-%m-%d %H:%M:%S]",time.localtime())), 1)
self.smtp = smtplib.SMTP_SSL(host, port)
self.smtp.set_debuglevel(1) if self.__debug_lv >= 3 else None
else:
self.print_and_log('{0}正在连接服务器(NOT_SSL)...'.format(time.strftime("[%Y-%m-%d %H:%M:%S]",time.localtime())), 1)
self.smtp = smtplib.SMTP(host, port)
self.smtp.set_debuglevel(1) if self.__debug_lv >= 3 else None
self.print_and_log('{0}服务器连接成功!'.format(time.strftime("[%Y-%m-%d %H:%M:%S]",time.localtime())), 1)
except:
self.print_and_log('{0}服务器连接失败!'.format(time.strftime("[%Y-%m-%d %H:%M:%S]",time.localtime())), 1)
self.print_and_log('*'*50, 1)
self.write_to_log(False)
raise
def set_debug_level(self, level):
self.__debug_lv = level
def set_is_log(self, is_log):
self.__is_log = is_log
def login(self, user, pwd):
try:
self.prin
Python实现邮件群发,多附件,正文添加图片
最新推荐文章于 2024-09-21 17:18:17 发布
本文介绍如何使用Python进行邮件群发,并详细阐述了如何添加多个附件以及在邮件正文中插入图片,适用于自动化报告发送或批量通知场景。
摘要由CSDN通过智能技术生成