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 Python to realize the function of sending email

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Python to achieve email function". In daily operation, I believe many people have doubts about how to use Python to achieve email function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python to achieve email function". Next, please follow the editor to study!

Prepare for

Editor: sublime text 3

Modules: smtplib and email

Project implementation

1. Installation

Pip install smtplib pip install email

Note: there is a small pit, that is, the installation of smtplib can not be installed directly as above, so you have to install PyEmail first, because your smtplib is integrated in this module, just like the pillow module integrated in PIL, another normal installation can be done.

two。 Activate pop3 SMTP imap service to learn about email authorization code

If you want to send email to any mailbox, you must first activate the above services to enable email to communicate, and you must also have an email authorization code, such as QQ Mail:

Open QQ Mail, select Settings-account, and then start the service.

After enabling the service, click to generate the authorization code and save the authorization code. To get the authorization code, you only need to send text messages or dynamic tokens with your registered mobile phone number.

3. Build mail ports and establish connections

Import smtplib sm=smtp.SMTP () # initialize connection sm.connect ('email server address', 'port') # establish connection sm.login ('mailbox account', 'mailbox password / authorization code') # login account sm.sendmail ('email sender', 'email recipient', 'email content') # send email sm.quit () # close the connection and end the mail service

After knowing the above knowledge, let's try to log in. Let me take QQ Mail as an example:

Such a sign indicates that the login is successful. By the way, why did I not configure the mail server port here? because the server has been configured for us. The default is port 25. If you are worried about the security of your email, such as being intercepted by hackers, you can also use ssl link to transfer:

Similarly, its port is configured by default, and its port number is 465. For security, we choose this option.

4. Build the content part of the message

Here we need to use the email module, we all know that e-mail can generally send a lot of things, such as text, pictures, files and so on, so let's take a look.

I. text

Import module

From email.mime.text import MIMEText

Fill text

Before that, we need to know how to use it:

MIMEText ('message content', 'type', 'code')

The message content is a string

Type: text/plain text/html

Code: utf-8 gbk

Construct text

MIMEText ('hello','text/plain','utf-8')

Construct hypertext

MIMEText ('Click here for surprise', 'text/html','utf-8')

Let's do it in practice.

Very successful. I got the e-mail. It was the mailbox 2091500484 that sent it to me. Of course, we only achieved the simplest function.

We need to standardize its format, such as adding a beginning and end to it. We need to import the module that builds the full content of the email:

From email.header import Header

Then set the head, content, and tail.

Msg1 ['From'] = Header (' are you a pig') # set the sender nickname msg1 ['To'] = Header (' hwhrr123321@163.com') # set the recipient nickname msg1 ['Subject'] = Header (' I'm a pig') # set the title

You can see, don't you think it's interesting? come and try it, ha.

2. Pictures

After sending the text, we still want to send a picture, what should we do? Don't panic, you need to import the module that sends the image first:

From email.mime.image import MIMEImage

Then we read the picture file and add it to the email.

Ff=open ('1.jpg fd.add_header fd.add_header Content-ID','1.jpg'). Read () # Open file fd=MIMEImage (ff,'subtype') # initialize JPG (' jpg) # add to the header

You can see that the picture is not displayed, so what's going on? Oh, the original image is based on attachments, either html or attachments, but both need the support of the attachment module. Let's import the attachment module below:

From email.mime.multipart import MIMEMultipart

1. Insert a picture into html

That is, insert the picture into the body, not in the form of an attachment.

Msg3 = MIMEMultipart ('related') msg3 [' From'] = 'are you a pig' msg3 ['To'] =' hwhrr123321@163.com' msg3 ['Subject'] =' I am a pig 'msg4 = MIMEMultipart (' alternative') # build an attachment msg3.attach (msg4) # introduce the attachment to another attachment text= "

"" msg4.attach (MIMEText (text, 'html',' utf-8')) # insert the html into the attachment ff=open ('2.jpg recording ff.close rb') img = MIMEImage (ff.read ()) # read the picture into the attachment ff.close () img.add_header (' Content-ID','

') # add the picture header msg3.attach (img) # add the picture to the attachment sm.sendmail (' 2091500484roomqq.compositional journal journal hwhrr123321resume 163.comet copyright msg3.assigning string ()) # send sm.quit ()

As you can see, the process is still more complex, than simple attachments to add pictures trouble points, mainly nesting a layer of attachment structure.

two。 Introduce the picture into the attachment

This is easier to achieve. As shown in the figure:

III. Documents

One of the things we need to consider before sending a file is that we should read it in binary form and then add it to the attachment.

1. Read a file

Here we need to construct a data stream of base64 to read the file:

Msg6=MIMEMultipart () txt=MIMEText (open ('fd.txt','rb'). Read (),' base64', 'utf-8')

two。 Set the transport type

Txt ["Content-Type"] = 'application/octet-stream'

3. Set attachment name

Txt ["Content-Disposition"] = 'attachment; filename = "fd.txt"'

4. Add a file to an attachment

Msg6.attach (txt)

Finally, the file was successfully added to the attachment.

Project summary

That's all I've learned about email delivery, and if you want to show the process of email transmission visually, just add:

Sm.set_debuglevel (1) at this point, the study on "how to use Python to send email" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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