In the field of system management, we often use e-mail to send management information, business quality statements, etc., to facilitate the first time operators to understand the service status of the business. Through the Python smtplib module to implement the message sending function, simulates an SMTP client, through interacts with the SMTP server to implement the mail sends the function.
Common classes and methods of Smtplib modules
Smtp.connect ([Host[,port]) method, connect the remote SMTP host method, host is the remote host address, Port is the remote host SMTP port, the default is 25, can also be directly used in the form of Host:port to express, Example: Smtp.connect ("smtp.qq.com", "25")
Smtp.login (user, password) method, the remote host's check method.
Smtp.sendmail (from_addr, To_addrs, MSG, mail_options=[],rcpt_options=[]) method, to implement the message sending function, the parameters of the sender, the recipient, the message content, For example: Smtp.sendmail ("[Email protected]", "[email protected]", body), where the body content is as follows:
The Smtp.starttls () method enables TLS (secure transport) mode, and all SMTP instructions are encrypted for transmission.
Smtp.quit () method to disconnect from the SMTP server.
Note: The QQ mailbox needs to generate authorization code.
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9A/FA/wKiom1lcuVewxW5DAAIjEDZrGLQ934.png-wh_500x0-wm_ 3-wmp_4-s_3317030553.png "title=" 2f3b1af728c52d46869299e22de23c16.png "alt=" Wkiom1lcuvewxw5daaijedzrglq934.png-wh_50 "/>
Example:
#!/usr/bin/env pythonimport smtplibhost = ' smtp.qq.com ' SUBJECT = ' Test email from python ' to = ' [email protected] ' from = ' [ Email protected] "test =" Just a test txt! " BODY = "\ r \ n". Join (("From:%s"%from, "to:%s"%to, "Subject:%s"%subject, "", Test)) Server = Smtplib. SMTP () Server.connect (HOST, "Server.starttls") server.login ("[Email protected]", "password") Server.sendmail ( From,[to],body) Server.quit ()
Python Learning note e-mail module smtplib