def test_prepare_message_err(self, temp_event_loop, content, expectre): sess_ = ServerSession(temp_event_loop) enve_ = Envelope() handler = MessageHandler() enve_.content = content with pytest.raises(TypeError, match=expectre): _ = handler.prepare_message(sess_, enve_)
def test_prepare_message(self, temp_event_loop, content): sess_ = ServerSession(temp_event_loop) enve_ = Envelope() handler = MessageHandler() enve_.content = content msg = handler.prepare_message(sess_, enve_) assert isinstance(msg, Em_Message) assert msg.keys() == ['X-Peer', 'X-MailFrom', 'X-RcptTo'] assert msg.get_payload() == ""
async def handle_DATA(self, server: SMTP, session: Session, envelope: Envelope) -> str: signal_numbers = [] mail_addresses = [] for addr in envelope.rcpt_tos: # a real email address cannot start with a special char if addr.startswith("+"): signal_numbers.append(addr) else: mail_addresses.append(addr) # send signal message if required if len(signal_numbers) > 0: print("Forwarding message to signal") success = await self.send_signal(envelope, signal_numbers) if not success: return "554 Sending signal message has failed" # send email if required if len(mail_addresses) == 0: return "250 Message accepted for delivery" else: envelope.rcpt_tos = mail_addresses print( f"Sending email via MTA. From: {envelope.mail_from} To: {envelope.rcpt_tos}" ) return send_mail( self.config["smtp_host"], int(self.config["smtp_port"]), self.config["smtp_user"], self.config["smtp_passwd"], envelope, )
async def open_file(self, file_path) -> Union[None, Tuple[Envelope, str]]: try: async with aiofiles.open(file_path, mode='rb') as file: content = await file.read() except OSError: self.logger.exception( f'Error in FileManager.open_file() while opening/reading at {file_path}' ) return None if self.encrypt: pass try: encoded = json.loads(base64.b64decode(content).decode('ascii')) encoded['original_content'] = base64.b64decode( encoded['original_content']) encoded['content'] = base64.b64decode(encoded['content']) envelope = Envelope() envelope.__dict__.update(encoded) peer = envelope.__dict__.pop('peer') except KeyError: self.logger.exception( 'Error in FileManager.open_file() while accessing JSON keys') return None except ValueError: self.logger.exception( 'Error in FileManager.open_file() while decoding the envelope and peer' ) return None return envelope, peer
async def handle_MAIL(self, server: SMTP, session: Session, envelope: Envelope, address: str, mail_options: list): ip = session.peer[0] result, description = spf.check2(ip, address, session.host_name) valid_spf = result == 'pass' envelope.spf = valid_spf log.info("SPF: %s, %s", result, description) if self.verify_spf and not valid_spf: return '550 SPF validation failed' envelope.mail_from = address envelope.mail_options.extend(mail_options) return '250 OK'
def setup_method(self, method): self.handler = ExampleHandler('gmail.com', verify_spf=True) self.envelope = Envelope() self.envelope.content = b"""From: Cate Person <*****@*****.**> To: Dave Person <*****@*****.**> Subject: A test Message-ID: <bee> Hi Dave, this is Cate.""" self.server = SMTP(self.handler)
async def test_handle_MAIL_spf(self): server = SMTP(self.handler) session = Session(server.loop) session.peer = ['209.85.220.69', 65000] session.host_name = 'gmail.com' response = await self.handler.handle_MAIL( self.server, session, Envelope(), '*****@*****.**', [] ) assert response == '250 OK'
async def handle_DATA(self, server: SMTP, session: Session, envelope: Envelope): valid_dkim = dkim.verify(envelope.content) envelope.dkim = valid_dkim log.info("DKIM: %s", test_results[valid_dkim]) message: Message = email.message_from_bytes(envelope.content) log.info('From: %s', message['From']) log.info('To: %s', message['To']) log.info('Subject: %s', message['Subject']) log.info('Message data:\n%s', message_to_display(message)) if self.verify_dkim and not valid_dkim: return '550 DKIM validation failed' return '250 Message accepted for delivery'
def envelope(): env = Envelope() return env