1.发送文本邮件
import smtplib
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com',465)
print(con)
con.login(user='834628301@qq.com',password='xxx')
sender = '834628301@qq.com'
reciever = '834628301@qq.com'
htmlContent = "<a href='http://www.baidu.com'>我是html代码</a>"
message = MIMEText(htmlContent,'html','utf-8')
message['Subject'] = '发送给小幸运的一封信'
message['from'] = sender
message['to'] = reciever
try:
con.sendmail(sender,reciever,message.as_string())
print('邮件发送成功')
except Exception as e:
print('没有办法发送邮件,报错了')
2.发送html测试报告给到相关测试人员
import smtplib
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')
sender = '834628301@qq.com'
reciever =['834628301@qq.com','yunshaliying@sina.com']
with open(r'./files/2020-11-16 21-15-18test_report.html','rb') as f:
htmlContent = f.read()
message = MIMEText(str(htmlContent,encoding='utf-8'),'html','utf-8')
message['Subject'] = '发送给小幸运的一封信'
message['from'] = sender
message['to'] = ';'.join(reciever)
con.sendmail(sender,reciever,message.as_string())
3.发送html附件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')
sender = '834628301@qq.com'
reciever ='834628301@qq.com'
message = MIMEMultipart()
with open(r'./files/2020-11-16 21-15-18test_report.html','rb') as f:
content = f.read()
files2 = MIMEText(content,'base64','utf-8')
files2['Content-Disposition'] = 'attachment;filename="2.html"'
message.attach(files2)
msg = MIMEText('正文内容','plain','utf-8')
message.attach(msg)
message['Subject'] = '这是一封家书'
message['from'] = sender
message['to'] = reciever
con.sendmail(sender,reciever,message.as_string())
4.发送图片
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')
sender = '834628301@qq.com'
reciever =['834628301@qq.com']
cs = ['yunshaliying@sina.com']
message = MIMEMultipart()
with open('./files/image.jpg','rb') as f:
image1 = f.read()
image_data = MIMEImage(image1)
image_data['Content-Disposition'] = 'attachment;filename="2.jpg"'
message.attach(image_data)
msg = MIMEText('图片的正文','plain','utf-8')
message.attach(msg)
message['Subject'] = '这是一封家书'
message['from'] = sender
message['to'] = ';'.join(reciever)
message['cc'] = ';'.join(cs)
con.sendmail(sender, reciever+cs, message.as_string())
5.zmail
import zmail
sender = {'username':'834628301@qq.com','password':'xxx'}
server = zmail.server(sender['username'],sender['password'])
mail_content = {
'subject':'我是标题',
'Content_html':'<a href="http://www.baidu.com">嗨,你好</a>',
'Attachments':'./files/test.docx'
}
reciever=['834628301@qq.com']
server.send_mail(reciever,mail_content,cc=['yunshaliying@sina.com'])