Created
February 4, 2012 01:03
-
-
Save ecounysis/1734171 to your computer and use it in GitHub Desktop.
command line send mail
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/python | |
from mailer import Mailer, Message | |
from optparse import OptionParser | |
def isnull(opt, default): | |
if (opt == "" or opt is None): | |
return default | |
else: | |
return opt | |
parser = OptionParser() | |
parser.add_option("-t", "--to", dest="send_to", action="store", help="who to send message to") | |
parser.add_option("-b", "--body", dest="message_body", action="store", help="content of message") | |
parser.add_option("-a", "--attachment", dest="attachments", action="append", help="file(s) to attach") | |
parser.add_option("-f", "--from", dest="from_addr", action="store", help="from address") | |
parser.add_option("-s", "--server", dest="server", action="store", help="smtp server") | |
parser.add_option("-u", "--subject", dest="subject", action="store", help="message subject") | |
(options, args) = parser.parse_args() | |
m = Mailer() | |
m.host = options.server | |
mess = Message() | |
mess.To = options.send_to | |
mess.From = options.from_addr | |
mess.Body = options.message_body | |
mess.Subject = options.subject | |
if (options.attachments is not None and len(options.attachments) > 0): | |
for a in options.attachments: | |
mess.attach(a) | |
m.send(mess) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment