Skip to main content

Prefect integrations for interacting with email.

Project description

prefect-email

PyPI

Visit the full docs here to see additional examples and the API reference.

prefect-email is a collection of prebuilt Prefect integrations that can be used to interact with email services.

Getting Started

Integrate with Prefect flows

prefect-email makes sending emails effortless, giving you peace of mind that your emails are being sent as expected.

First, install prefect-email and save your email credentials to a block to run the examples below!

from prefect import flow
from prefect_email import EmailServerCredentials, email_send_message

@flow
def example_email_send_message_flow(email_addresses):
    email_server_credentials = EmailServerCredentials.load("BLOCK-NAME-PLACEHOLDER")
    for email_address in email_addresses:
        subject = email_send_message.with_options(name=f"email {email_address}").submit(
            email_server_credentials=email_server_credentials,
            subject="Example Flow Notification using Gmail",
            msg="This proves email_send_message works!",
            email_to=email_address,
        )

example_email_send_message_flow(["EMAIL-ADDRESS-PLACEHOLDER"])

Outputs:

16:58:27.646 | INFO    | prefect.engine - Created flow run 'busy-bat' for flow 'example-email-send-message-flow'
16:58:29.225 | INFO    | Flow run 'busy-bat' - Created task run 'email someone@gmail.com-0' for task 'email someone@gmail.com'
16:58:29.229 | INFO    | Flow run 'busy-bat' - Submitted task run 'email someone@gmail.com-0' for execution.
16:58:31.523 | INFO    | Task run 'email someone@gmail.com-0' - Finished in state Completed()
16:58:31.713 | INFO    | Flow run 'busy-bat' - Finished in state Completed('All states completed.')

Please note, many email services, like Gmail, require an App Password to successfully send emails. If you encounter an error similar to smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted..., it's likely you are not using an App Password.

Capture exceptions and notify by email

Perhaps you want an email notification with the details of the exception when your flow run fails.

prefect-email can be wrapped in an except statement to do just that!

from prefect import flow
from prefect.context import get_run_context
from prefect_email import EmailServerCredentials, email_send_message

def notify_exc_by_email(exc):
    context = get_run_context()
    flow_run_name = context.flow_run.name
    email_server_credentials = EmailServerCredentials.load("email-server-credentials")
    email_send_message(
        email_server_credentials=email_server_credentials,
        subject=f"Flow run {flow_run_name!r} failed",
        msg=f"Flow run {flow_run_name!r} failed due to {exc}.",
        email_to=email_server_credentials.username,
    )

@flow
def example_flow():
    try:
        1 / 0
    except Exception as exc:
        notify_exc_by_email(exc)
        raise

example_flow()

Resources

For more tips on how to use tasks and flows in a Collection, check out Using Collections!

Installation

Install prefect-email with pip:

pip install prefect-email

Then, register to view the block on Prefect Cloud:

prefect block register -m prefect_email

Note, to use the load method on Blocks, you must already have a block document saved through code or saved through the UI.

Requires an installation of Python 3.8+.

We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.

These tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the Prefect documentation.

Saving credentials to block

Note, to use the load method on Blocks, you must already have a block document saved through code or saved through the UI.

Below is a walkthrough on saving block documents through code.

Create a short script, replacing the placeholders.

from prefect_email import EmailServerCredentials

credentials = EmailServerCredentials(
    username="EMAIL-ADDRESS-PLACEHOLDER",
    password="PASSWORD-PLACEHOLDER",  # must be an app password
)
credentials.save("BLOCK-NAME-PLACEHOLDER")

Congrats! You can now easily load the saved block, which holds your credentials:

from prefect_email import EmailServerCredentials

EmailServerCredentials.load("BLOCK_NAME_PLACEHOLDER")

!!! info "Registering blocks"

Register blocks in this module to
[view and edit them](https://docs.prefect.io/ui/blocks/)
on Prefect Cloud:

```bash
prefect block register -m prefect_email
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prefect_email-0.3.6.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

prefect_email-0.3.6-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file prefect_email-0.3.6.tar.gz.

File metadata

  • Download URL: prefect_email-0.3.6.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for prefect_email-0.3.6.tar.gz
Algorithm Hash digest
SHA256 1d91b959e188783fefe6c6887896d20c5598ace381dcab075467dd44f07f2047
MD5 7b8f24d914a9f8387df9480dc4e2c8a4
BLAKE2b-256 11a0b1e1da7115be3376b6ad93d35ebef22b8d3f3d64302bf82d40fcb3fb2585

See more details on using hashes here.

File details

Details for the file prefect_email-0.3.6-py3-none-any.whl.

File metadata

File hashes

Hashes for prefect_email-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 418a223618c2e0ae0268fa208e4a58ce645a65d82d74941213c7143b2d016723
MD5 0e1e8746f9b499b7743a70eb2e8abd12
BLAKE2b-256 4bda0ae0db7a01947e91e52db1b7f4d2465678e2c2497458240eb7b7432602e3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page