How to use aiosmtpd - 10 common examples

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 / smtpd.py View on Github external
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):
        """Advertise auth login support."""
        session.host_name = hostname
        return "250-AUTH LOGIN\r\n250 HELP"


class TestSMTPD(SMTPD):
    def _getaddr(self, arg):
        """
        Don't raise an exception on unparsable email address
        """
        try:
            return super()._getaddr(arg)
        except HeaderParseError:
            return None, ""

    async def _call_handler_hook(self, command, *args):
        self.event_handler.record_command(command, *args)
        return await super()._call_handler_hook(command, *args)

    async def push(self, status):
        result = await super().push(status)
        self.event_handler.record_server_response(status)
github cole / aiosmtplib / tests / testserver / smptd_server.py View on Github external
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)


class TestSMTPD(SMTPD):
    def _getaddr(self, arg):
        """
        Don't raise an exception on unparsable email address
        """
        try:
            return super()._getaddr(arg)
        except HeaderParseError:
            return None, ""

    async def _call_handler_hook(self, command, *args):
        self.event_handler.record_command(command, *args)

        hook_response = await super()._call_handler_hook(command, *args)
        response_message = getattr(
            self.event_handler, command + "_response_message", None
        )
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 lindsay-stevens / limesurveyrc2api / tests / utils.py View on Github external
def __init__(self):
        self.messages = []
        self.handler = CapturingAiosmtpdHandler(context=self)
        self.controller = Controller(
            handler=self.handler, hostname="localhost", port=10025)
github cole / aiosmtplib / tests / testserver / smptd_server.py View on Github external
async def smtp_EXPN(self, arg):
        """
        Pass EXPN to handler hook.
        """
        status = await self._call_handler_hook("EXPN")
        await self.push("502 EXPN not implemented" if status is MISSING else status)
github cole / aiosmtplib / tests / smtpd.py View on Github external
async def smtp_HELP(self, arg):
        """
        Override help to pass to handler hook.
        """
        status = await self._call_handler_hook("HELP")
        if status is MISSING:
            await super().smtp_HELP(arg)
        else:
            await self.push(status)
github aio-libs / aiosmtpd / aiosmtpd / testing / nose.py View on Github external
"""nose2 test infrastructure."""

import os
import re
import doctest
import logging
import aiosmtpd

from contextlib import ExitStack
from nose2.events import Plugin

DOT = '.'
FLAGS = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF
TOPDIR = os.path.dirname(aiosmtpd.__file__)


def setup(testobj):
    testobj.globs['resources'] = ExitStack()


def teardown(testobj):
    testobj.globs['resources'].close()


class NosePlugin(Plugin):
    configSection = 'aiosmtpd'

    def __init__(self):
        super(NosePlugin, self).__init__()
        self.patterns = []
github aio-libs / aiosmtpd / aiosmtpd / main.py View on Github external
if args.setuid:                               # pragma: nomswin
        if pwd is None:
            print('Cannot import module "pwd"; try running with -n option.',
                  file=sys.stderr)
            sys.exit(1)
        nobody = pwd.getpwnam('nobody').pw_uid
        try:
            os.setuid(nobody)
        except PermissionError:
            print('Cannot setuid "nobody"; try running with -n option.',
                  file=sys.stderr)
            sys.exit(1)

    factory = partial(
        SMTP, args.handler,
        data_size_limit=args.size, enable_SMTPUTF8=args.smtputf8)

    logging.basicConfig(level=logging.ERROR)
    log = logging.getLogger('mail.log')
    loop = asyncio.get_event_loop()

    if args.debug > 0:
        log.setLevel(logging.INFO)
    if args.debug > 1:
        log.setLevel(logging.DEBUG)
    if args.debug > 2:
        loop.set_debug(enabled=True)

    log.info('Server listening on %s:%s', args.host, args.port)

    server = loop.run_until_complete(
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)