#!/usr/bin/env python
#_ *_coding:utf-8 _*_
Import time
Import socket
Import Fcntl
Import struct
Import Smtplib
From Email.mime.text import Mimetext
def get_ip_add (ifname):
S=socket.socket (Socket.af_inet,socket. SOCK_DGRAM)
Return Socket.inet_ntoa (Fcntl.ioctl (
S.fileno (),
0x8915,
Struct.pack (' 256s ', ifname[:15])
) [20:24])
def sendmail (Subject,msg,fromemail,emailpasswd,toemail):
User=fromemail
pwd=emailpasswd
To=toemail
Nowtime=time.strftime ('%y-%m-%d%h:%m:%s ')
Msg=mimetext (msg)
msg["Subject"]=subject
Msg["from"]=user
msg["to"]=to
Try
#s =smtplib. Smtp_ssl (' smtp.qq.com ', 465)
S=smtplib. SMTP (' smtp.126.com ', +)#连接126服务端
S.login (USER,PWD)
S.sendmail (user,to,msg.as_string ())
S.quit ()
print "[%s]info:email send Success!" % Nowtime
Except Smtplib. Smtpexception,e:
print "[%s]error:email send faild,%s"% (nowtime,e)
If __name__== ' __main__ ':
Local_id=get_ip_add (' em1 ')
Print local_id
subject= ' Server [%s] Log alarm! ' %local_id# can be defined according to the actual
fromemail= ' [email protected] '
Emailpasswd= ' xxxxx ' # # Here the password is the client's authorization code, not the 126 login password
toemail= ' [email protected] ' # sent to who
name_1= "This is tesing"
SendMail (Subject,name_1, fromemail,emailpasswd,toemail) #Name_1 can write the corresponding content according to the actual demand
This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1877807
Python get local IP address send mail