Created
March 28, 2014 10:29
-
-
Save ZiTAL/9829715 to your computer and use it in GitHub Desktop.
python send mail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys, re, smtplib | |
class Mail(object): | |
params = {} | |
server = {} | |
def send(self): | |
msg = "Subject: %s\n\n%s\n" % ( self.params['--mail-title'], self.params['--mail-body']) | |
self.server.sendmail(self.params['--mail-from'], self.params['--mail-to'], msg) | |
return True | |
def getSmtp(self): | |
server = smtplib.SMTP(self.params['--smtp-host']) | |
server.starttls() | |
server.login(self.params['--smtp-user'], self.params['--smtp-passwd']) | |
self.server = server | |
def getArg(self): | |
lastKey = '' | |
j = 0 | |
tmp = {} | |
for i in sys.argv: | |
if(re.match("^\-\-", i)): | |
lastKey = i | |
tmp[lastKey] = '' | |
j = 0 | |
else: | |
if(lastKey!=''): | |
if(j>0): | |
tmp[lastKey] = tmp[lastKey]+" " | |
tmp[lastKey] = tmp[lastKey]+i | |
j = j+1 | |
self.params = tmp | |
def __init__(self): | |
self.getArg() | |
self.getSmtp() | |
self.send() | |
def __exit__(self): | |
self.server.quit() | |
Mail() | |
sys.exit() | |
# usage | |
# | |
# /usr/local/bin/mail.py --smtp-host smtp.gmail.com:587 \ | |
# --smtp-user erabiltzailea \ | |
# --smtp-passwd pasahitza \ | |
# --mail-from [email protected] \ | |
# --mail-to [email protected] \ | |
# --mail-title ssh login detected \ | |
# --mail-body `who` & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment