How to use the aiosmtplib.SMTP function in aiosmtplib

To help you get started, we’ve selected a few aiosmtplib 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 / test_config.py View on Github external
async def test_connect_tls_context_option_takes_precedence(
    hostname, smtpd_server_port, client_tls_context, server_tls_context
):
    client = SMTP(
        hostname=hostname, port=smtpd_server_port, tls_context=server_tls_context
    )

    await client.connect(tls_context=client_tls_context)

    assert client.tls_context is client_tls_context

    await client.quit()
github cole / aiosmtplib / tests / test_config.py View on Github external
async def test_connect_event_loop_takes_precedence(
    event_loop, event_loop_policy, hostname, smtpd_server_port
):
    init_loop = event_loop_policy.new_event_loop()
    with pytest.warns(DeprecationWarning):
        client = SMTP(hostname=hostname, port=smtpd_server_port, loop=init_loop)

    with pytest.warns(DeprecationWarning):
        await client.connect(loop=event_loop)

    assert init_loop is not event_loop
    assert client.loop is event_loop

    await client.quit()
github cole / aiosmtplib / tests / test_connect.py View on Github external
async def test_connect_error_second_attempt(hostname, unused_tcp_port):
    client = SMTP(hostname=hostname, port=unused_tcp_port, timeout=1.0)

    with pytest.raises(SMTPConnectError):
        await client.connect()

    with pytest.raises(SMTPConnectError):
        await client.connect()
github cole / aiosmtplib / tests / test_config.py View on Github external
async def test_default_port_on_connect(
    event_loop, bind_address, use_tls, start_tls, expected_port
):
    client = SMTP()

    try:
        await client.connect(
            hostname=bind_address, use_tls=use_tls, start_tls=start_tls, timeout=0.001
        )
    except (asyncio.TimeoutError, OSError):
        pass

    assert client.port == expected_port

    client.close()
github cole / aiosmtplib / tests / test_asyncio.py View on Github external
async def test_close_works_on_stopped_loop(smtpd_server, event_loop, hostname, port):
    client = SMTP(hostname=hostname, port=port)

    await client.connect()
    assert client.is_connected
    assert client._writer is not None

    event_loop.stop()

    client.close()
    assert not client.is_connected
github cole / aiosmtplib / aiosmtplib.py View on Github external
return sys.stdin.readline().strip()

    sender = prompt("From")
    recipients = prompt("To").split(',')
    print("Enter message, end with ^D:")
    message = []
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        message.append(line)
    message = '\n'.join(message)
    print("Message length is %d" % len(message))

    loop = asyncio.get_event_loop()
    smtp = SMTP(hostname='localhost', port=25, loop=loop)
    send_message = asyncio.async(smtp.sendmail(sender, recipients, message))
    loop.run_until_complete(send_message)
github imbolc / aiohttp-login / aiohttp_login / utils.py View on Github external
loop=cfg.APP.loop,
        hostname=cfg.SMTP_HOST,
        port=cfg.SMTP_PORT,
        use_tls=cfg.SMTP_TLS,
    )

    msg = MIMEText(body, 'html')
    msg['Subject'] = subject
    msg['From'] = cfg.SMTP_SENDER
    msg['To'] = recipient

    if cfg.SMTP_PORT == 587:
        # aiosmtplib does not handle port 587 correctly
        # plaintext first, then use starttls
        # this is a workaround
        smtp = aiosmtplib.SMTP(**smtp_args)
        await smtp.connect(use_tls=False, port=cfg.SMTP_PORT)
        if cfg.SMTP_TLS:
            await smtp.starttls(validate_certs=False)
        if cfg.SMTP_USERNAME:
            await smtp.login(cfg.SMTP_USERNAME, cfg.SMTP_PASSWORD)
        await smtp.send_message(msg)
        await smtp.quit()
    else:
        async with aiosmtplib.SMTP(**smtp_args) as smtp:
            if cfg.SMTP_USERNAME:
                await smtp.login(cfg.SMTP_USERNAME, cfg.SMTP_PASSWORD)
            await smtp.send_message(msg)
github beebyte / irisett / irisett / notify / email.py View on Github external
async def send_email(loop: asyncio.AbstractEventLoop, mail_from: str, mail_to: Union[Iterable, str],
                     subject: str, body: str, server: str='localhost') -> None:
    """Send an email to one or more recipients.

    Only supports plain text emails with a single message body.
    No attachments etc.
    """
    if type(mail_to) == str:
        mail_to = [mail_to]
    smtp = aiosmtplib.SMTP(hostname=server, port=25, loop=loop)
    try:
        await smtp.connect()
        for rcpt in mail_to:
            msg = MIMEText(body)
            msg['Subject'] = subject
            msg['From'] = mail_from
            msg['To'] = rcpt
            await smtp.send_message(msg)
        await smtp.quit()
    except aiosmtplib.errors.SMTPException as e:
        log.msg('Error sending smtp notification: %s' % (str(e)), 'NOTIFICATIONS')
github plone / guillotina / guillotina / contrib / mailer / utility.py View on Github external
async def connect(self):
        try:
            self.conn = aiosmtplib.SMTP(self.settings["host"], self.settings["port"])
            await self.conn.connect()
            if "username" in self.settings:
                await self.conn.login(self.settings["username"], self.settings["password"])
            if "tls" in self.settings and self.settings["tls"]:
                await self.conn.starttls()
        except Exception:
            logger.error("Error connecting to smtp server", exc_info=True)