Python uses the smtplib method of the email module, pythonsmtplib
Smptp class definition: smtplib. SMTP (host [, port [, local_hostname [, timeout]) is used as the SMTP constructor to establish a connection with the smtp server. After the connection is successful, you can send related requests to the server, such as login, verification, sending, and logout. The host parameter is the remote smtp host address, for example, stmp.163.com; port is the connection port, and the default value is 25; local_hostname is used to send the HELO/EHLO command on the local FQDN (complete domain name, timeout is a connection or an attempt to time out in most seconds. The SMTP class has the following methods:
SMTP. connect ([host [, port]) method, connect to the remote smtp host method, host is the remote host address, port is the remote host smtp port, default 25, you can also directly use host: port format, for example, SMTP. connect ("smtp.163.com", "25 ').
SMTP. login (user, password) method, remote smtp host validation method, parameters for the user name and password, such as SMTP. login ("18801457794@139.com", '20140901 ').
SMTP. sendmail (from_addr, to_addrs, msg [, mail_options, rcpt_options]) method to enable mail sending. The parameters are sender, recipient, and mail content in sequence, for example, SMTP. sendmail ("python@163.com", '2017 @ qq.com ', body), the body content is defined as follows:
"From: python@163.com
To: 404408853@qq.com
Subject: test mail
Test mail body """
SMTP. starttls ([keyfile [, certfile]) method to enable TLS (Secure Transmission) mode. All SMTP commands are encrypted transmission, for example, when using the stmp server of gmail, you must start this item to send emails normally.
SMTP. quit () method, port smtp server connection
The following describes how python sends emails through the instance column.
[Root @ localhost smtplib] # cat simple1.py #! /Usr/bin/env python #-*-coding: UTF-8-*-import smtplibimport stringHOST = "smtp.139.com" # define smtp host SUBJECT = "test" # define mail subject to = "404408853@qq.com" # define mail recipient FROM = "18801457794@139.com" # define email sender text = "python test mail" # BODY of the email content = string. join (# assemble the mail body content of the sendmail method, separated by "\ r \ n" "From: % s" % FROM, "To: % s" %, "Subject: % s" % SUBJECT, "", text), "\ r \ n") server = smtplib. SMTP () # create an SMTP Object server. connect (HOST, "25") # connect to the smtp HOST server through the connect method. starttls () # Start the Secure Transmission Mode server. login ("18801457794@139.com", "123456") # email account login verification server. sendmail (FROM, TO, BODY) # send the email TO the server. quit () # Disconnect smtp
Execute this code and we will receive an email
Implement data report emails in HTML Format
The content of plain text emails cannot meet our diversified needs. In this example, we introduce email. the MIMETex class of mime is used to support HTML-format emails. It supports all HTML elements, including tables, images, animations, CSS styles, and forms. In this example, a perfect business traffic report is customized using an HTML table. The implementation code is as follows:
#! /Usr/bin/env python # coding: utf-8import smtplibfrom email. mime. text import MIMEText # import MIMEText Class HOST = "smtp.139.com" SUBJECT = u "official website traffic data report" TO = "404408853@qq.com" FROM = "18801457794@139.com" msg = MIMEText ("" <table width = "800" border = "0" cellspacing = "0" cellpadding = "4"> <tr> <td bgcolor = "# CECFAD" height = "20" style =" font-size: 14px "> * website data <a href =" monitor.domain.com "> more </a> </td> </tr> <td bgcolor =" # EFEBDE "height =" 100 "style =" font-size: 13px "> 1) daily traffic: <font color = read> 152433 </font> visits: 23651 page views: 45123 clicks: 545122 data traffic: 504 Mb <br> 2) status code message <br> 500: 105 404; 3264 503; 214 <br> 3) Visitor browser Information <br> IE: 50% firefox: 10% chrome: 30% other: 10% <br> 4) Page Information <br>/index. php 42153 <br>/view. php 21451 <br> </td> </tr> </table> "," html "," UTF-8 ") msg ['subobject'] = SUBJECTmsg ['from'] = FROMmsg ['to'] = TOtry: server = smtplib. SMTP () server. connect (HOST, '25') server. starttls () server. login ('1970 @ 139.com ', '123') server. sendmail (FROM, TO, msg. as_string () server. quit () print "email sent successfully" failed t Exception, e: print "failed:" + str (e)
Run the code result,
Example 2: Implement server performance report email in graphic format
The MIMEImage class must be referenced when the email content that contains image data is required. If the email subject is composed of multiple MIME objects, the MIMEMultipart class must be referenced for encapsulation. In this example, the combination of MIMEText and MIMEImage class is used to customize the server performance report email in graphic format. The implementation code is as follows:
#! /Usr/bin/env python # coding: utf-8import smtplib, stringfrom email. mime. multipart import MIMEMultipartfrom email. mime. text import MIMETextfrom email. mime. image import MIMEImageHOST = "smtp.139.com" # define smtp host SUBJECT = "kingmeide platform system status report" # define mail subject to = "404408853@qq.com, 302803690@qq.com "# define mail recipient FROM =" 18801457794@139.com "# define mail sender TO_list =. split (TO) def addimg (src, imgid): # Add an image function. Parameter 1: Image path; parameter 2: Image ID fp = open (Src, 'rb') # open the file msgImage = MIMEImage (fp. read () # create a MIMEImage object, read the image content, and use it as the parameter fp. close () # close the file msgImage. add_header ('content-id', imgid) # specify the Content-ID of the image file. The label src uses return msgImage # return msgImage object msg = MIMEMultipart ('related ') # create a MIMEMultipart object, use related to define the email body msgtext = MIMEText ("<table width =" 600 "border =" 0 "cellspacing =" 0 "cellspacing =" 4 "> <tr bgcolor = "# CECFAD" height = "20" style = "font-size: 14px "> <td colspan = 2> The following figure shows the system status of 211.157.111.41 </td> </tr> <tr bgcolor =" # EFEBDE "height =" 100 "style =" font- size: 13px "> <td> </td> </td> </tr> <tr bgcolor =" # EFEBDE "height =" 100 "style =" font-size: 13px "> <td> </td> </td> </tr> </table>", "html", "UTF-8 ") # the src attribute of a tag is the msg referenced by Content-ID. attach (msgtext) # MIMEMultipart object Append the MIMEText content msg. attach (addimg ("img/bytes_io.png", "io") # Use the MIMEMultipart object to append the MIMEImage content msg. attach (addimg ("img/OS _load.png", "load") msg. attach (addimg ("img/OS _mem.png", "mem") msg. attach (addimg ("img/OS _disk.png", "disk") msg ['subobject'] = SUBJECTmsg ['from'] = FROMmsg ['to'] = TOtry: server = smtplib. SMTP () server. connect (HOST, "25") server. starttls () server. login ('1970 @ 139.com ', '123') server. s Endmail (FROM, TO_list, msg. as_string () server. quit () print "email sent successfully! "Failed t Exception, e: print" failed: "+ str (e)
Code Running Effect