Miksus/red-mail

ENH: `async` support

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
I'm considering using red-mail to send emails in a backend application. Since connecting to the SMTP server is an I/O blocking operation, I would like to make use of async to avoid blocking other requests while the SMTP server or network are slow.

Describe the solution you'd like
I would like to be able to use red-mail in the following way.

from redmail import EmailSender
from fastapi import FastAPI

app = FastAPI()

@app.post("/send-email")
async def send_email() -> dict[str, str]:
    """Send an example email when this API endpoint is requested."""
    
    email = EmailSender(host='localhost', port=0)  # Real SMTP server would be on a remote network
    
    # By awaiting here, we allow other requests to be processed while waiting for I/O
    # Of course, using a different method name like `asend` would also be fine in order to keep the synchronous methods backwards compatible
    await email.send(  
        subject='async support in red-mail',
        sender="me@example.com",
        receivers=['you@example.com']
    )

    return {"status": "OK"}

Describe alternatives you've considered
Without async support I'd have to introduce additional complexity to get the same results.

  • manually creating threads or processes
  • fork red-mail and make it async
  • introduce a task queue and hand over email sending tasks

@Miksus If you're open to this, please let me know :) Thank you!