邮件配置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了邮件配置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2011字,纯文字阅读大概需要3分钟。
内容图文
![邮件配置](/upload/InfoBanner/zyjiaocheng/1202/ae7454ad7e1e4b88b6fd4e69609792ee.jpg)
一、邮件配置yaml格式
# conf.yaml mail: # 发送邮件信息 smtpserver : "smtp.itcast.cn" receiver : "******@itcast.cn" username : "******@itcast.cn" password : "*******"
二、邮件配置读取
# Conf.py class ConfigYml: def __init__ (self): logging.info(_config_file) # print(get_log_path()) self.r_config = YamlReader(_config_file).data self.r_db_config = YamlReader(_config_db).data def get_mail_info(self): """ 获取邮件相关设置信息 :return: """return self.r_config["mail"]
三、邮件读取封装
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText class SendMail(object): def__init__(self,username,passwd,recv,title,content, file=None, smtp_server=‘smtp.itcast.cn‘,port=25): self.username = username self.passwd = passwd self.recv = recv self.title = title self.content = content self.file = file self.smtp_server = smtp_server self.port = port def send_mail(self): msg = MIMEMultipart() #发送内容的对象if self.file:#处理附件的 att = MIMEText(open(self.file).read()) att["Content-Type"] = ‘application/octet-stream‘ att["Content-Disposition"] = ‘attachment; filename="%s"‘%self.file msg.attach(att) msg.attach(MIMEText(self.content))#邮件正文的内容 msg[‘Subject‘] = self.title # 邮件主题 msg[‘From‘] = self.username # 发送者账号 msg[‘To‘] = self.recv # 接收者账号列表 self.smtp = smtplib.SMTP(self.smtp_server,port=self.port)#发送邮件服务器的对象self.smtp.login(self.username,self.passwd) try: self.smtp.sendmail(self.username,self.recv,msg.as_string()) except Exception as e: print(‘出错了‘,e) else: print(‘发送成功!‘)
四、设置run.py
def send_email(): mail_info = Conf.ConfigYml().get_mail_info() title= "接口自动化测试报告" username = mail_info["username"] passwd = mail_info["password"] receiver = mail_info["receiver"] smtp_server = mail_info["smtpserver"] content="" report_file= report_html_path+os.sep+"index.html"try: mail = SendMail( smtp_server=smtp_server, username=username, passwd=passwd, recv=receiver, title=title, content=content, file = report_file, ) mail.send_mail() except: log.error(‘发送邮件失败,请检查邮件配置‘) raise
原文:https://www.cnblogs.com/siguadd/p/14836743.html
内容总结
以上是互联网集市为您收集整理的邮件配置全部内容,希望文章能够帮你解决邮件配置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。