-
-
Save suhailshergill/b749ba47477225529657 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
""" Email Me. | |
Usage: | |
emailme <message> | |
emailme [-s] <message> | |
emailme [-s] <subject> <message> | |
emailme <toaddr> <subject> <message> | |
emailme <toaddr> <fromaddr> <subject> <message> | |
emailme -h | --help | |
emailme --version | |
Options: | |
-h --help Show help | |
-s --sms Send SMS | |
--version Show version | |
""" | |
#Python script to send simple emails | |
import smtplib | |
from email.mime.text import MIMEText | |
import netrc | |
from docopt import docopt | |
DEFAULTFROM = "[email protected]" | |
DEFAULTTO = "[email protected]" | |
SMSADDRESS = "[email protected]" | |
DEFAULTSUBJECT = "PYTHON NOTIFICATION" | |
""" | |
Note that by default, this looks to check your netrc credentials | |
to use this feature, create a .netrc file, so that only you can read and write it | |
touch ~/.netrc | |
chmod 600 ~/.netrc | |
and then add the information for the gmail smtp server, i.e. | |
machine smtp.gmail.com | |
login [email protected] | |
password yourpassword | |
This way only you will have access to this file | |
For SMS support, use your service providers gateway. See: http://en.wikipedia.org/wiki/List_of_SMS_gateways | |
""" | |
from functools import partial | |
def send_email(message="",subject=DEFAULTSUBJECT, | |
me=DEFAULTFROM,recipients=[DEFAULTTO], | |
smtpserver="smtp.gmail.com",tls=True,login=None, | |
password=None): | |
""" Send an email using the gmail smtp servers, and netrc to hide the username | |
and password information """ | |
if login is None or password is None: | |
secrets = netrc.netrc() | |
netrclogin,netrcaccount,netrcpassword = secrets.authenticators(smtpserver) | |
if login is None: | |
login = netrclogin | |
if password is None: | |
password = netrcpassword | |
msg = MIMEText(message) | |
msg['Subject'] = subject | |
msg['From'] = me | |
msg['To'] = ", ".join(recipients) | |
s = smtplib.SMTP(smtpserver) | |
if tls: | |
s.starttls() | |
s.login(login,password) | |
s.sendmail(me,recipients, msg.as_string()) | |
s.quit() | |
send_sms = partial(send_email, recipients =[SMSADDRESS] ,subject="") | |
if __name__=="__main__": | |
args = docopt(__doc__, version="Email Me version 0.1") | |
toaddr = args['<toaddr>'] or DEFAULTTO | |
fromaddr = args['<fromaddr>'] or DEFAULTFROM | |
message = args['<message>'] | |
if args['--sms']: | |
subject = args['<subject>'] or '' | |
send_sms(subject=subject, | |
message=message) | |
else: | |
subject = args['<subject>'] or DEFAULTSUBJECT | |
send_email(recipients=[toaddr], | |
me=fromaddr, | |
subject=subject, | |
message=message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment