The EmailHandler is a simple and effective tool for managing email communication in your applications. It allows you to send emails easily, handle multiple recipients, and include attachments, making it perfect for both personal and professional use.

From: The email address of the sender.
To: The primary recipient(s) of the email.
Cc: Additional recipients who will receive a copy of the email, visible to all.
Bcc: Recipients who receive a copy of the email without other recipients knowing their addresses.
Subject: A brief summary or title of the email’s content.
Body: The main content or message of the email, where the sender communicates their thoughts or information.

Setup

Install Postfix locally by running it on your machine. For more detail refer here.

Note: Postfix can run on UNIX-like systems including AIX, BSD, HP-UX, Linux, MacOS X, Solaris, and more.

Example

This code initializes an EmailHandler to connect to an email server using the specified host and port number. This setup enables sending, receiving, or managing emails programmatically.

email_handler.py
import asyncio

from superagentx.handler.send_email import EmailHandler


async def send_email():
    email_handler = EmailHandler(
        host="mail.example.com",
        port=0
    )
    body = {
        "sender": "sample123@abcd.io",
        "to": ["test231@abcdf.io"],
        "subject": "test",
        "body": "Hi"
    }
    return await email_handler.send_email(**body)


async def main():
    res = await send_email()
    print(res)

if __name__ == '__main__':
    asyncio.run(main())