Skip to content

Instantly share code, notes, and snippets.

@ctaloi
Created May 21, 2013 15:20
Show Gist options
  • Save ctaloi/5620644 to your computer and use it in GitHub Desktop.
Save ctaloi/5620644 to your computer and use it in GitHub Desktop.
optparse example for Python 2.6
#!/usr/bin/env python
import os
from optparse import OptionParser
def buildEmail():
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option('-s', '--subject', action='store', dest='em_subject')
parser.add_option('-b', '--body', action='store', dest='em_body')
parser.add_option('-t', '--to', action='store', dest='em_to')
parser.add_option('-f', '--from', action='store', dest='em_from')
(options, args) = parser.parse_args()
if any([options.em_from is None, options.em_to is None, options.em_subject is None, options.em_body is None]):
print "Missing an Option"
parser.print_help()
exit(-1)
else:
sendEmail(options.em_from, options.em_to, options.em_subject, options.em_body)
def sendEmail(em_from, em_to, em_subject, em_body):
sendmail_location = '/usr/sbin/sendmail'
p = os.popen('%s -t' % sendmail_location, 'w')
p.write('From: %s\n' % em_from)
p.write('To: %s\n' % em_to)
p.write('Subject: %s \n' % em_subject)
p.write('\n')
p.write(em_body)
p.close()
def main():
buildEmail()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment