Commit ac4e868b authored by huangqiang's avatar huangqiang

修改执行方式

parent a99055b4
[test]
student_new = reception/testsuites/apply_stuent_new.yml
\ No newline at end of file
# coding=utf-8
import smtplib
import common
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
class SendEmail:
def __init__(self):
self.__get_email_conf()
self.smt_obj = smtplib.SMTP_SSL(self.host, self.port)
def __get_email_conf(self):
self.host = common.readconf("email", "host")
self.port = common.readconf("email", "port")
self.from_adress = common.readconf("email", "from_adress")
self.password = common.readconf("email", "password")
self.to_adresses = common.readconf("email", "to_adresses").split(",")
def send_email(self, subject, text, html_msg=None, attachments=None):
message = MIMEMultipart()
# 发件人邮箱账号
message['From'] = self.from_adress
# 收件人邮箱账号
message['To'] = ";".join(self.to_adresses)
message['Subject'] = subject
message.attach(MIMEText(text, 'plain', 'utf-8'))
if html_msg:
message.attach(MIMEText(html_msg, 'html', 'utf-8'))
# 构造附件1,传送当前目录下的 test.txt 文件
if isinstance(attachments, list) and attachments:
for attachment in attachments:
att = MIMEText(open(attachment, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att["Content-Disposition"] = 'attachment; filename="%s"' % attachment
message.attach(att)
try:
self.smt_obj.login(self.from_adress, self.password)
self.smt_obj.sendmail(self.from_adress, self.to_adresses, message.as_string())
print("邮件发送成功")
self.smt_obj.quit()
except smtplib.SMTPException as e:
print("Error: 邮件发送失败", e)
self.smt_obj.quit()
if __name__ == "__main__":
send_email = SendEmail()
send_email.send_email("测试邮件", "测试内容")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment