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 use custom functions for Python to send email

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

Share

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

This article introduces how to use custom functions for Python to send e-mail, the content is very detailed, interested friends can refer to, hope to be helpful to you.

About a month ago, a friend asked me how to use Python to send email, and to be honest, I really didn't come across this in my previous resume, so I didn't help him solve the problem very well at that time. But I have been thinking about this question all the time. What if I also encounter this problem in the future? You can't cram on the spur of the moment. You have to plan ahead.

Some basic information must be prepared before the message occurs, such as the sender's email address, sender's email password, recipient's email address, sender's nickname, recipient's nickname, message subject, and message content. These contents are customized, so the rest should be sent by e-mail. Sending mail using Python requires the use of email module (mainly for email configuration) and smtplib module (for email sending), so let's get started! (here, take my QQ Mail as an example to send content to other emails. )

Taking into account your understanding of the code, I am equipped with the corresponding comments in the custom function, if you still do not understand what you can leave a message. It should be noted that if you also use QQ Mail for testing, you must configure the authorization code of the third-party client mailbox, otherwise the email will be published.

Configuration of authorization codes

Step 1: log in to your own QQ Mail

Step 2: click "Settings" in the mailbox to enter the "account" bar. Drop down and you will see the following screenshot:

Step 3: click * * enable * * in the figure, send SMS messages according to the following figure, and you will get the authorization code (this authorization code is used to configure the my_pass parameter in the custom function mail).

Mail test

If the above authorization codes are configured with OK, you can use the above 9 lines of custom functions and test them happily! Here, QQ Mail is used as the sender and my mailbox as the receiver. The code is as follows:

Try: mail_msg = "

Dear Liu Shunxiang,

Hello, I'm sorry to bother you, this is a Python mail test, you can ignore it when you see it.

More than 2 months nearly 600 pages of data analysis and mining PPT to share with you!

"" # call the function (login password needs to be changed to your own) mail ('1029776077 login qq.compositionMagic' kjwpzipnmbfbb.') 'Liu Shunxiang', 'Liu Shunxiang', mail_msg) print ('email sent successfully!') except: print ('email delivery failed!')

If nothing happens, the result of the function will be "the message was sent successfully!" And then go back to my 163 email, and I will see something like this:

Batch transmission

You might think, is it so troublesome to send an email? I just log in to the mailbox and send it. When there is a problem, you may need to send bulk emails to the target customers in the work scenario, and then you will look scum if you do it manually. If you want to send mail in bulk, the easiest thing to think of is the for loop. Here, I asked my friend for a mailbox as a test. The code is as follows:

Surprise constantly, all sent successfully, and the speed is also very fast, the sending of three e-mails is estimated to be about 1 second. The effect is as follows:

On how to use custom functions to send Python e-mail to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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