# Content :1. use Python Code send text mail 2. send out html mail Sending attachments picture
# smtp Send E-mail smtp Mail Transfer Protocol smtplib library : Simple encapsulation Simple
import smtplib
## ① Connect to mailbox server
# Post Office smtplib.SMTP_SSL( Email connection address , Port number ) smtp.xx.com
# 163 mailbox ;smtp.163.com Port number
# qq mailbox ; smtp.qq.com Port number 465 perhaps 587
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com',465)
print(con)
## ② Login mailbox
# User name and password Authorization code for password qq mailbox -- Set up -- Account --POP3/SMTP Service is open Authorization code
con.login(user='[email protected]',password='xxx')
## ③ Sender account number
sender = '[email protected]'
## ④ Recipient account number
reciever = '[email protected]'
## ⑤ send content _text Content , _subtype='plain' Text , _charset=None Character set
htmlContent = "<a href='http://www.baidu.com'> I am a html Code </a>"
message = MIMEText(htmlContent,'html','utf-8')
# message = MIMEText(' Hello, little lucky , I'm Zha Zha Qiu ','plain','utf-8')
## ⑥ Set header content
# title Sender The recipient
# title
message['Subject'] = ' A letter sent to little lucky '
# Give a person
message['from'] = sender
message['to'] = reciever
## ⑦ Send E-mail
# Has the email been sent Is there a problem , I'm afraid there is something wrong with the email sent
try:
# Send E-mail
con.sendmail(sender,reciever,message.as_string())
print(' Mail sent successfully ')
except Exception as e:
print(' There is no way to send email , Wrong report ')
# send out html Format message
# Send test report , send out html The test report of Send it to test related personnel
2. send out html Test report to relevant testers
import smtplib
from email.mime.text import MIMEText
con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='[email protected]',password='xxx')
# Sender account number
sender = '[email protected]'
# Recipient account number
reciever =['[email protected]','[email protected]']
# send out html Contents of the document Read out
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')
# title
message['Subject'] = ' A letter sent to little lucky '
# Give a person
message['from'] = sender
message['to'] = ';'.join(reciever)
con.sendmail(sender,reciever,message.as_string())
3. send out html The attachment
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='[email protected]',password='xxx')
# Sender account number
sender = '[email protected]'
# Recipient account number
reciever ='[email protected]'
# send content
# Attachment examples The box
message = MIMEMultipart()
# Get the content
with open(r'./files/2020-11-16 21-15-18test_report.html','rb') as f:
content = f.read()
# The content is written on stationery
files2 = MIMEText(content,'base64','utf-8')
# Name the paper Don't write in Chinese In English
files2['Content-Disposition'] = 'attachment;filename="2.html"'
# Put your letter in a box and send it
message.attach(files2)
msg = MIMEText(' Text content ','plain','utf-8')
message.attach(msg)
# title
message['Subject'] = ' This is a letter from home '
# Give a person
message['from'] = sender
message['to'] = reciever
con.sendmail(sender,reciever,message.as_string())
# Be careful : In daily work , send out html The test report of , Compressed into a compressed file before sending
# Manual compression Code compression zipfile
4. Send pictures
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='[email protected]',password='xxx')
# Sender account number
sender = '[email protected]'
# Recipient account number
reciever =['[email protected]']
# CC account
cs = ['[email protected]']
# Send pictures
message = MIMEMultipart()
# Picture content
with open('./files/image.jpg','rb') as f:
image1 = f.read()
# Put it on paper , picture
image_data = MIMEImage(image1)
image_data['Content-Disposition'] = 'attachment;filename="2.jpg"'
message.attach(image_data)
msg = MIMEText(' The body of the picture ','plain','utf-8')
message.attach(msg)
# title
message['Subject'] = ' This is a letter from home '
# send out
message['from'] = sender
message['to'] = ';'.join(reciever)
message['cc'] = ';'.join(cs)
con.sendmail(sender, reciever+cs, message.as_string())
5.zmail
# install : pip install zmail
import zmail
# subject title
# Content_text The text of the email
# Content_html Content
# Attachments: The attachment
# Give a person
sender = {
'username':'[email protected]','password':'xxx'}
# Login mailbox
server = zmail.server(sender['username'],sender['password'])
# Email content
mail_content = {
'subject':' I'm the title ',
# 'Content_text':' I am the content of the email http://www.baidu.com',
'Content_html':'<a href="http://www.baidu.com"> hi , Hello </a>',
# 'Attachments':'./files/2020-11-16 21-15-18test_report.html'
'Attachments':'./files/test.docx'
}
# The recipient
reciever=['[email protected]']
# Send E-mail
server.send_mail(reciever,mail_content,cc=['[email protected]'])
# Be careful
# Content_text and Content_html Only one can be used