zoukankan      html  css  js  c++  java
  • python代码实现邮件发送

    python实现邮件自动发送,同时可以带上附件,并向多人发送邮件

    #coding:utf-8
    import smtplib,os
    from tools.configData import configKV
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart

    class mail(object):
    """
    host:指定连接的邮箱服务器,port:端口,user:邮箱用户名,password:邮箱密码,sender:邮件发送者地址,receiver:邮件接收者地址,msg:邮件内容
    """
    def sendmail(self, host, port, user, password, sender, receiver, msg):
    smtp = smtplib.SMTP()
    smtp.connect(host, port)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg)
    smtp.quit()

    def get_Msgs(self): #邮件body内容
    config=configKV()
    url=config.getKeys('url.ini','url')
    host_port=config.getKeys_values('hostport.ini','host_port')
    msgs = 'xxxx系统/xxxx接口全量测试报告' + ' '
    '1.概述' + ' '
    '测试目的:全量stockid验证行情接口的正确性' + ' '
    '被测接口:'+str(url)+ ' '
    '2.测试环境' + ' '
    '服务器IP:xxx.xxx.xxx.xxx' + ' '
    '服务器端口:8088'+ ' '
    '3.数据库信息' + ' '
    '数据库ip:xxx.xxx.xxx.xxx'+ ' '
    '数据库用户名:8080' + ' '
    '数据库密码:abcabc'+ ' '
    '4.测试方式' + ' '
    '自动测试' + ' '
    '5.风险点'+' '
    '1)测试是否通过是与公共接口(xxx.xxx.xxx.xxx)比较确定的,可能存在公共接口和测试接口数据都不正确的情况'+' '
    '2)测试用例更新不及时'+ ' '
    '3)测试环境出现异常' + ' '
    '4)其他未能预测的风险' + ' '
    "注:测试数据详见附件!缺陷可能会很多,故不再写入excel,可sql语句查询(SELECT * FROM mms.full_result t WHERE t.result='0';)"
    return msgs

    #emailName:邮件名称,msgs:邮件body
    #host:指定连接的邮箱服务器,port:端口,user:邮箱用户名,password:邮箱密码,sender:邮件发送者地址,receiver:邮件接收者地址,多个接收者则可以组织成一个list
    def sendReport(self,msgs,emailName,host, port, user, password, sender, receiver):
    xlsxpart = MIMEApplication(open('xxx/xxx/report.xlsx','rb').read())
    xlsxpart.add_header('Content-Disposition', 'attachment', filename=report.xlsx) #加入要发送的附件
    msg = MIMEMultipart()
    msg.attach(xlsxpart)
    msg.attach(MIMEText(msgs, 'plain', 'utf-8'))
    msg['Subject'] = Header(emailName, 'utf-8').encode()
    self.sendmail(host,port,user,password,sender,receiver, msg.as_string())

  • 相关阅读:
    Linux下Socket编程之地址结构
    矫正Fedora 8中livna源Nvidia驱动托付关连
    Firefox3 RC1颁布各种新特征发扬阐发更平定
    Fedora 8中完全开启compizfusion特效
    编译安置gsopcast SVN版
    Sopcast for linux更新至3.01!
    措置惩罚Fedora 8中的装备权限标题成绩
    vsftp假造用户设置(Ubuntu8.04)
    QQ2008贺岁版 on FedoraBy wine 0.9.58
    vFU NET
  • 原文地址:https://www.cnblogs.com/shuyichao/p/10384602.html
Copyright © 2011-2022 走看看