Skip to content

Instantly share code, notes, and snippets.

@gintsmurans
Last active March 7, 2016 14:18
Show Gist options
  • Save gintsmurans/5df01708fd1866c418ae to your computer and use it in GitHub Desktop.
Save gintsmurans/5df01708fd1866c418ae to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Little library for email and sms (using SMSEagle - http://www.smseagle.eu/) notifications.
# Can be used as a command line or library.
# For this to work please set SMTP and SMSEAGLE parameters
import argparse
import smtplib
import json
from email.mime.text import MIMEText
from urllib.request import urlopen
# SMTP Parameters
SMTP_HOST = ''
SMTP_PORT = 587
SMTP_LOGIN_EMAIL = ''
SMTP_LOGIN_PASSWORD = ''
DEFAULT_FROM = ''
# SMSEagle Parameters
SMSEAGLE_HOST = 'http://192.168.0.130'
SMSEAGLE_USERNAME = 'admin'
SMSEAGLE_PASSWORD = 'password'
def email(to_email, subject, body, from_email = DEFAULT_FROM, email_type = 'plain', headers = ''):
""" Send email via SMTP """
if isinstance(to_email, (list, tuple)):
to_email = ",".join(to_email)
# Construct the message
msg = MIMEText(body, 'text' if email_type == 'text' else 'html')
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject
# Connect to mail server
mailserver = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
mailserver.login(SMTP_LOGIN_EMAIL, SMTP_LOGIN_PASSWORD)
# Send the email
mailserver.send_message(msg)
# Close connection
mailserver.quit()
def sms(to_numbers, body):
""" Send SMS via SMSEagle """
if isinstance(to_numbers, (list, tuple)):
to_numbers = ",".join(to_numbers)
# Contruct api url
api_url = '%s/index.php/jsonrpc/sms' % SMSEAGLE_HOST
msg = {
"method": "sms.send_sms",
"params": {
"login": SMSEAGLE_USERNAME,
"pass": SMSEAGLE_PASSWORD,
"unicode": 1,
"to": to_numbers,
"message": body
}
}
msg = json.dumps(msg)
msg = msg.encode('utf-8')
# Send message
content = urlopen(api_url, data=msg).read()
content = content.decode('utf-8')
return ('OK' in content)
if __name__ == "__main__":
""" Handle script called from command line """
# Parse commandline arguments
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='Send email')
email_parser = subparsers.add_parser("email")
email_parser.add_argument('to', type=str, help='To email address')
email_parser.add_argument('subject', type=str, help='Email subject')
email_parser.add_argument('msg', type=str, help='Email body')
email_parser.add_argument('--from', type=str, default=DEFAULT_FROM, dest='from_email', help='From email address')
email_parser.add_argument('--type', type=str, default='plain', dest='email_type', choices=['plain', 'html'], help='Email type')
email_parser.set_defaults(which='email')
sms_parser = subparsers.add_parser("sms")
sms_parser.add_argument('to', type=str, help='Number (or multiple numbers, separated by comma)')
sms_parser.add_argument('msg', type=str, help='Text')
sms_parser.set_defaults(which='sms')
args = parser.parse_args()
if hasattr(args, 'which'):
if args.which == 'email':
email(args.to, args.subject, args.msg, args.from_email, args.email_type)
print("Done\n")
elif args.which == 'sms':
status = sms(args.to, args.msg)
if status:
print("OK\n")
else:
print("ERR: %s\n" % content)
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment