Skip to main content

Mailgun

rbt.thirdparty.mailgun.v1


Third-party integration that supports sending email messages using the Mailgun API.

To make the integration part of your application, include its servicers when constructing your Application, using the servicers() helper from the reboot.thirdparty.mailgun module:

import reboot.thirdparty.mailgun
from reboot.aio.applications import Application

application = Application(
servicers=[YourServicer] + reboot.thirdparty.mailgun.servicers(),
)

To use the Mailgun integration, set the MAILGUN_API_KEY environment variable to your Mailgun API key. See Secrets for how to set this for local development and for deployments to Reboot Cloud.

Calls to the integration must also authenticate using that same API key as the bearer token: the integration only accepts calls whose bearer token matches MAILGUN_API_KEY, and rejects all other calls as unauthenticated. Pass the key using Options when calling Message.send:

from reboot.aio.call import Options

await Message.send(
context,
Options(bearer_token=mailgun_api_key),
recipient="[email protected]",
sender="Your App <[email protected]>",
domain="yourdomain.com",
subject="Hello!",
text="Hello from Reboot!",
)

Message

A single message sent using the integration.

Created and scheduled using its constructor: await Message.send(...).

Send

rpc Send(SendRequest) SendResponse

Construct and send an email message using the Mailgun API.

Returns a task_id which can be used to wait for the message to have been sent.

Messages

SendRequest

See Send.

FieldTypeDescription
recipientstringThe email address of the recipient of the message.
senderstringThe email address of the sender of the message.
subjectstringThe subject of the message.
domainstringThe domain to send from.
body.textstringThe body content of the message, as text. Set exactly one of body.text and body.html.
body.htmlstringThe body content of the message, as HTML.

SendResponse

See Send.

FieldTypeDescription
task_idrbt.v1alpha1.TaskIdID of the task scheduled to send the email.

Testing

If your servicer sends emails, you can mock this functionality using reboot.thirdparty.mailgun.servicers.MockMessageServicer. This mock service stores emails in memory, allowing you to verify the emails sent during your tests. Simply pass it as a servicer to the Reboot class servicers list and use the emails_sent list to check the emails sent.

# Some servicer method call, which should send an email.

await MockMessageServicer.emails_sent_sema.acquire()
self.assertEqual(1, len(MockMessageServicer.emails_sent))
note

When using MockMessageServicer, ensure that the MAILGUN_API_KEY environment variable is set (any non-empty value works for the mock).