The mail function can be realized simply with Python smtplib. What is SMTP? How to realize the mail function?
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.
Python creates SMTP object syntax as follows:
import smtplib
smtpObj = smtplib.SMTP([host [, port [, local_hostname]]])
Parameter Description:
host: SMTP server host. You can specify the IP address or domain name of the host, such as runoob.com, this is an optional parameter.
port: If you provide the host parameter, you need to specify the port number used by the SMTP service. Normally, the SMTP port number is 25.
local_hostname: If SMTP is on your local machine, you only need to specify the server address as localhost.
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:
from_addr: The address of the mail sender.
to_addrs: string list, mail sending address.
msg: send message
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.
Share with everyone for your reference. The specific implementation method is as follows:
import smtplib, socket
fromaddr = [email protected]
toaddrs =["[email protected]","[email protected]"]
msg =open("multimsg.eml","r").read()try:
server = smtplib.SMTP('10.0.0.1')
result = server.sendmail(fromaddr, toaddrs, msg)
server.quit()if result:for r in result.keys():
print "Error sending to", r
rt = result[r]
print "Code", rt[0],":", rt[1]except(smtplib.SMTPException, socket.error), arg:
print "SMTP Server could not send mail", arg
Example supplement:
python realizes sending mail function
''' Socket is for a specific network protocol (such as TCP/IP,ICMP/IP,UDP/IP, etc.), allowing programs to accept and connect. To build a simple server with TCP and stream sockets in python, you need to use the socket module. Using the function and class definitions contained in this module, you can generate programs that communicate over the network. , Divided into some methods of the six-step server:
1. What is a socket
Sockets in the kernel are abstracted layer by layer, extracting common things,This way, the externally provided interfaces can be as unified as possible,The three parameters in the Socket function are actually the conditions for the abstract socket to be embodied. The famil parameter determines the second layer communication domain shown in the figure, and the type determines the third layer communication mode, protocol
Determine the real communication protocol of the fourth layer.
First understand at which level this content is located? How to communicate
http://www.cnblogs.com/wangcq/p/3520400.html, looking at the method, the server and client are roughly divided into these steps
1. Create object
2. Bind the socket to the specified address, socket.bing(address)3.Step 3, after binding, the socket must be prepared and the port is monitored socket.listen(backlog)4.The accept method of socket waits for the client to request a connection
5. Processing stage
6. After the transmission is over, the server calls the socket's close method to close the connection
The client has 4 steps:
1. Create socket connection server
2. Use the socket connect method to connect to the server socket.connect((host,port))3.Client and server communicate through send and recv methods
4. Call the socket's close method to communicate
'''
# encoding=utf-8import socket
t = socket.socket(socket.AF_INET, socket.SOCK_STREAM)'''Create socket object, format socket.socket(family,type),family format is AF_INET (for TCP and UDP of IPV4 protocol), type parameter, SOCK_STREAM (stream socket, providing connection-oriented, reliable data services for TCP) or SOCK_DGRAM (datagram sockets provide connection-oriented, unreliable data services for UDP),SOCK_RAW (raw socket, lower-level protocol)'''
t.connect(('smtp.126.com',25)) #establish connection
print "start", t.recv(1024) #Use recv method to communicate and transfer data
t.send("helo 126.com\r\n") #Send Content
print "111", t.recv(1024)
t.send("auth login\r\n") #Log in to the mailbox
print "222", t.recv(1024) #Print in the console
t.send("dGVzdG1hbjE5ODA=\r\n") #Encoded in base64 bit mode, decoded as: testman1980
print "333", t.recv(1024) #Print in the console
t.send("Zm9zdGVyd3UxOTc4\r\n")#Encoded in base64 bit mode, decoded as: fosterwu1978
print "444", t.recv(1024) #Print in the console
t.send("mail from:<[email protected] \r\n") #It’s not clear to the sender here why it is also included in the email content below.
print "555", t.recv(1024) #Print in the console
t.send("rcpt to:<[email protected] \r\n") #I changed the recipient, but I don’t understand that there is also a recipient below, whether the message will be received below, and what the priority is. Let’s experiment tomorrow.
print "666", t.recv(1024) #Print in the console
t.send("data\r\n") #The role of data has not yet been discovered, practice tomorrow
print "777", t.recv(1024) #Print in the console
# Send mail content
t.send("from:[email protected]\r\n") #optional, can forge others' sending records
t.send("to:[email protected]\r\n") #optional,Can be sent to anyone
t.send("subject:hello!\r\n")
t.send("\r\n") #According to the requirements of the smtp protocol, a blank line should be sent after the title
t.send("mail test1111!\r\n") #Send mail content
t.send(".\r\n")
print "888", t.recv(1024)
This is the end of this article on how Python implements the mail function. For more information about how to implement the mail function in Python, please search ZaLou.Cn
Recommended Posts