Skip to content

Python client library for the User Notify service

The ucam-user-notify library provides opinionated implementations of ways to interact with the DevOps shared user notify service.

Example

Minimal example
#!/usr/bin/env python
"""
A minimal example of using ucam_user_notify to send an email.
"""
from typing import Optional

from ucam_user_notify import EmailMessage, Session

# ID of service as registered with the user notify service.
SERVICE_ID = "testing-development"

# If you're running this code from a Cloud Run service, do not use
# impersonation and instead set this to None or omit it.
IMPERSONATE_GOOGLE_SERVICE_ACCOUNT: Optional[str] = (
    "service-testing@user-notify-devel-2ff1dc8d.iam.gserviceaccount.com"
)

# A session will perform some one-time authentication and fetch of
# configuration when constructed so that this overhead is not present
# for each call to send_email().
session = Session.for_service(
    SERVICE_ID,
    impersonate_service_account=IMPERSONATE_GOOGLE_SERVICE_ACCOUNT,
    # DO NOT SET environment WHEN YOU USE THIS LIBRARY. THIS IS ONLY
    # HERE TO ENSURE THAT THIS EXAMPLE CANNOT SEND EMAIL OUTSIDE OF
    # THE EMAIL SANDBOX.
    environment="development",
)

# If no from_address is set, the default from address for the service
# will be used.
message_id = session.send_email(
    EmailMessage(
        to_addresses=["success@simulator.amazonses.com"],
        subject="testing",
        body_text="This is a test.",
    )
)

print(f"Message id: {message_id}")

# vim:tw=70