This article is participating in Python Theme Month. See the link for details.

Python includes the smtplib and email modules in custom modules, which are combined to send formatted E-mail.

SMTP, POP3, IMAP

SMTP protocol can send mail, POP3/IMAP protocol can receive mail, the figure shows a complete cross-server mail transfer process.

1.1 SMTP (Sending)

The full name of SMTP is Simple Mail Transfer Protocol. It is a set of specifications used to transport messages from source addresses to destination addresses to control how messages are forwarded. SMTP belongs to the TCP/IP protocol family, which helps each computer find the next destination when sending or forwarding a letter. The SMTP server is a mail sending server that complies with THE SMTP protocol.

SMTP authentication simply means that you must provide an account name and password to log in to an SMTP server, which makes it impossible for spammers to spread spam. SMTP authentication is added to protect users from spam.

1.2 POP3 (Receiving)

POP3 is short for Post Office Protocol 3, version 3 of the Post Office Protocol, which specifies how to connect a personal computer to a mail server on the Internet and an electronic Protocol for downloading E-mail. It was the first offline protocol standard for Internet E-mail. POP3 allowed users to store mail from a server to a local host (that is, their own computer) while deleting the mail stored on the mail server, which was a POP3 protocol receiving mail server used to receive E-mail.

1.3 IMAP (Receiving)

IMAP stands for Internet Mail Access Protocol, or INTERACTIVE Mail Access Protocol. It is a standard Mail Access Protocol similar to POP3. However, after IMAP is enabled, the emails you receive from the email client are still stored on the server, and all the operations on the client are reported to the server, such as deleting emails or marking the emails as read. The emails on the server will also take corresponding actions. Therefore, whether you log in to the mailbox from the browser or the client software, the mail and status are the same.

IMAP vs POP3

Operating position Operating content IMAP POP3
inbox Read, mark, move, delete messages, etc The client synchronizes with mailbox updates Only inside the client
outbox Save to sent The client synchronizes with mailbox updates Only inside the client
Creating a folder Create a custom folder The client synchronizes with mailbox updates Only inside the client
The draft Save drafts The client synchronizes with mailbox updates Only inside the client
Garbage folder Receive messages that mistakenly remove spam folders support Does not support
spam Receive messages that have been moved to the advertising mail folder support Does not support

Overall, IMAP provides a more convenient and reliable experience for users. POP3 is more prone to losing mail or downloading the same mail multiple times, but IMAP avoids these problems by bidirectional synchronization between mail clients and Webmail.

2. Smtplib

The first step is to create an SMTP object, each of which is used to connect to a server.

import smtplib
server = smtplib.SMTP('smtp.163.com', 25)
Copy the code

Then log in to the SMTP server.

server.login("youremailusername", "password")
Copy the code

Finally, send an email.

msg = "
Hello!" # The /n separates the message from the headers
server.sendmail("[email protected]", "[email protected]", msg)
Copy the code

The SMtPLib module is mainly responsible for sending emails. It is not responsible for constructing email contents, including sender, recipient, subject, body, attachment, etc., which are constructed through the Email module.

3. Use of email

First import the class library we are going to use.

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
Copy the code

The header is then combined.

fromaddr = "[email protected]" toaddr = "[email protected]" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr  msg['Subject'] = "Python email"Copy the code

Next, append the body of the E-mail message to the MIME message.

body = "Python test mail"
msg.attach(MIMEText(body, 'plain'))
Copy the code

4. Complete examples

import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText fromaddr = "[email protected]" toaddr = "[email protected]" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "Python email" server = smtplib.SMTP('smtp.163.com', 25) server.ehlo() server.starttls() server.ehlo() Server.login (fromaddr, "auth password") # Text = msg.as_string() server.sendmail(fromaddr, toaddr, text)Copy the code

Of course, this is just a simple example, but if you want to send text, HTML, images, attachments and other complex scenes, please refer to the official documentation.