Skip to content

Instantly share code, notes, and snippets.

@ZiTAL ZiTAL/mail.py
Created Mar 28, 2014

Embed
What would you like to do?
python send mail
#!/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 erabiltzailea@gmail.com \
# --mail-to hartzailea@gmail.com \
# --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
You can’t perform that action at this time.