Python implements SMTP mail sending

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

Python implements SMTP mail sending
How Python implements the mail function
Python implements QQ mailbox to send mail
Python implements SSL sending based on QQ mailbox
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements parking management system
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
Python simply implements the snake game
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down