This article mainly introduces how to implement mail Notification for user login in python, and analyzes how to use Python to schedule tasks and send emails, for more information, see the example in this article. Share it with you for your reference. The details are as follows:
This is scheduled to run in a scheduled task in linux. When a new user logs in, the user name is sent to the specified email address to notify the administrator.
#! /Usr/bin/env python # encoding = utf-8from smtplib import SMTPimport subprocesssmtp = "smtp.qq.com" user = '000000' password = 'xxxx' run _ comd = subprocess. popen ('W packet grep pts ', shell = True, stdout = subprocess. PIPE) data = run_comd.stdout.read () mailb = ["the server has a new user login", data] mailh = ["From: 1234567@qq.com", "To: xxxx@gmail.com", "Subject: user Logon monitoring "] mailmsg =" \ 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 = '': passelse: send_mail ()
I hope this article will help you with Python programming.