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 to send mail automatically in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to send email automatically in Python. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1. Enable the SMTP service

In order to send mail automatically, we need to enable the SMTP service in the mailbox:

This is critical, do not forget to open SMTP, otherwise the email will not be sent successfully. Then you need to click below to generate the authorization code, which is the real password when sending e-mail using Python.

After the mailbox is set up successfully, you can start script development.

two。 Prepare for

Before you begin, make sure that Python and pip are successfully installed on your computer. Oh, if not, please visit this article: hyperdetailed Python installation Guide for installation. If you use Python for data analysis, you can directly install Anaconda:Python data analysis and mining helper-Anaconda

Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Of course, I recommend that you use the VSCode editor, Copy the code of this article, and install the dependency module by running commands in the terminal below the editor. What a comfortable thing: the best partner of Python programming-VSCode detailed guide.

Enter the following command at the terminal to install the dependent module we need:

Pip install yagmail

If you see Successfully installed xxx, the installation is successful. The full source code of this article can be obtained in the background reply: automatically send mail.

3. Write a script

The module you need to use today is yagmail, a very convenient Python email sending module, with which you can even send email with one-line command:

To make this method of sending mail more useful, we encapsulated it in a class:

There are many other things that can be encapsulated in this class, such as the log function, which is used to display the time:

In this way, when you need to display the time, you only need to call self.log, and you can change the sendmail function as follows:

When it is sent successfully, it displays:

PS D:\ CODE\ stock\ api > python.\ sendmail_yagmail.py2020-04-22 00:51:34: email sent successfully 4. Small example

Save the newly written class as sendmail.py, and then you can try a small example. Let's use a small script that automatically monitors whether the website is down as an example:

Request a website through requests.get, and you can get the status code using response.status_code. 200 is normal, and all other cases are abnormal. This test runs every ten minutes, so time.sleep (600).

This is how to send email automatically in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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

Internet Technology

Wechat

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

12
Report