def send_email(reportname,title): ''' 发送测试报告邮件 :param reportname: 报告名称 :param title: 邮件主题 :return: ''' # 连接smtp服务器 smtp = smtplib.SMTP_SSL(host=conf.get("email","host"),port=conf.getint("email","port")) smtp.login(user=conf.get("email","user"),password=conf.get("email","pwd")) # 构建一封邮件 # 读取邮件报告的内容 with open(os.path.join(reports_dir,"report.html"),"rb") as f: content = f.read() # 创建一个多组件对象 msg = MIMEMultipart() # 邮件内容 text_msg = MIMEText(content,_subtype="html",_charset="utf8") # 将邮件加入到组件 msg.attach(text_msg) # 添加附件内容 report_msg = MIMEApplication(content) # 添加附件头内容 report_msg.add_header('content-disposition', 'attachment', filename=reportname) # 将附件添加到组件中 msg.attach(report_msg) msg["Subject"] = title msg["From"] = conf.get("email","from_addr") msg["To"] = conf.get("email","to_addr") # 发送邮件 smtp.send_message(msg,from_addr=conf.get("email","from_addr"),to_addrs=conf.get("email","to_addr").split(","))
def __init__(self): self.conn = pymysql.connect(host=conf.get('mysql', 'host'), port=conf.getint('mysql', 'port'), user=conf.get('mysql', 'name'), password=conf.get('mysql', 'password'), charset=conf.get('mysql', 'charset'), cursorclass=pymysql.cursors.DictCursor) self.cur = self.conn.cursor()
def __init__(self): self.conn = pymysql.connect(host=conf.get("db", "host"), port=conf.getint("db", "port"), user=conf.get("db", "user"), password=conf.get("db", "pwd"), charset=conf.get("db", "charset"), cursorclass=pymysql.cursors.DictCursor) self.cur = self.conn.cursor()
def send_email(file_path,text_msg=None,subject="Python代码邮件"): """ 发送附件文件的邮件的方法 :param file_path: 附件路径,必填项 :param text_msg: 邮件内文本内容,不填则默认为附件内文本; :param subject: 邮件的主题名称。 :return: """ #第一步:根据smtp服务器地址,准备测试账号及授权码 #smtp服务器地址 host = conf.get("email","host") #端口 port = conf.getint("email","port") #发件人邮箱 user = conf.get("email","user") #发件人邮箱授权码 password = conf.get("email","password") #收件人邮箱 to_user = eval(conf.get("email","to_user")) #附件的文件名 filename = conf.get("email","filename") # 1、连接smtp服务器 smtp = smtplib.SMTP_SSL(host=host,port=port) #2、登录账户 smtp.login(user=user,password=password) # 创建一封多组件的邮件 email = MIMEMultipart() with open(file_path,"rb") as f : content = f.read() #邮件内的文本信息 if text_msg == None: text_msg = content text_email = MIMEText(text_msg, _subtype='html', _charset="utf8") # 添加到多组件的邮件中 email.attach(text_email) #邮件的附件信息 file_email = MIMEApplication(content) file_email.add_header("content-disposition", "attachment", filename=filename) # 添加到多组件的邮件中 email.attach(file_email) #1、设置邮件主题名 email["Subject"] = subject #2、设置邮件发送人 email["From"] = user #3、设置邮件接收人 for i in to_user: email["To"] = i #发送邮件 smtp.send_message(email,from_addr=user,to_addrs=to_user)
def __init__(self): # 连接数据库 self.connect = pymysql.connect(host=conf.get("bd", "host"), port=conf.getint("bd", "port"), user=conf.get("bd", "user"), password=conf.get("bd", "password"), charset="utf8", cursorclass=pymysql.cursors.DictCursor) # 创建一个游标对象 self.cur = self.connect.cursor()
def send_email(filename, title): """ 发送邮件的功能函数 :param filename: 文件的路径 :param title: 邮件的主题 :return: """ # 第一步:连接邮箱的smtp服务器,并登录 smtp = smtplib.SMTP_SSL(host=conf.get("email", "host"), port=conf.getint("email", "port")) smtp.login(user=conf.get("email", "user"), password=conf.get("email", "pwd")) # 第二步:构建一封邮件 # 创建一封多组件的邮件 msg = MIMEMultipart() with open(filename, "rb") as f: content = f.read() # 创建邮件文本内容 text_msg = MIMEText(content, _subtype="html", _charset="utf8") # 添加到多组件的邮件中 msg.attach(text_msg) # 创建邮件的附件 report_file = MIMEApplication(content) report_file.add_header('content-disposition', 'attachment', filename=filename) # 将附件添加到多组件的邮件中 msg.attach(report_file) # 主题 msg["Subject"] = title # 发件人 msg["From"] = conf.get("email", "from_addr") # 收件人 msg["To"] = conf.get("email", "to_addr") # 第三步:发送邮箱 smtp.send_message(msg, from_addr=conf.get("email", "from_addr"), to_addrs=conf.get("email", "to_addr"))