import datetime
import os
import sys
import unittest
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from pathlib import Path
TOP_DIR = Path(__file__).parent.parent.joinpath("utils")
sys.path.append(TOP_DIR)
from utils.send_email import send_email
class TestSendEmail(unittest.TestCase):
SRC_DIR = Path(__file__).parent.parent.joinpath("qbot/gui")
mail_sender = "1144262839@qq.com"
mail_license = os.getenv("MAIL_LICENSE")
mail_receivers = ["yidazhang1@gmail.com"]
subject = """Python邮件测试"""
def test_sendtext(self, mail_sender=mail_sender, mail_receivers=mail_receivers):
body_content = """你好,这是一个测试邮件!"""
message_text = MIMEText(body_content, "plain", "utf-8")
self.assertTrue(send_email(mail_sender, mail_receivers, message_text))
def test_sendattachment(self):
attachment = MIMEText(
open(self.SRC_DIR.joinpath("bkt_result/bkt_result.html"), "rb").read(),
"base64",
"utf-8",
)
attachment["Content-Disposition"] = 'attachment; filename="bkt_result.html"'
self.assertTrue(send_email(self.mail_sender, self.mail_receivers, attachment))
def test_sendimage(self):
image_data = open(self.SRC_DIR.joinpath("imgs/UFund.png"), "rb")
message_image = MIMEImage(image_data.read())
image_data.close()
self.assertTrue(send_email(self.mail_sender, self.mail_receivers, message_image))
def test_sendhtml(self):
now_time = datetime.datetime.now()
year = now_time.year
month = now_time.month
day = now_time.day
mytime = str(year) + " 年 " + str(month) + " 月 " + str(day) + " 日 "
fayanren = "爱因斯坦"
zhuchiren = "牛顿"
html_content = """
<html>
<body>
<h1 align="center">这个是标题,xxxx通知</h1>
<p><strong>您好:</strong></p>
<blockquote><p><strong>以下内容是本次会议的纪要,请查收!</strong></p></blockquote>
<blockquote><p><strong>发言人:{fayanren}</strong></p></blockquote>
<blockquote><p><strong>主持人:{zhuchiren}</strong></p></blockquote>
<p align="right">{mytime}</p>
<body>
<html>
""".format(
fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime
)
message_html = MIMEText(html_content, "html", "utf-8")
self.assertTrue(send_email(self.mail_sender, self.mail_receivers, message_html))
def test_qmail(self):
self.assertTrue(self.test_sendtext())
def test_gmail(self):
self.assertTrue(self.test_sendtext())
if __name__ == "__main__":
unittest.main()