SMTP (Simple Mail Transfer Protocol) is the Simple Mail Transfer Protocol. It is a set of rules used to transfer mail from a source address to a destination address, and it controls the transfer of mail.
Python's smtplib provides a very convenient way to send emails. It simply encapsulates the smtp protocol.
The SMTP object syntax is as follows:
import smtplib
smtpObj = smtplib.SMTP([host [, port [, local_hostname]]])
Parameter Description:
The Python SMTP object uses the sendmail method to send mail, the syntax is as follows:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
Parameter Description:
Here we should pay attention to the third parameter, msg is a string, which means mail. We know that emails are generally composed of title, sender, recipient, email content, attachments, etc. When sending emails, pay attention to the format of msg. This format is the format defined in the smtp protocol.
Examples
The following implementation examples require that your machine has installed a service that supports SMTP, such as sendmail.
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender ='[email protected]' #sender
receiver ='[email protected]' #receiver
# Three parameters: the first is the text content, the second plain sets the text format, and the third is utf-8 Set encoding
message =MIMEText('QQ sends test files like 163....','plain','utf-8')
message['From']=Header('Python tutorial...') #sender
message['To']=Header('test','utf-8') #recipient
subject ='Python SMTP test'
message['subject']=Header('utf-8')try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receiver, message.as_string())print("Mail sent successfully")
except smtplib.SMTPException:print("Error:Unable to send mail")
# Mail sent successfully
If our machine does not have sendmail access, we can also use the SMTP access of other mail service providers (QQ, Netease, Google, etc.).
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# Third-party SMTP service
host="smtp.qq.com" #Set up server
user="973708513" #username
password="xxxxxx" #Password
sender ='[email protected]' #sender
receiver ='[email protected]' #receiver
# Three parameters: the first is the text content, the second plain sets the text format, and the third is utf-8 Set encoding
message =MIMEText('QQ sends test files like 163....','plain','utf-8')
message['From']=Header('Python tutorial...') #sender
message['To']=Header('test','utf-8') #recipient
subject ='Python SMTP test'
message['subject']=Header('utf-8')try:
smtpObj = smtplib.SMTP()
smtpObj.connect(host,465) #25 is the SMTP port number
smtpObj.login(user,password)
smtpObj.sendmail(sender, receiver, message.as_string())print("Mail sent successfully")
except smtplib.SMTPException:print("Error:Unable to send mail")
# Mail sent successfully
The above is the detailed content of Python's implementation of SMTP mail sending. For more information about Python SMTP, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts