How to use the aiosmtpd.handlers.Message function in aiosmtpd

To help you get started, we’ve selected a few aiosmtpd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cole / aiosmtplib / tests / testserver / smptd_server.py View on Github external
from email.errors import HeaderParseError
from email.message import Message

from aiosmtpd.handlers import Message as MessageHandler
from aiosmtpd.smtp import SMTP as SMTPD, MISSING


class RecordingHandler(MessageHandler):
    HELO_response_message = None
    EHLO_response_message = None
    NOOP_response_message = None
    QUIT_response_message = None
    VRFY_response_message = None
    MAIL_response_message = None
    RCPT_response_message = None
    DATA_response_message = None
    RSET_response_message = None
    EXPN_response_message = None
    HELP_response_message = None

    def __init__(self, messages_list, commands_list, responses_list):
        self.messages = messages_list
        self.commands = commands_list
        self.responses = responses_list
github cole / aiosmtplib / tests / smtpd.py View on Github external
import socket
import threading
from email.errors import HeaderParseError
from email.message import Message

from aiosmtpd.handlers import Message as MessageHandler
from aiosmtpd.smtp import MISSING
from aiosmtpd.smtp import SMTP as SMTPD

from aiosmtplib.sync import shutdown_loop


log = logging.getLogger("mail.log")


class RecordingHandler(MessageHandler):
    def __init__(self, messages_list, commands_list, responses_list):
        self.messages = messages_list
        self.commands = commands_list
        self.responses = responses_list
        super().__init__(message_class=Message)

    def record_command(self, command, *args):
        self.commands.append((command, *args))

    def record_server_response(self, status):
        self.responses.append(status)

    def handle_message(self, message):
        self.messages.append(message)

    async def handle_EHLO(self, server, session, envelope, hostname):
github ont / slacker / handler.py View on Github external
import os
import re
import yaml
import slack
import slack.chat
from aiosmtpd.handlers import Message


class MessageHandler(Message):
    def __init__(self, *args, **kargs):
        Message.__init__(self, *args, **kargs)

        config = os.getenv('CONFIG', '/etc/slacker/config.yml')
        print(config)
        if not os.path.exists(config):
            print('Config doesn\'t exists!')
            exit(1)

        self.config = yaml.load(open(config))

    def handle_message(self, message):
        """ This method will be called by aiosmtpd server when new mail will
            arrived.
        """
        options = self.process_rules(message)
github aio-libs / aiosmtpd / aiosmtpd / handlers.py View on Github external
message = message_from_bytes(data, self.message_class)
        else:
            assert isinstance(data, str), (
              'Expected str or bytes, got {}'.format(type(data)))
            message = message_from_string(data, self.message_class)
        message['X-Peer'] = str(session.peer)
        message['X-MailFrom'] = envelope.mail_from
        message['X-RcptTo'] = COMMASPACE.join(envelope.rcpt_tos)
        return message

    def handle_message(self, message):
        raise NotImplementedError                   # pragma: nocover


@public
class AsyncMessage(Message):
    def __init__(self, message_class=None, *, loop=None):
        super().__init__(message_class)
        self.loop = loop or asyncio.get_event_loop()

    @asyncio.coroutine
    def handle_DATA(self, server, session, envelope):
        message = self.prepare_message(session, envelope)
        yield from self.handle_message(message)
        return '250 OK'

    @asyncio.coroutine
    def handle_message(self, message):
        raise NotImplementedError                   # pragma: nocover


@public
github shantanugoel / email-actions / email_actions / server.py View on Github external
# Infer the type from the host.
        addr = host, port
        if ':' in host:
          addr += 0, 0
          type_ = socket.AF_INET6
        else:
          type_ = socket.AF_INET
          info_0 = type_, socket.SOCK_STREAM, 0, '', addr
          info = info_0,

    family, type, proto, canonname, addr = next(iter(info))
    sock = bind(family, type, proto)
    return sock


class MessageHandler(Message):
  filter_obj = None

  def __init__(self, message_class=None):
    super().__init__(message_class)
    self.filter_obj = Filter()

  def handle_message(self, message):
    logging.debug(message)
    loop = asyncio.get_event_loop()
    filter_action = partial(
      self.filter_obj.filter, message['From'], message['To'],
      message['Subject'], message.get_payload()
    )
    loop.run_in_executor(None, filter_action)
github aio-libs / aiosmtpd / aiosmtpd / handlers.py View on Github external
super().__init__(message_class)
        self.loop = loop or asyncio.get_event_loop()

    @asyncio.coroutine
    def handle_DATA(self, server, session, envelope):
        message = self.prepare_message(session, envelope)
        yield from self.handle_message(message)
        return '250 OK'

    @asyncio.coroutine
    def handle_message(self, message):
        raise NotImplementedError                   # pragma: nocover


@public
class Mailbox(Message):
    def __init__(self, mail_dir, message_class=None):
        self.mailbox = mailbox.Maildir(mail_dir)
        self.mail_dir = mail_dir
        super().__init__(message_class)

    def handle_message(self, message):
        self.mailbox.add(message)

    def reset(self):
        self.mailbox.clear()

    @classmethod
    def from_cli(cls, parser, *args):
        if len(args) < 1:
            parser.error('The directory for the maildir is required')
        elif len(args) > 1: