def test_mail_to_error(self, patched_recv, patched_send): s = smtp.SMTP() s.sock = Mock(smtp.socket.socket) patched_recv.return_value = b'334-bad sender\r\n' with self.assertRaises(smtp.SMTPException): smtp.SMTP().mail_to('*****@*****.**') patched_send.assert_called_with(b'rcpt to: ' b'<*****@*****.**>\r\n')
def test_start_tls_error(self, _, patched_recv): with self.assertRaises(smtp.SMTPException): patched_recv.side_effect = [b'334-tls is not allowed!\r\n'] smtp.SMTP().start_tls() with self.assertRaises(smtp.SMTPException): patched_recv.side_effect = [ b'250-go ahead\r\n', b'250 go ahead\r\n', b'334-error!\r\n' ] smtp.SMTP().start_tls()
def test_wrap_socket(self, patched_wrap): s = smtp.SMTP() s.sock = Mock(smtp.socket.socket) patched_wrap.return_value = Mock(smtp.socket.socket) s.wrap_socket() self.assertIsInstance(s.enc_sock, smtp.socket.socket) self.assertEqual(s.encrypted, True)
def test_sending_data(self, patched_sendall): s = smtp.SMTP() patched_sendall.return_value = True s.send('ehlo') patched_sendall.assert_called_with(b'ehlo\r\n') s.encrypted = True s.enc_sock = Mock(smtp.socket.socket) s.send('ehlo') patched_sendall.assert_called_with(b'ehlo\r\n')
def test_auth(self, patched_recv, patched_sendall): patched_recv.side_effect = [ b'334-ok\r\n', b'334-ok\r\n', b'235-ok\r\n' ] smtp.SMTP().authorize('login', 'password') calls = [ call(b'auth login\r\n'), call(b'bG9naW4=\r\n'), call(b'cGFzc3dvcmQ=\r\n') ] patched_sendall.assert_has_calls(calls)
def newSlave(server, clientAddress, info): """ Threaded process that sends object, and recieves info back from the node """ # todo remake in TCP so slaves can be held try: virus = smtp.SMTP(info[0], info[1], info[2], info[3], info[4], info[5]) payload = pickle.dumps(virus) server.sendto(payload, clientAddress) message, clientAddress = server.recvfrom(2048) print(f"\tMessage Recieved from node : "+message.decode()) except: pass
def processEmail(emailID, subject, body, fromAddress, toAddresses, ccAddresses): logging.info('Processing email %s from %s with subject %s...', emailID, fromAddress, repr(subject)) if service.settings['productionLevel'] == 'private': # When on a private machine, send emails only to the current user # Do not use getAddress() for this, since it can return the default # address (config.defaultAddress). privateAddress = cernldap.CERNLDAP().getUserEmail(getpass.getuser()) fromAddress = privateAddress toAddresses = (privateAddress, ) ccAddresses = () else: fromAddress = getAddress(fromAddress) toAddresses = [getAddress(x) for x in toAddresses] ccAddresses = [getAddress(x) for x in ccAddresses] logging.info('Sending email %s from %s with subject %s...', emailID, fromAddress, repr(subject)) smtp.SMTP().sendEmail(subject, body, fromAddress, toAddresses, ccAddresses)
def test_receive_data(self, patched_recv): patched_recv.side_effect = [b'hi', b' how', b' are', b' you?\r\n'] self.assertEqual(smtp.SMTP().receive(), b'hi how are you?\r\n')
def test_encrypt(self, patched_wrap, patched_starttls): smtp.SMTP().encrypt() patched_wrap.assert_called_once() patched_starttls.assert_called_once()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-d', '--debug', action='store_true', default=False, dest='debug', help='Enable debug mode') options = parser.add_mutually_exclusive_group(required=True) options.add_argument('-s', '--single', action='store', dest='pathToSingleMail', help='Single mail', type=str) options.add_argument('-m', '--multiple', action='store', dest='pathToFolderWithMails', help='Folder with mails', type=str) result_of_parsing = parser.parse_args() try: user_info = pathlib.Path("./userinfo.usrinf") user_info.resolve() with open(str(user_info), 'r') as ui: lines = ui.readlines() server = lines[0].strip('\r\n') port = int(lines[1]) login = lines[2].strip('\r\n') password = lines[3].strip('\r\n') except FileNotFoundError: print("Could not find configuration file") sys.exit(1) except PermissionError: print("Did not have enough permissions to read configuration file") sys.exit(1) except (IndexError, ValueError): print("Configuration file had wrong formatting (see 'readme.md'") sys.exit(1) try: aliases_path = pathlib.Path("./aliases.json") aliases_path.resolve() with open(str(aliases_path), 'rb') as al: aliases = aliasloader.AliasLoader().get_dict(al.read()) except (OSError, IOError) as e: if result_of_parsing.debug: print(str(e)) aliases = [] if result_of_parsing.pathToSingleMail is not None: path_to_mail = pathlib.Path(result_of_parsing.pathToSingleMail) try: path_to_mail.resolve() except FileNotFoundError: print("File not found") sys.exit(1) if not path_to_mail.is_file(): print("Not a file") sys.exit(1) try: parser = letterparser.LetterParser(str(path_to_mail), aliases) parser.parse_lines(iterate_file(str(path_to_mail))) mails = list( map(lambda x: mimemail.Mail(aliases, login, *x), parser.get_letter_attributes())) except letterparser.MailParsingException as e: print("'{}' failed with error : '{}'".format(path_to_mail, str(e))) sys.exit(1) else: mails = [] path_to_folder = pathlib.Path(result_of_parsing.pathToFolderWithMails) try: path_to_folder.resolve() except FileNotFoundError: print("Folder not found") sys.exit(1) if not path_to_folder.is_dir(): print("Not a folder") sys.exit(1) for path in path_to_folder.iterdir(): if path.is_dir(): continue if path.suffix != '.lttr': continue try: parser = letterparser.LetterParser(str(path), aliases) parser.parse_lines(str(path)) mails_to_append = list( map(lambda x: mimemail.Mail(aliases, login, *x), parser.get_letter_attributes())) except letterparser.MailParsingException as e: print("'{}' failed with error : '{}'".format(path, str(e))) else: for mail in mails_to_append: mails.append(mail) y = smtp.SMTP(server, port, login, password, result_of_parsing.debug) was_interrupted, delivery_results = y.send(mails) for result in delivery_results: print(result) if was_interrupted: sys.exit(1)
def test_raises_exception_for_invalid_server(self, patched_connect): patched_connect.side_effect = OSError with self.assertRaises(smtp.SMTPException): smtp.SMTP().connect('a', 888)
def test_start_tls(self, patched_sendall, patched_recv): patched_recv.side_effect = [ b'250-go ahead\r\n', b'250 go ahead\r\n', b'220 go ahead\r\n' ] smtp.SMTP().start_tls() patched_sendall.assert_called_with(b'starttls\r\n')
def test_disconnect(self, patched_close, patched_sendall): smtp.SMTP().disconnect() patched_close.assert_called() patched_sendall.assert_called_with(b'quit\r\n')
def test_error_code_raises_exception(self, patched_recv): patched_recv.return_value = b'230-hi\r\n' with self.assertRaises(smtp.SMTPException): smtp.SMTP().check_code(b'220')
def test_socket_timeout(self, patched_recv): patched_recv.side_effect = smtp.socket.timeout with self.assertRaises(smtp.SMTPException): smtp.SMTP().receive()
def test_mail_to(self, patched_recv, patched_send): s = smtp.SMTP() s.sock = Mock(smtp.socket.socket) patched_recv.return_value = b'250-ok\r\n' smtp.SMTP().mail_to('*****@*****.**') patched_send.assert_called_with(b'rcpt to: <*****@*****.**>\r\n')
def test_connects_to_server(self, patched_connect): smtp.SMTP().connect('smtp.gmail.com', 587) patched_connect.assert_called_with(('smtp.gmail.com', 587))