Code Monkey home page Code Monkey logo

aiosmtplib's Introduction

aiosmtplib

"aiosmtplib TravisCI build status" codecov "aiosmtplib on the Python Package Index" pypi-python-versions pypi-status pypi-license "Code style: black"


aiosmtplib is an asynchronous SMTP client for use with asyncio.

For documentation, see Read The Docs.

Quickstart

import asyncio
from email.mime.text import MIMEText

import aiosmtplib

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname="127.0.0.1", port=1025, loop=loop)
loop.run_until_complete(smtp.connect())

message = MIMEText("Sent via aiosmtplib")
message["From"] = "root@localhost"
message["To"] = "[email protected]"
message["Subject"] = "Hello World!"

loop.run_until_complete(smtp.send_message(message))

Requirements

Python 3.5.2+, compiled with SSL support, is required.

Connecting to an SMTP Server

Initialize a new SMTP instance, then await its connect coroutine. Initializing an instance does not automatically connect to the server, as that is a blocking operation.

client = SMTP()

loop = asyncio.get_event_loop()
loop.run_until_complete(client.connect(hostname="localhost", port=25))

Connecting over TLS/SSL

If an SMTP server supports direct connection via TLS/SSL, pass use_tls=True when initializing the SMTP instance (or when calling connect).

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname="smtp.gmail.com", port=465, loop=loop, use_tls=True)
loop.run_until_complete(smtp.connect())

STARTTLS connections

Many SMTP servers support the STARTTLS extension over port 587. When using STARTTLS, the initial connection is made over plaintext, and after connecting a STARTTLS command is sent which initiates the upgrade to a secure connection. To connect to a server that uses STARTTLS, set use_tls to False when connecting, and call starttls on the client.

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname="smtp.gmail.com", port=587, loop=loop, use_tls=False)
loop.run_until_complete(smtp.connect())
loop.run_until_complete(smtp.starttls())

Sending messages

SMTP.send_message

Use send_message to send email.message.Message objects.

from email.mime.text import MIMEText

message = MIMEText("Sent via aiosmtplib")
message["From"] = "root@localhost"
message["To"] = "[email protected]"
message["Subject"] = "Hello World!"

loop = asyncio.get_event_loop()
loop.run_until_complete(smtp.send_message(message))

This is the simplest API, and is the recommended way to send messages, as it makes it easy to set headers correctly and handle multi part messages. For details on creating email.message.Message objects, see the stdlib documentation examples.

SMTP.sendmail

Use sendmail to send raw messages.

sender = "root@localhost"
recipients = ["[email protected]"]
message = """To: [email protected]
From: root@localhost
Subject: Hello World!

Sent via aiosmtplib
"""

loop = asyncio.get_event_loop()
loop.run_until_complete(smtp.sendmail(sender, recipients, message))

Note that when using this method, you must format the message headers yourself.

Bug reporting

Bug reports (and feature requests) are welcome via Github issues.

aiosmtplib's People

Contributors

cole avatar agronholm avatar etodd avatar zh0uquan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.