PyMailIOTask¶
Main Public API for sending emails by adding it to the asyncio task queue without having to block waiting for a response.
-
class
pymail_io.pymailio_task.
Task
(queue: pytask_io.pytask_io.PyTaskIO = <pytask_io.pytask_io.PyTaskIO object>, email: pymail_io.email.Email = <pymail_io.email.Email object>, *args, **kwargs)[source]¶ PyMailIO is a python library built on CPython’s AsyncIO library. The entree to asyncio is via PyTaskIO which is an asynchronous task queue library that runs an event loop in a background thread. First, download and install & run the Redis image. Example:
docker run Redis
Now, we are ready to use PyMailIO. Basic Usage. Example:
from pymail_io.pymailio_task import Task p = Task( password="wizard", sender_email="your_email@gmail.com", email_host="smtp.gmail.com", ) # Create your email subject & body email_meta = p.send_email( subject="The subject...", body="The email body...", receiver_email="joe@blogs.com", # Or a list of emails receiver_email=["joe@blogs.com", ...], ) # Get a response from your sent email: res = p.get_email_response(email_meta)
To update the task queue & store settings, you can pass in extra values as kwargs to the Task class. For example:
p = Task( password="wizard", sender_email="your_email@gmail.com", email_host="smtp.gmail.com", # extra settings: store_port=6379, store_host="localhost", db=0, workers=1, )
- Kwargs
- Key password
Your senders email password.
- Key receiver_email
This can be either a string or a list of email addresses.
- Key sender_email
The senders email address.
- Key store_port
Redis store port (defaults to 6379).
- Key store_host
The email server host.
- Key db
The Redis store database name.
- Key workers
The number of workers created to run tasks in the queue.
- Key email_host
The email server host.
- Key email_port
The email server SSL or TLS port.
-
datetime_exec
() → str[source]¶ There are 2 datetime values that reference when PyMailIO executed the send_email method & also when the email was actually sent from the background queue: The datetime_exec method will give you the datetime value that PyMailIO executed the send_email method. For example:
r = p.send_email( subject="The subject...", body="The email body...", ) self.datetime_exec()
- Returns
-
db
: int = None¶ The Redis store database name.
-
exec_time
(data: Dict[str, Dict[str, Any]]) → str[source]¶ - Parameters
data – The response Dict from the send_email method.
There are 2 datetime values that reference when PyMailIO executed the send_email method & also when the email was actually sent from the background queue: The exec_time method will give you the datetime value that PyMailIO’s queue executed the send_email method. For example:
r = p.send_email( subject="The subject...", body="The email body...", ) # Some time in the future... r = get_email_response(r) time_email_sent = self.exec_time(r)
- Returns
-
get_email_response
(email_res: Dict[str, Dict[str, Any]]) → Any[source]¶ To get the results of the email from the store, pass the metadata to get_email_response. For example:
r = p.send_email( subject="The subject...", body="The email body...", ) email_meta = p.get_email_response(r)
- Parameters
email_res –
- Returns
-
run_forever
: bool = False¶ Set to False by default. If you want to keep the asyncio task queue running in the background thread then set this to true. Setting run_forever to True, will give much faster performances & is ideal when you can run the event loop thread against a long running process, such as the life time process of a web framework or rest API library.
-
send_email
(*, subject: str, body: str, receiver_email: Union[str, List[str]]) → Any[source]¶ Example:
email_meta = p.send_email( subject="The subject...", body="The email body...", )
- Parameters
subject –
body –
receiver_email – Example: “joe@blogs.com”, # Or a list of emails receiver_email=[“joe@blogs.com”, …],
- Returns
-
store_host
: str = None¶ store_host: The email server host.
-
store_port
: int = None¶ The Redis port (this will default to 6379).
-
workers
: int = None¶ The number of workers created to run tasks in the queue.