Send and receive emails in python
About emails
Before the university, there was almost no mailbox, so I basically couldn't feel its existence and I don't know how to use it. However, after the university, as more and more people know each other, the knowledge is getting wider and wider, email has become an important communication tool. For some university courses and assignments, you need to send emails to teachers, email addresses to register websites, and email addresses to find jobs. What is the principle of email address?
Send email
SMTP protocol
SMTP (Simple Mail Transfer Protocol) is a Simple Mail Transfer Protocol. It is a set of rules used to send Mail from the source address to the destination address, which controls the Transfer mode of Mail. The SMTP protocol is a TCP/IP protocol cluster that helps each computer locate the next destination when sending or transferring letters. The server specified by the SMTP protocol can send e-mail to the recipient's server for a few minutes.
SMTP module in python
Basic Steps for using SMTP
1. Connect to the server
2. log on
3. Send service requests
4. Exit
Import smtplibfrom email import encodersfrom email. header import Headerfrom email. mime. text import MIMETextfrom email. utils import parseaddr, formataddrdef send_email (from_addr, to_addr, subject, password): msg = MIMEText ("mail body", 'html', 'utf-8 ') msg ['from'] = U' <% s> '% from_addr msg ['to'] = U' <% s>' % to_addr msg ['subobject'] = Subject smtp = smtplib. SMTP_SSL ('smtp .163.com ', 465) smtp. set_debuglevel (1) smtp. ehlo ("smtp.163.com") smtp. login (from_addr, password) smtp. sendmail (from_addr, [to_addr], msg. as_string () if _ name _ = "_ main _": # The password here is the client logon authorization code entered when the smtp service is enabled, not the mailbox password # Now many mailboxes need to enable smtp before sending the mail send_email (u "from_addr", u "to_addr", u "topic", u "password ")
The preceding example shows that smtplib is used to send emails and SSL encryption is used. This is relatively secure. An email is used to construct the content of the emails. Here, the content sent is plain text, I think the most important thing to note is the password of the email here. In addition, the mail servers and ports of different companies may be different. You can check them before using them.
Here are several common examples:
Receive email
POP3 and IMAP
POP refers to the Post Office Protocol, which allows users to access emails on the mailbox server and allow users to store emails on the server to a local host (that is, their own computer, delete the emails stored on the mail server, while the POP3 server receives emails from the mail server following the POP3 protocol.
Later, the IMAP Protocol (Interactive Mail Access Protocol) emerged, that is, the Interactive Mail Access Protocol. Unlike POP3, after IMAP is enabled, emails received on the email client are retained on the server, and operations on the client are reported to the server, such as deleting emails and marking read emails, emails on the server will also perform the corresponding action.
Use POP3
The python poplib module supports POP3. The basic steps are as follows:
1. Connect to the server
2. log on
3. Send a service request
4. Exit
Common poplib methods:
Example
from poplib import POP3p = POP3('pop.163.com')p.user('xxxxxxx@163.com')p.pass_('xxxxxxxx')p.stat()...p.quit()
Use IMAP
The imaplib package in python supports IMAP4.
Common Methods:
Example
import getpass, imaplibM = imaplib.IMAP4()M.login(getpass.getuser(), getpass.getpass())M.select()typ, data = M.search(None, 'ALL')for num in data[0].split(): typ, data = M.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, data[0][1])M.close()M.logout()
For more details, see the official documentation:
• Smtplib module: https://docs.python.org/2/library/smtplib.html
• Email module: https://docs.python.org/2/library/email.html
• Poplib module: https://docs.python.org/2/library/poplib.html
• Imaplib: https://docs.python.org/2/library/imaplib.html
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.