SMPTP class definition: Smtplib. SMTP (Host[,port[,local_hostname[,,timeout]]), as the constructor of SMTP, the function is to establish a connection with the SMTP server, after the successful connection, you can send related requests to the server, such as login, check, send, exit and so on. The host parameter is a remote SMTP host address, such as stmp.163.com;port for a connection port, and the default is 25;local_hostname to send Helo/ehlo instructions on the local FQDN (full domain name). Timeout is a connection or attempt to time out in most seconds, the SMTP class has the following methods:
Smtp.connect ([Host[,port]) method, connect the remote SMTP host method, host is the remote host address, Port is the remote host SMTP port, default 25, can also be directly used in the form of Host:port, for example: Smtp.connect ("smtp.163.com", "25").
Smtp.login (User,password) method, the remote SMTP host check method, parameters for the user name and password, such as Smtp.login ("[Email protected]", ' 123456 ').
Smtp.sendmail (From_addr,to_addrs,msg[,mail_options,rcpt_options]) method, to implement the message sending function, the parameters are the sender, the recipient, the message content, for example: Smtp.sendmail ("[Email protected]", ' [email protected] ', body), where the body content is defined as follows:
"" "From:[email protected]
To:[email protected]
Subject:test Mail
Test mail Body "" "
SMTP.STARTTLS ([Keyfile[,certfile]) method, enable TLS (secure transport) mode, all SMTP instructions are encrypted, such as the use of Gmail's STMP server need to start this to send mail normally
Smtp.quit () method, connection to the Port SMTP server
Here's an example of how Python sends messages Lezilai
[[email protected] smtplib]# cat simple1.py #!/usr/bin/env python# -*- coding: utf-8 -*-import smtplibimport stringHOST = "Smtp.139.com" # Define an SMTP host subject = "test" #定义邮件主题TO = "[email protected]" #定义邮件收件人FROM = "[email protected]" #定义邮件发件人text = "Python test mail" #邮件的内容BODY = String.Join ( #组装sendmail方法的邮件主体内容, sections are separated by "\ r \ n" "from:%s" %FROM, "to:%s" %TO, "subject:%s"%subject, "", text), "\ r \ n") server = smtplib. SMTP () #创建一个SMTP对象server. Connect (HOST, "STARTTLS") #通过connect方法连接smtp主机server. # Start Secure transfer Mode Server.login ("[email protected]", "123456") #邮件账户登录校验server. SendMail (from,to,body) # Email server.quit () #断开smtp连接
Execute this code, we will receive an email
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/DA/wKioL1RypFqhQSTXAACHRh_IHcw783.jpg "title=" 1.png " alt= "Wkiol1rypfqhqstxaachrh_ihcw783.jpg"/>
This article is from the "Server" blog, so be sure to keep this source http://zhangfang2012.blog.51cto.com/6380212/1581823
Python uses the e-mail module smtplib