Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python send email?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how python sends emails. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Introduction

Some friends may ask: what is the use of the python email function? In fact, the email function is really useful. The editor once came into contact with an automated testing project, which deployed the project on the server, and the running result was to send the test report to the developer's mailbox through the mail function. Of course, the e-mail library used for that project is not a simple library like the stmplib library, but they have a lot of similarities in use, and the stmplib library is sufficient for lightweight use.

For a send mail function, as long as it is possible to send mail is sufficient. This is reflected in the stmplib library.

Installation

Stmplib is a built-in library for python and no additional installation is required.

Use

The first method: when a stmp server exists locally:

Import smtplibfrom email.mime.text import MIMETextfrom email.header import Headersender = 'from@yisu.com' # Sender receivers = [' 429240967 email qq.com'] # recipient, you can set it to your QQ Mail or other mailbox # three parameters: the first is the text content, and the second plain sets the text format The third utf-8 setting encodes message = MIMEText ('Python mail sending test.', 'plain',' utf-8') message ['From'] = Header ("W3Cschool tutorial",' utf-8') message ['To'] = Header ("test",' utf-8') subject = 'Python SMTP mail test' # string to be used as title message ['Subject'] = Header (subject 'utf-8') # write the string to be used as the title to the email try: smtpObj = smtplib.SMTP (' localhost') smtpObj.sendmail (sender, receivers, message.as_string ()) print ("message sent successfully") except smtplib.SMTPException: print ("Error: unable to send message")

This approach requires a local STMP server, after which we can use localhost as the stmp server address, but in most cases we do not have a local stmp server, so we can use the following approach.

The second way: use a third-party STMP server:

Many mailbox service providers provide stmp services. For example, 163mailboxes and QQ Mail, which are common in China, provide corresponding stmp services. We can use these stmp services to send mail (instead of local stmp services).

Import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# third-party SMTP service mail_host= "smtp.XXX.com" # set up server mail_user= "XXXX" # username mail_pass= "XXXXXX" # password sender = 'from@yisu.com' # Sender receivers = [' 429240967 sender qq.com'] # receive mail Can be set to your QQ Mail or other mailbox message = MIMEText ('Python email sending test.', 'plain',' utf-8') message ['From'] = Header ("W3Cschool tutorial",' utf-8') message ['To'] = Header ("test",' utf-8') subject = 'Python SMTP email test' message ['Subject'] = Header (subject) 'utf-8') try: smtpObj = smtplib.SMTP () smtpObj.connect (mail_host, 25) # 25 is the SMTP port number smtpObj.login (mail_user,mail_pass) smtpObj.sendmail (sender, receivers, message.as_string ()) print ("message sent successfully") except smtplib.SMTPException: print ("Error: unable to send message")

Note: sender should be changed to the user name of the sender, otherwise an error will be reported. In addition, the password for mailbox is not a simple mailbox password.

Third-party mail server support

Many users do not have a local stmp server, but use the stmp server provided by the mailbox service provider. Take QQ Mail as an example, we can use the QQ account + @ qq.com as the user name and the QQ password as the password to access QQ Mail. However, the qq password cannot be used as the password in the above code, which involves security issues. Usually, the password used by the stmp service is a string called authorization code, which means that the mail_pass of the above code is actually an authorization code. This authorization code can be obtained from the mailbox settings.

These are all the contents of the article "how to send email by python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report