This is the 26th day of my participation in the August More Text Challenge

The Python Smtplib tutorial shows how to use the smtplib module to send email in Python. To send email, we use a Python development server, the Mailtrap online service, and a shared web-hosted mail server.

Smtplib library

Python uses the smtplib library to send mail

SMTP

Simple Mail Transfer Protocol (SMTP) is a communication protocol used for email transmission. Is Is an Internet standard that was first defined in 1982 by RFC 821 and updated in 2008 by RFC 5321 to extend SMTP. Mail servers and other mail transport agents use SMTP to send and receive mail.

Smtplib is a Python library for sending email using the Simple Mail Transfer Protocol (SMTP). Smtplib is a built-in module; We don’t need to install it. It abstracts all the complexities of SMTP.

Mail server

To actually send E-mail, we need to have access to the mail server. Python comes with a simple development mail server. Mailslurper is an easy-to-use local development server. The shared web hosting provider gives us access to the mail server. We can find the details in the account.

The basic COMMANDS of SMTP include: \

HELO identifies the user to the server \ MAIL Initializes the MAIL transfer MAIL from:\ RCPT identifies a single MAIL recipient; RCPT to:\ DATA After one or more RCPT commands to indicate that all MAIL recipients have been identified and to initiate DATA transmission. End \ VRFY used to verify whether the specified user/mailbox exists; For security reasons, the server often disables this command \ EXPN to verify the existence of a given list of mailboxes and to expand the list of mailboxes. It is also often disabled. \ HELP queries what command the server supports MAIL FROM specify sender address \ RCPT TO specify receiver addressCopy the code

In actual combat

SMTP service is usually disabled by default, we have to enable it first

 

 

2. The Python code is as follows


# smtplib is used for sending mail
import smtplib
from email.mime.text import MIMEText
# email is used to build message content
from email.header import Header
# used to build message headers
Sender's information: sender's mailbox, authorization code of mailbox 126
from_addr = '[email protected]'
password = 'POP3/SMTP service authorization password, obtained in the previous step '
 
# Recipient email address
to_addr = '[email protected]'
 
# sending server
smtp_server = 'smtp.126.com'


"" "title "" "
head="Mailbox verification code"
"" "body "" "
text="[TRobot] your verification code 32123, this verification code is valid for 5 minutes, do not leak to others!"

 
The first parameter is content, the second parameter is format (plain is plain text), and the third parameter is encoding
msg = MIMEText(text,'plain'.'utf-8')
 
# Message headers
msg['From'] = Header(from_addr)
msg['To'] = Header(to_addr)
msg['Subject'] = Header(head)
 
# enable the sending service, which uses encrypted transmission
#server = smtplib.SMTP_SSL()
server=smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)
# Login to sender mailbox
server.login(from_addr, password)
# Send email
server.sendmail(from_addr, to_addr, msg.as_string())
# shutdown server
server.quit()

Copy the code