程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python ten lines of code to send mail

編輯:Python

Preface

Automated test framework , Generally, a failure notification is sent to the tester after the execution fails . Common notification methods : Mail notification 、 Enterprise wechat notice 、 Nail notice, etc , Email notification is one of the most common and formal notification methods .

Let's talk this time Python How to send email notification in , We all know Python Built in right SMTP Support for , You can send plain text 、 Rich text 、HTML And so on , however SMTP It's a little complicated to write code .

So I hope to have a lightweight solution , Until I met yagmail.

Get ready

With 163 Mailbox as an example , Before coding , We need to turn on SMTP service .

Manually add an authorization code . account number 、 Authorization code 、 The server address is used to connect to the login mailbox server .

ymail Introduce

stay Python Send email in ,yagmail Probably the easiest way to use it right now .yagmail Just a few lines of code , Can realize the function of sending mail . comparison zmail,yagmail The way to send email is more simple and elegant .

github: https://github.com/kootenpv/yagmail

install

Support at the same time python2 and python3 Two versions installed

pip install yagmail
pip3 install yagmail

Write a script

It mainly introduces the two common carriers of sending text and sending attachments .

Send text

First, simply send a piece of text ,contents by list Structure to wrap text .

Code example :

import yagmail
# Connect to server
# user name 、 Authorization code 、 Server address
yag = yagmail.SMTP(user='[email protected]', password=' Authorization code ', host='smtp.163.com')
# next , adopt send() function , Send the mail out
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3']
yag.send('[email protected]', 'subject', contents)
# Close the connection
yag_server.close()

Available at the receiving end Mailing list, See the code just now to send an email , There is almost no delay in receiving emails .

Sending attachments

The following code loads multiple test reports locally and sends them as attachments

Code example :

import yagmail
# Connect to server
# user name 、 Authorization code 、 Server address
yag = yagmail.SMTP(user='[email protected]', password=' Authorization code ', host='smtp.163.com')
# next , adopt send() function , Send the mail out
# Send object list
email_to = ['[email protected]', ]
email_title = ' Test report '
email_content = " This is the test report "
# Attachment list
email_attachments = ['./report-1652541422.html','./reports/report-1652540875.html']
# Send E-mail
yag.send(email_to, email_title, email_content, email_attachments)
# Close the connection
yag_server.close()

You can see two test reports in the attachment of the email , Click to preview .

Conclusion

It says Python Use in ymail Some basic uses for sending mail , And it is often used in my daily work , It is highly recommended that you use .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved