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()
def run_smtp(mode_data, smtp_server, dis_enc, fromaddr, password, toaddrs, data): print('[*] Connecting to server...') smtp = SMTP(SMTP_SERVERS[smtp_server], dis_enc) smtp.ehlo() smtp.auth(fromaddr, password) recverr = send_by_mode(mode_data, smtp, fromaddr, toaddrs, data) for name, error in recverr.items(): print('[-] Error with {}: {} {}'.format(name, error[0], error[1][:-1])) if (len(recverr) < len(toaddrs)): print('[+] Message were sended') else: print('[-] There are no valid emails') smtp.close() print('[+] Done')