cmd中直接安装:
pip install pyEmail
一般用PyEmail,所以直接pip install PyEmail 就行 ,然后在import smtplib
实际应用的代码是:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
class SendEmail():
def send_attach(self, file_name):
msg_from = "1535257110@qq.com"
pwd = "mgjobdddbetjjijd"
to = "liuhanxia@51faguanggao.com"
message = MIMEMultipart()
message['From'] = Header("物联测试平台系统", 'utf-8')
message['To'] = Header("刘晗霞", 'utf-8')
subject = 'Python 自动化测试报告'
message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText('这是自动化测试Python 邮件发送测试结果……', 'plain', 'utf-8'))
att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename='+file_name
message.attach(att)
try:
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.login(msg_from, pwd)
smtpObj.sendmail(msg_from, to, message.as_string())
except smtplib.SMTPException as e:
print("Error: 无法发送邮件" + e.strerror)
def send_normal(self,subject,contect):
msg_from = "1535257110@qq.com"
pwd = "mgjobdddbetjjijd"
to = "liuhanxia@51faguanggao.com"
msg = MIMEText(contect)
msg["Subject"] = subject
msg["From"] = msg_from
msg["To"] = to
try:
smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtpObj.login(msg_from, pwd)
smtpObj.sendmail(msg_from, to, msg.as_string())
print("发送成功!")
except Exception as e:
print("发送失败!详情:", e)
发送附件:
file = “…/report/standard_debug_result.html”
SendEmail.send_attach(SendEmail, file)