Home > Backend Development > Python Tutorial > python实现用户登陆邮件通知的方法

python实现用户登陆邮件通知的方法

WBOY
Release: 2016-06-10 15:09:41
Original
1313 people have browsed it

本文实例讲述了python实现用户登陆邮件通知的方法。分享给大家供大家参考。具体如下:

这里写在linux计划任务里定时执行,当有新用户登陆时候发送用户名到指定邮箱通知管理员。

#!/usr/bin/env python
#encoding=utf-8
from smtplib import SMTP
import subprocess
smtp = "smtp.qq.com"
user = '1234567'
password = 'xxxx'
run_comd = subprocess.Popen('w¦grep pts',shell=True,stdout=subprocess.PIPE)
data = run_comd.stdout.read()
mailb = ["服务器有新用户登录",data]
mailh = ["From: 1234567@qq.com", "To: xxxx@gmail.com", "Subject: 用户登录监控"]
mailmsg = "\r\n\r\n".join(["\r\n".join(mailh), "\r\n".join(mailb)])
def send_mail():
  send = SMTP(smtp)
  send.login(user,password)
  result = send.sendmail("1234567@qq.com", ("xxxx@gmail.com",), mailmsg)
  send.quit()
if data == '':
  pass
else:
  send_mail()

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template