#!/usr/bin/env python from email.utils import parseaddr from smtplib import SMTP from argparse import ArgumentParser, ArgumentTypeError if __name__ == '__main__': parser = ArgumentParser(description='send an email') parser.add_argument('--sender', required=True, help='mail sender') parser.add_argument('--to', required=True, help='mail recipient') parser.add_argument('--cc', nargs='*', help='cc recipients') parser.add_argument('--bcc', nargs='*', help='bcc recipients') parser.add_argument('--subject', help='subject') group = parser.add_mutually_exclusive_group(required=True) group.add_argument('body', nargs='?', help='the mail content') group.add_argument('--file', help='file with mail content') args = parser.parse_args() args.recipients = [args.to] if args.cc: args.recipients += args.cc if args.bcc: args.recipients += args.bcc if args.file: with open(args.file, 'r') as ifh: args.body = ifh.read() del args.file print(args)