How to use the smtplib module to send emails in Python 3.x
Overview:
Email is an important method of communication in modern society. Whether it is an individual or a business, it needs to be done through email. Information transfer and communication. In Python, we can use the smtplib module to send emails conveniently. This article will introduce how to use the smtplib module to send emails in Python 3.x, and give corresponding code examples.
Step 1: Import the smtplib module and related dependent modules
First, we need to import the smtplib module and other related dependent modules. Using smtplib to send emails in Python requires the following modules:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
Step 2: Connect to the SMTP server and log in
Next, we need to connect to the SMTP server and log in. SMTP (Simple Mail Transfer Protocol) is a protocol used to send emails. When using smtplib to send mail, we need to specify the address and port of the SMTP server, and whether authentication is required depending on the situation.
Email providers usually provide the address and port of the SMTP server as well as the corresponding account number and password. Taking QQ mailbox as an example, the SMTP server address is smtp.qq.com, the port is 465 or 587, and authentication is required.
smtp_server = 'smtp.qq.com'
smtp_port = 465
sender = 'your-email@qq.com'
password = 'your-email-password'
smtp_obj = smtplib.SMTP_SSL(smtp_server, smtp_port) # Create an SMTP object and use SSL to connect to the SMTP server
smtp_obj.login(sender, password) # Log in to the SMTP server
Step 3: Set up email Content and title
Before sending the email, we need to set the content and title of the email. Emails containing text content can be easily created using the MIMEText class of the email.mime.text module.
msg = MIMEText('mail content', 'plain', 'utf-8') # Create a MIMEText object. The first parameter is the mail content, the second parameter is the content type, and the third parameter is the content type. The parameter is the character encoding
msg['From'] = Header('sender', 'utf-8') # Set the sender
msg['To'] = Header('recipient' , 'utf-8') #Set the recipient
msg['Subject'] = Header('Email title', 'utf-8') #Set the email header
Step 4: Send the email
After setting the content and title of the email, we can send the email. Emails can be sent using the sendmail method of the SMTP object.
receiver = 'receiver-email@example.com' #Recipient email address
smtp_obj.sendmail(sender, receiver, msg.as_string()) #Send email
Steps Five: Close the SMTP connection
After sending the email, in order to release resources, we need to close the SMTP connection.
smtp_obj.quit() # Close the SMTP connection
The complete sample code is as follows:
import smtplib
from email.mime.text import MIMEText
from email .header import Header
smtp_server = 'smtp.qq.com'
smtp_port = 465
sender = 'your-email@qq.com'
password = 'your-email-password '
smtp_obj = smtplib.SMTP_SSL(smtp_server, smtp_port)
smtp_obj.login(sender, password)
msg = MIMEText('Mail content', 'plain', 'utf- 8')
msg['From'] = Header('Sender', 'utf-8')
msg['To'] = Header('Recipient', 'utf-8' )
msg['Subject'] = Header('Mail title', 'utf-8')
receiver = 'receiver-email@example.com'
smtp_obj.sendmail(sender, receiver, msg.as_string())
smtp_obj.quit()
Conclusion:
Sending emails using Python’s smtplib module is very simple and can be completed with just a few lines of code. The email content, title, recipients and other information in the above sample code need to be modified according to the actual situation. I hope this article can help you learn how to use the smtplib module to send emails in Python 3.x.
The above is the detailed content of How to use the smtplib module to send emails in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!