Python SMTP sends HTML-formatted messages through the Mimetext class

Source: Internet
Author: User
Tags get ip

Due to the fact that the content of plain text is not enough to meet the diverse needs, the main introduction is to introduce Mail.mime's Mimetext class to support HTML-formatted messages, supporting all HTML-formatted elements, including tables, images, animations, CSS styles, forms, and more. (Refer to Teacher Liu's literature)

The case collects the simplest server hardware information, send the information to the recipient's mailbox through SMTP, you can collect the required information according to your own needs (such as CPU percentage, percentage of HDD remaining, percent of memory used, and set threshold, when the hard disk has less than 10% space left), Send an email to notify the administrator to process it promptly)



#!/usr/bin/env python

#coding: Utf-8

Import Smtplib

Import OS

Import Psutil

From Email.mime.text import mimetext//import Mimetext class

ip = Os.popen ("ifconfig |grep-v 127 |grep inet |awk ' {print $} ' |cut-d:-f2"). Read (). Strip ()//Get IP Address

hostname = Os.popen ("hostname"). Read (). Strip ()//Get host Name

CPU = Psutil.cpu_count ()//Get CPU Thread

MEM = Os.popen ("free-m |grep Mem |awk ' {print $} '). Read (). Strip () +" M "//Get Total memory

Disk = Os.popen ("fdisk-l |grep-e disk |awk ' {print $} '"). Read (). Strip () + "G"//Get the total size of the hard drive

HOST = "smtp.163.com"//designated to use NetEase 163 mailbox

SUBJECT = u "Server hardware Information"//message header

to = "[email protected]"//Recipient

from = "[email protected]"//Sender

msg = Mimetext ("" "

<table color= "CCCC33" width= "$" border= "1" cellspacing= "0" cellpadding= "5" text-align= "Center" >

<tr>

<TD text-align= "center" >name</td>

<TD text-align= "center" >network</td>

<td>CPU</td>

<td>Mem</td>

<td>Disk</td>

</tr>

<tr>

<TD text-align= "center" >%s </td>

<td>%s </td>

<td>%s </td>

<td>%s </td>

<td>%s </td>

</tr>

</table> "" "% (Hostname,ip,cpu,mem,disk)," HTML "," uft-8 ")

msg[' Subject '] = Subject

msg[' from ') = from

msg[' to ') = To

Try

Server = Smtplib. SMTP ()//Create an SMTP object

Server.connect (Host, "25")//link to SMTP host via Connect method

SERVER.STARTTLS ()//Start Secure transfer Mode

Server.login ("[Email protected]", "passwordxx")//Login 163 Email Verification user, password

Server.sendmail (from, [to], msg.as_string ())//Send mail

Server.quit ()

print "Mail send succeeded%s%s%s%s%s"% (Hostname,ip,cpu,mem,disk)/sent successfully and printed

Except Exception, E:

Print "message sent failed:" +str (E)



Operation Result:


650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8D/0B/wKiom1iDMt-zlZsCAAA2aqwUaFk487.png-wh_500x0-wm_3 -wmp_4-s_889160194.png "title=" Bv1v52mf~wy%}f]qgnofi]2.png "alt=" Wkiom1idmt-zlzscaaa2aqwuafk487.png-wh_50 "/>


You can collect the information you need by judging the status information of the server, and the related information messages.


This article is from the "My_soul" blog, make sure to keep this source http://soul455879510.blog.51cto.com/6180012/1893579

Python SMTP sends HTML-formatted messages through the Mimetext class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.