Ejemplo n.º 1
0
def run(args) -> None:
    smtp = SMTP(args.verbose)
    args.attachments = []
    attch_parts = []

    open_attachments(args, smtp)
    open_named_attachments(args)

    if args.zip:
        zip_attachments(args)

    if args.max_file_size:
        split_attachments(args, attch_parts)

    i = 0
    without_attch = not args.attachments and not attch_parts
    while args.attachments or attch_parts or without_attch:
        try:
            smtp.connect(args.host, args.port)
            smtp.hello()
            if args.no_ssl is not True:
                smtp.encrypt()
            smtp.authorize(args.login, args.password)
            smtp.mail_from(args.sender)
            for recipient in args.recipients:
                smtp.mail_to(recipient)

            email = Email(
                args.sender,
                args.recipient,
                args.name,
                cc=set(args.cc),
                attachments=set(args.attachments) if i == 0 else None,
                attch_part=attch_parts[0] if not without_attch else None,
                subject=args.subject if i == 0 else '{} - {}'.format(
                    args.subject, i + 1),
                text=args.text if i == 0 else 'Letter continuation.',
                encoding=smtp.encoding)

            smtp.send_letter(email.to_string())
            smtp.disconnect()

            for file, _ in args.attachments:
                file.close()
            if args.zip:
                os.remove('attachments.zip')

            if without_attch:
                break

            i += 1
            args.attachments.clear()
            attch_parts.pop(0)

        except (SMTPException, OSError) as e:
            smtp.client.warning('An error occurred '
                                'during the runtime: {}'.format(e.message))
            smtp.close()
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(
        usage='{} [OPTIONS]'.format(os.path.basename(sys.argv[0])),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description='SMTP client, with cool CUI. To')
    parser.add_argument('address', help='address to connect',
                        nargs='?', default=SMTP_SERVER)
    parser.add_argument('port', help='port', nargs='?',
                        type=int, default=SMTP_PORT)
    group = parser.add_mutually_exclusive_group()

    group.add_argument('-c', '--console', action="store_true", help="Enable console mode")
    group.add_argument('-s', '--settings', metavar="FILE", type=str, default="input.json",
                       help="JSON file with settings. MUST have following keys:\n"
                            f"{', '.join([FROM, RECEIVERS, SUBJECT, TEXT, ATTACHMENTS])}")

    args = parser.parse_args()
    smtp_con = SMTP(args.address, args.port)
    print(smtp_con.connect())
    if args.console:
        smtp_con.run_batch()
    else:
        send_mail(smtp_con, args.settings)
Ejemplo n.º 3
0
def main():
    smtp_con = SMTP(SMTP_SERVER, SMTP_PORT)
    print(smtp_con.connect())
    send_mail(smtp_con, INPUT)