Python get local IP address send mail

Source: Internet
Author: User

#!/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

Related Article

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.