#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import optparse, commands

def myhelp():
    msg = """[-f from] [-h mailhost] [-s subject] [-c copy] [recipient ...]
       -8          set charset utf-8
       -c          set the Cc: field
       -d <nn>     set debug level to <nn>
       -f          set the From: field
       -h          use mailhost:port as the SMTP server
       -s          set the Subject: field
       -r          set the Reply-To: field
       -l          set the maximum number of lines that should be sent (default: unlimited)
"""
    return msg

def parse_cmdline():
    parser = optparse.OptionParser(conflict_handler="resolve")
    parser.add_option('-8', dest='8', action="store_true", default=False)
    parser.add_option('-c', dest='c')
    parser.add_option('-d', dest='d')
    parser.add_option('-f', dest='f')
    parser.add_option('-h', dest='h')
    parser.add_option('-s', dest='s')
    parser.add_option('-r', dest='r')
    parser.add_option('-l', dest='l')

    (options, args) = parser.parse_args()

    return options, args

def main():
    options, args = parse_cmdline()
    bsmtp_args = ''
    for i in [ '8', 'c', 'd', 'f', 'h', 's', 'r', 'l']:
        j = getattr(options, i)
        if j == True:
            bsmtp_args += ' -%s'%i
        elif j not in [False, None]:
            if j.count(' ') > 0:
                bsmtp_args += ' -%s "%s"'%(i, j)
            else:
                bsmtp_args += ' -%s %s'%(i, j)
    for i in args:
        if i.count(' ') > 0:
            bsmtp_args += ' "%s"'%i
        else:
            bsmtp_args += ' %s'%i
    a=commands.getoutput('bsmtp %s'%(bsmtp_args))
#    file('/tmp/bsmtp_debug', 'a').write('RES : %s**\n'%a)

if __name__ == '__main__':
    main()

