Created
December 6, 2013 03:38
-
-
Save mattrasband/7818170 to your computer and use it in GitHub Desktop.
A simple SMTP mailer written in Python allowing file attachments (gmail is the default).
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 | |
import urllib2, sys, os, smtplib, argparse | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.Utils import formatdate | |
from email import encoders | |
parser = argparse.ArgumentParser(description='Send messages through SMTP server from the command line.') | |
parser.add_argument( | |
'-t', '--to', | |
type=str, | |
nargs='+', | |
required=True, | |
help='The recipient(s) of your message (list multiple separated by a space), required' | |
) | |
parser.add_argument( | |
'-s', '--subject', | |
type=str, | |
required=True, | |
help='The subject line of your message, required.' | |
) | |
parser.add_argument( | |
'-m', '--message', | |
type=str, | |
required=True, | |
help='The body of your message, required.' | |
) | |
parser.add_argument( | |
'-a', '--attach', | |
nargs='+', | |
help='Attach files, list files separated by a space for more than 1.' | |
) | |
parser.add_argument( | |
'-u', '--username', | |
type=str, | |
default='<define a default email if desired>', | |
help='The [gmail] username you want to send mail as.' | |
) | |
parser.add_argument( | |
'-p', '--password', | |
type=str, | |
default='<define password for email, careful!>', | |
help='The password for your [gmail] username, only use if -u is elected.' | |
) | |
parser.add_argument( | |
'--server', | |
type=str, | |
default='smtp.gmail.com', | |
help='You can define a different server if you don\'t want to use gmail.' | |
) | |
parser.add_argument( | |
'--port', | |
type=int, | |
default='587', | |
help='The port through which the server connects.' | |
) | |
args = parser.parse_args() | |
def buildMsg(): | |
global args | |
msg = MIMEMultipart() | |
if (args.to > 1): | |
msg['To'] = ', '.join(args.to) | |
else: | |
msg['To'] = args.to | |
msg['From'] = args.username | |
msg['Subject'] = args.subject | |
msg['Date'] = formatdate( localtime=True ) | |
body = MIMEText('text','plain') | |
body.set_payload( args.message ) | |
msg.attach( body ) | |
try: | |
for a in args.attach: | |
part = MIMEBase('application', 'octet-stream') | |
part.set_payload( open(a, 'rb').read() ) | |
encoders.encode_base64( part ) | |
part.add_header('Content-Disposition', 'attachment; filename="%s"' %(os.path.basename(a)) ) | |
msg.attach( part ) | |
except TypeError: | |
pass | |
return msg | |
def mail( message ): | |
msg = message | |
session = smtplib.SMTP( args.server, args.port) | |
session.ehlo() | |
session.starttls() | |
session.ehlo | |
session.login(args.username, args.password) | |
goodies = msg.as_string() | |
session.sendmail(args.username, args.to, msg.as_string()) | |
session.quit() | |
#os.system('notify-send "IP Update Email Sent"') # System Popup notification | |
def main(): | |
mail( buildMsg() ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment