人生苦短,我学python之python+selenium 发送邮件

# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
def send_email(smtpserver,port,sender,psw,receiver):
    #发件服务器
    # smtpserver='smtp.qq.com'
    # #端口
    # port=465
    # sender="770762632@qq.com"
    # #QQ邮箱为授权码,其他邮箱为密码
    # psw="snjczicdivhcbcde"
    # receiver='770762632@qq.com'
    msg=MIMEMultipart()
    msg['subject']="自动化测试用例"
    msg['from']=sender
    msg['to']=receiver
    #当前脚本所在路径
    curpath=os.path.realpath(__file__)
    print curpath
    #获取当前脚本所在文件夹的路径
    filepath=os.path.dirname(curpath)
    print filepath
    fatpath=os.path.dirname(filepath)
    print fatpath
    reportpath=os.path.join(fatpath,'report')
    print reportpath
    report=os.path.join(reportpath,'report.html')
    print report

    b=open(report,'r')
    main_body=b.read()
    b.close()
    # 添加到附件
    at=MIMEText(main_body,'base64','utf-8')
    at["Content-Type"] = "application/octet-stream"
    at["Content-Disposition"] ='attachment; filename="test_report.html"'
    msg.attach(at)
    # 添加正文
    body=MIMEText(main_body,'html','utf-8')
    msg.attach(body)
    #写信流程
    smtp=smtplib.SMTP_SSL(smtpserver,port)
    smtp.login(sender,psw)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()
send_email('smtp.qq.com',465,"770762632@qq.com","snjczicdivhcbcde",'770762632@qq.com')

 

posted @ 2018-04-18 21:06  w770762632  阅读(154)  评论(0编辑  收藏  举报