diff --git a/mailing.py b/mailing.py index a8108f1..c16d57d 100755 --- a/mailing.py +++ b/mailing.py @@ -28,74 +28,66 @@ from email.header import Header from email.message import EmailMessage -parser = argparse.ArgumentParser( - description=""" -Simple mailing script. -""" -) - -ma = parser.add_argument_group(title="mandatory arguments", description=None) -ma.add_argument( - "-t", - "--tofile", - metavar="TO.FILE", - type=str, - required=True, - help="Sort of CSV file containing addresses of recipients.", -) -ma.add_argument( - "-b", - "--bodyfile", - metavar="BODY.FILE", - type=str, - required=True, - help="Template of the mail to be sent", -) - -parser.add_argument( - "-d", "--dry-run", action="store_true", help="Load data but don't send anything.", -) - - -parser.add_argument( - "-a", - "--attachement", - metavar="ATTACHED.FILE", - type=str, - nargs="?", - help="Optionnal attachment.", -) - - -TOFILE = None -BODYFILE = None -JOINFILE = None +def parse_args(): + parser = argparse.ArgumentParser(description="Simple mailing script.") + ma = parser.add_argument_group(title="mandatory arguments", description=None) + ma.add_argument( + "-t", + "--tofile", + metavar="TO.FILE", + type=str, + required=True, + help="Sort of CSV file containing addresses of recipients.", + ) + ma.add_argument( + "-b", + "--bodyfile", + metavar="BODY.FILE", + type=str, + required=True, + help="Template of the mail to be sent", + ) + parser.add_argument( + "-d", + "--dry-run", + action="store_true", + help="Load data but don't send anything.", + ) + parser.add_argument( + "-a", + "--attachement", + metavar="ATTACHED.FILE", + type=str, + nargs="?", + help="Optionnal attachment.", + ) + return parser.parse_args() # read the recipients file where values are separated by | characters -def read_recipients(): - recipientfile = open(TOFILE) +def read_recipients(args): + recipientfile = open(args.tofile) lines = recipientfile.readlines() return [line[:-1].split("|") for line in lines] -def read_body(): - bodyfile = open(BODYFILE) +def read_body(args): + bodyfile = open(args.bodyfile) body = "" for line in bodyfile.readlines(): body = body + line return body -def read_join(): +def read_join(args): try: - with open(JOINFILE, "rb") as fp: - ctype, encoding = mimetypes.guess_type(JOINFILE) + with open(args.attachement, "rb") as fp: + ctype, encoding = mimetypes.guess_type(args.attachement) if ctype is None or encoding is not None: ctype = "application/octet-stream" maintype, subtype = ctype.split("/", 1) metadata = { - "filename": JOINFILE, + "filename": args.attachement, "maintype": maintype, "subtype": subtype, } @@ -203,21 +195,18 @@ def send_message(message, to, attachment, args): if __name__ == "__main__": - args = parser.parse_args() - TOFILE = args.tofile - BODYFILE = args.bodyfile - JOINFILE = args.attachement + args = parse_args() # read the recipients file - sets = read_recipients() + sets = read_recipients(args) # read the template of the mail - bodytemplate = read_body() + bodytemplate = read_body(args) # optionnaly read the attachment of the mail attachment = None - if JOINFILE: - attachment = read_join() + if args.attachement: + attachment = read_join(args) # counter to be able to pause each 10 mails send counter = 0