pub async fn receive<F, Fut>(
message: ForwardableEmailMessage,
env: Env,
handler: F,
) -> Result<()>Expand description
Runs the app’s mailbox handler for an email from Cloudflare Email Routing; the Worker’s email entry point.
Reads the raw message, parses it into an InboundEmail, logs one
[ocre mail] received from <from> to <to>: <subject> line and calls
handler(ctx, email) with a fresh Ctx. When the handler
returns an Err, the error is logged ([ocre mail] the mailbox failed: ...) and the email bounced (“The message could not be processed”), so the
sender knows it was not handled. Routing rules in the dashboard (Email
Routing > Routing rules) send an address to the Worker; receiving is free
and unlimited on every plan.
§Errors
A worker::Error only when the raw message cannot be read; handler
errors are logged and bounce the email instead.
§Examples
ocre g mailbox writes the entry point in src/lib.rs and the handler in src/mailbox.rs:
mod mailbox {
use ocre::{Ctx, Result, mail::InboundEmail};
pub async fn receive(_ctx: Ctx, email: InboundEmail) -> Result<()> {
email.forward("team@example.com").await
}
}
#[worker::event(email)]
async fn email(
message: worker::ForwardableEmailMessage,
env: worker::Env,
_ctx: worker::Context,
) -> worker::Result<()> {
ocre::mail::receive(message, env, mailbox::receive).await
}