Skip to main content

deliver_later

Function deliver_later 

Source
pub fn deliver_later(
    ctx: &Ctx,
    email: Email,
) -> impl Future<Output = Result<()>> + Send + use<>
Expand description

Checks email now and sends it later from the background jobs queue, like Rails’ deliver_later.

The address, MAIL_FROM and MAIL_ADAPTER are checked right away, like send, so a bad address is still a 400 for the request. The email is then put on the JOBS queue and the handler answers without waiting for the provider; consume sends it with send, logs [ocre jobs] mail done, and on failure retries it with the jobs backoff (30 s, 1 min, 3 min, 9 min, 27 min) before the dead-letter queue. The Resend key and the send_email binding are only looked up when the consumer sends.

Needs the JOBS queue: run ocre g job <Name> once to wire it.

Free-plan cost: one queue message, i.e. 3 of the 10,000 daily Queues operations (write, read, delete), one more read per retry and one more write if it is dead-lettered; plus the provider’s limits of send.

§Errors

  • Error::BadRequest (400): a recipient or reply_to is not an email address.
  • Error::Internal (500): MAIL_ADAPTER unset or unknown, MAIL_FROM unset or not an address, a subject that is empty or spans several lines, an email over the 128 KB queue message limit, the JOBS queue binding missing from cloudflare.config.ts, or Queues refusing the message.

§Examples

use axum::extract::State;
use ocre::mail::Email;
use ocre::{Ctx, Result};

async fn sign_up(State(ctx): State<Ctx>) -> Result<&'static str> {
    let email = Email::new("ada@example.com", "Welcome", "Hello Ada");
    ocre::mail::deliver_later(&ctx, email).await?;
    Ok("check your inbox")
}