import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Common sense of username and password sent by email: third-party authorization
_ user='your [email protected]'
_ pwd='enter password'
now=time.strftime('%Y-%m-%d-%H-%M-%S')#Get timestamp
classsendEmail:
def send_email(self,email_to,filepath):
# email_to recipient
# filepath the email address you want to send
# As the name suggests, Multipart is divided into multiple parts
msg=MIMEMultipart()
msg['Subject']=now +'Qinghan test report'
msg['From']=_user
msg['To']=email_to
#- - - This is the text part---
part=MIMEText('This is an automated test result, please check!')
msg.attach(part)
#- - - - This is the attachment part-----
# Send multiple attachments
# path=['1','2','3']#Many paths
# for item in path:
# part=MIMEApplication(open(item,'rb').read())
# part.add_header('Content-Disposition','attachment',filename=filepath)
# msg.attach(part)
# It can only read files but not folders, so you can put the files in the list and make a for loop below.
# Send an attachment
part=MIMEApplication(open(filepath,'rb').read())
part.add_header('Content-Disposition','attachment',filename=filepath)
msg.attach(part)
s=smtplib.SMTP_SSL('smtp.163.com',timeout=30)#Connect to smtp mail server, the default port is 25
s.login(_user,_pwd)#Login server
s.sendmail(_user,email_to,msg.as_string())#send email
if __name__ =='__main__':sendEmail().send_email('your mailbox/Someone else's [email protected]',r'C:\Users\18210\Desktop\python\jiaoben\ningmenban\API_AUTO _1\test_result\html_report\test_api.html')
Welcome to scan the QR code!
Recommended Posts