In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to deal with email in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. send e-mail
The Python standard library provides smtplib to implement the SMTP protocol for sending mail. The standard library also provides email modules to help us build mail formats. SMTP (Simple Mail Transfer Protocol, simple Mail transfer Protocol) is a set of rules for sending messages from an active address to a destination address, which are used to control the way letters are transferred.
Get QQ Mail password (authorization code)
Send mail in plain text format
The code is as follows:
Import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# mailbox user name sender = 'dad@qq.com' (enter your mailbox) # mailbox password (some mailboxes are authorization codes) password =' 123456'(enter your password) # recipient email address, note that [] package is required This means that you can write multiple email addresses and send receiver = ['baby@qq.com',] (enter the email address of the person you want to send) # email body text =' Hello,baby'message = MIMEText (text, 'plain',' utf-8') # sender's explicit name message ['From'] = Header (' hold the head', 'utf-8') # recipient's explicit name message [' To'] = Header ('baby') 'utf-8') # email title message [' Subject'] = 'letter from Dad Please accept!' Try: # use QQ enterprise mailbox server to send smtp = smtplib.SMTP ('smtp.qq.com') # login smtp.login (sender, password) # send smtp.sendmail (sender, receiver, message.as_string ()) print (' email sent successfully!') # exit server smtp.quit () except smtplib.SMTPException as e: print ('Error! failed to send mail!' , e)
E-mail processing of Python basic Analysis
Send the execution result of the message in plain text:
3. Send mail in HTML format
The code is as follows:
Import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# mailbox user name sender = 'dad@qq.com' (enter your mailbox) # mailbox password (some mailboxes are authorization codes) password =' 123456'(enter your password) # recipient email address, note that [] package is required This means that you can write multiple email addresses and send them in groups receiver = ['baby@qq.com',] (enter the email address of the person you want to send) # email body msg ='
Hold on with your head.
Life goes on, learning goes on
22574 visits 24 original articles 128997 authors ranked 762followers 2020-02-22 join CSDN, get 212 likes, get 111comments, get 5662favorites # designated message body uses HTML format message = MIMEText (msg, 'html') 'utf-8') # sender's explicit name message [' From'] = Header ('hold on to the head', 'utf-8') # recipient's explicit name message [' To'] = Header ('baby',' utf-8') # email title message ['Subject'] =' Dad's letter Please accept!' Try: # use QQ enterprise mailbox server to send smtp = smtplib.SMTP ('smtp.qq.com') # login smtp.login (sender, password) # send smtp.sendmail (sender, receiver, message.as_string ()) print (' email sent successfully!') # exit server smtp.quit () except smtplib.SMTPException as e: print ('Error! failed to send mail!' , e)
E-mail processing of Python basic Analysis
Send the execution result of the message in HTML format:
Send mail with attachments
The code is as follows:
Import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.header import Header# mailbox user name sender = 'dad@qq.com' (enter your mailbox) # mailbox password (some mailboxes are authorization codes) password =' 123456' (enter your password) # recipient email address, note that [] package is required This means you can write multiple email addresses and group receiver = ['baby@qq.com',] (enter the mailbox of the person you want to send) # specify the message body to use the compound type message = MIMEMultipart () # the sender's explicit name message [' From'] = Header ('hold the head', 'utf-8') # the recipient's explicit name message [' To'] = Header ('baby') 'utf-8') # email title message [' Subject'] = 'letter from Dad Please accept!' # email body msg ='
Hold on with your head.
Life goes on, learning goes on
22574 visits 24 original articles 128997 authors ranked 762followers 2020-02-22 joined CSDN, got 212 likes, got 111 comments, got 562 collections'# email attached html file message.attach (MIMEText (msg, 'html') 'utf-8')) # add attachment attached_file = MIMEText (open (_ _ file__, encoding='utf-8'). Read (),' base64', 'utf-8') # specify that the file name of the attachment is different from the original file attached_file [' Content-Disposition'] = 'attachment Filename= "mail.py"'# Mail attachment message.attach (attached_file) try: # use QQ enterprise mailbox server to send smtp = smtplib.SMTP ('smtp.qq.com') # login smtp.login (sender, password) # send smtp.sendmail (sender, receiver, message.as_string ()) print (' email sent successfully!') # exit server smtp.quit () except smtplib.SMTPException as e: print ('Error! failed to send mail!' , e)
E-mail processing of Python basic Analysis
Execution result of sending mail with attachments:
E-mail to send pictures
The code is as follows:
Import smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.header import Header# mailbox user name sender = 'dad@qq.com' (enter your mailbox) # mailbox password (some mailboxes are authorization codes) password =' 123456'(enter your password) # recipient email address, note that [] package is required This means that you can write multiple email addresses and group receiver = ['baby@qq.com',] (enter the mailbox of the person you want to send) # use related to define the mail body of the embedded resource message = MIMEMultipart (' related') # the sender's explicit name message ['From'] = Header (' hold the head', 'utf-8') # the recipient's explicit name message [' To'] = Header ('baby') 'utf-8') # email title message [' Subject'] = 'letter from Dad Please accept!' # email body content = MIMEMultipart ('alternative') # html content msg ='
Hold on with your head.
Life goes on, learning goes on
Use the head to stick to the personal home page.
'' # email attach html file message.attach (MIMEText (msg, 'html',' utf-8')) # add picture with open ('csdn.png',' rb') as f: img01 = MIMEImage (f.read ()) # define the name of the resource as img01img01.add_header ('Content-ID' 'img01') # Mail attached picture message.attach (img01) try: # use QQ enterprise mailbox server to send smtp = smtplib.SMTP (' smtp.qq.com') # login smtp.login (sender, password) # send smtp.sendmail (sender, receiver, message.as_string ()) print ('email sent successfully!') # exit server smtp.quit () except smtplib.SMTPException as e: print ('Error! failed to send mail!' , e)
E-mail processing of Python basic Analysis
The result of the email execution of sending the picture:
Receive e-mail
There are two common protocols for accepting mail: POP3 and IMAP.
POP3 protocol (Post Office Protocol-Version3, Post Office Protocol version 3): allows the email client to download the mail on the server, but the operations on the client (such as moving mail, marking read, etc.) will not be fed back to the server, such as receiving 3 emails from the mailbox through the client and moving them to other folders, the mail on the mail server will not be moved synchronously.
IMAP protocol (Internet Mail Access Protocol, Internet mail access protocol): provides two-way communication between Webmail and the e-mail client, and any changes made on the client will be synchronized to the server. The mail is operated on the client, and the mail on the server will also be operated accordingly.
7. Download email using POP3 protocol
The code is as follows:
Import poplibfrom email.parser import Parser# login mailbox user name username = 'baby@qq.com' (enter your mailbox) # login mailbox password (some mailboxes are authorization codes) password =' 123456' (enter your password) # connect to the mailbox server pop_server = poplib.POP3 ('pop.qq.com') # print out the welcome text of the mailbox server print (pop_server.getwelcome ()) # login Mailbox server pop_server.user (username) pop_server.pass_ (password) # prints the status of the current account The first return value is the number of messages, and the second return value is print ('Server stat', pop_server.stat ()) # to get the mailing list resp, mails, octets = pop_server.list () print (mails) # to get the latest email (with the largest serial number) The message index counts from 1: index = len (mails) resp, lines, octets = pop_server.retr (index) content = b'\ r\ n'.join (lines) .decode ('utf-8') # parse the message msg = Parser (). Parsestr (content) # you can delete messages directly from the server # pop_server.dele (index) # close the connection pop_server.quit ()
E-mail processing of Python basic Analysis
Execution result:
B'+OK XMail POP3 Server v1.0 Service Ready (XMail v1.0)'
Server stat (15, 50814)
1 1255, 2 1286, 3 1310, 4 1398, 5 1458, 6 1450, 7 1602, 8 1633, 9 5001, 10 2347, 11 2371, 12 2267, 13 5033, 14 5077, 15 17326']
This is the end of the content of "how to deal with email in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.