自动发送邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header

#发送邮件主题
subject = "python email test"

#编写HTML类型的邮件正文
msg = MIMEText("<html><h1>你好!</h1><html>", "html", "utf-8")
msg["Subject"] = Header(subject, "utf-8")

#发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.163.com")
#使用SMTP登录时,密码为授权码
smtp.login("¥¥¥¥.com", "授权码")
smtp.sendmail("¥¥¥¥@163.com", "¥¥¥¥¥@163.com",msg.as_string())
smtp.quit()

 

 发送带附件的邮件:

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

#发送邮件主题
subject = "python send email test"

#发送附件
with open('2020-08-20 21_47_55baidu_search.html','rb') as fp:
    send_att = fp.read()

att = MIMEText(send_att,'text','utf-8')
att["Content-Type"]='application/octet-stream'
att.add_header("Content-Disposition","attachment",\
               filename="2020-08-20 21_47_55baidu_search.html")


msg = MIMEMultipart()
msg["Subject"]=subject
msg.attach(att)

#发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.163.com")
#使用SMTP登录时,密码为授权码
smtp.login("sender@163.com", "授权码")
smtp.sendmail("sender@163.com", "receiver@163.com",msg.as_string())
smtp.quit()

三、使用yagmail模块发送邮件。

 

import yagmail

#连接邮箱服务器
yag = yagmail.SMTP(user="sender@163.com",password="授权码",\
                   host = "smtp.163.com")

#邮件正文
contents = ['This is the body,and here is just text https://somedomain/image.png',\
            'you can find an audio file attachd']
"""
#给单人发送邮件
yag.send("receiver@163.com", "subject", contents)

#给多人发送邮件
yag.send(["receiver@163.com","abc@qq.com","cdf@126.com"],"subject",contents)

"""
#发送附件
yag.send("sender@163.com", "test_report", contents,\
         ["d://baidu_search.html"])

 四、整合自动发送邮件功能。

import time,unittest,yagmail
from HTMLTestRunner import HTMLTestRunner
from TestBaidu import TestBaidu

#把测试报告发送到指定邮箱
def send_mail(report):
    yag = yagmail.SMTP(user = "sender@163.com",
                       password = "授权码",
                       host = "smtp.163.com")

    subject = "主题:自动化测试报告"
    contents = "请查看邮件"
    yag.send("收件人@163.com",subject, contents, report)
    print("email has send out")


if __name__=="__main__":
    suit = unittest.TestSuite()
    suit.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBaidu))
    now_time = time.strftime("%Y-%m-%d %H_%M_%S")
    html_report = now_time + "baidu_search.html"
    with open(html_report,"wb") as fp:
        runner = HTMLTestRunner(stream=fp, title="百度搜索测试报告",
                                description="win10 Chrome",
                                verbosity=2)
        runner.run(suit)
        fp.close()
        send_mail(html_report)

 

output:
ok test_search1_1___case1____selenium__ (TestBaidu.TestBaidu)
ok test_search1_2___case2____ddt__ (TestBaidu.TestBaidu)
ok test_search1_3___case3____python__ (TestBaidu.TestBaidu)
ok test_search2_1___case1____selenium__ (TestBaidu.TestBaidu)
ok test_search2_2___case2____ddt__ (TestBaidu.TestBaidu)
ok test_search2_3___case3____python__ (TestBaidu.TestBaidu)
ok test_search3_1 (TestBaidu.TestBaidu)
ok test_search3_2 (TestBaidu.TestBaidu)
ok test_search3_3 (TestBaidu.TestBaidu)
ok test_search4_1_case1 (TestBaidu.TestBaidu)
ok test_search4_2_case2 (TestBaidu.TestBaidu)
ok test_search4_3_case3 (TestBaidu.TestBaidu)
email has send out

 

posted @ 2020-08-20 22:36  jerrygogo  阅读(141)  评论(0编辑  收藏  举报