Skip to content

Use Connections for Mail ​

Experimental

Connections are experimental and off by default. Turn them on with experimental_connections: true in config.yaml, or DAGU_EXPERIMENTAL_CONNECTIONS=true. While they are off, mail that signs in through a connection fails. See Turn Connections On.

Three places send mail, and each can sign in with a connection's token in place of a password or an oauth block:

WhereFieldUsed by
Mail accountsmail_accounts.<address>.connectionmail.search, mail.organize, and mail.send with mailbox
A DAG's smtpsmtp.connectionmail.send without mailbox, and the DAG's mail_on emails
Notification SMTP settingsThe notification connectionEmail channels of the Web UI's notification rules

Dagu signs in with XOAUTH2. The connection refreshes the token, saves a rotated refresh token, and reports a revoked sign-in once, so mail has one refresh path instead of a refresh token copied into each DAG.

What the Connection Needs ​

Mail takes an oauth2 connection. Its tokens must be accepted by the mail server, and its bound hosts must name the servers:

  • Scopes: whatever the mail server's IMAP and SMTP sign-in needs, plus openid to show the account and the scope that returns a refresh token, such as offline_access.
  • Bound hosts: each IMAP and SMTP host. A bound host without a port matches IMAP on 993, and SMTP on 465 or 587. Add host:port for any other port, such as mail.example.com:143 for IMAP with STARTTLS.

The token goes only to a server on the bound hosts, over TLS with a verified certificate. Dagu checks both before it sends the token, and refuses a server that fails either check without signing in.

An mcp connection's token is issued for its MCP server alone, so do not name one for mail. The editor warns on it.

Mail Accounts ​

Name the connection in place of password or oauth:

yaml
labels:
  - workspace=ops

mail_accounts:
  support@example.com:
    imap:
      host: mail.example.com
    smtp:
      host: mail.example.com
    connection: support-mail

steps:
  - id: find
    action: mail.search
    with:
      mailbox: support@example.com
      unread: true
  • connection cannot be combined with password or oauth, even an empty one.
  • The account keeps its provider, imap, and smtp blocks, as Mail Accounts describes. An imap account needs imap.host, and smtp.host to send. A google or microsoft account takes its provider's servers.
  • skip_tls_verify and tls_cert_sha256 cannot be combined with connection, since either lets another server take the token. Validation fails.
  • username, which defaults to the address, is the user the token signs in as.
  • A google account with connection reads mail over IMAP and SMTP, not the Gmail API that a google account with oauth uses. See Gmail.
  • Dagu checks no mail address against the connection's account. The mail server decides which mailboxes the token opens.

mail_accounts in a base config may name a connection too. Each run resolves the name in its own workspace.

A DAG's smtp ​

smtp.connection signs in mail.send steps without mailbox, and the emails that mail_on sends:

yaml
labels:
  - workspace=ops

smtp:
  host: smtp.example.com
  username: alerts@example.com
  connection: alerts-mail

mail_on:
  failure: true

error_mail:
  from: alerts@example.com
  to: oncall@example.com

steps:
  - id: report
    action: mail.send
    with:
      from: alerts@example.com
      to: team@example.com
      subject: Nightly report
      message: The nightly run finished.
  • host and username are required. password and oauth are not allowed.
  • port defaults to 587. The port picks the transport: TLS from the first byte on 465, and STARTTLS on any other port.
  • host must be one of the connection's bound hosts.
  • A DAG's smtp that names a connection replaces a base config's smtp as a whole, as an oauth block does. A base config's smtp.connection resolves in each run's workspace.

Checks at Use ​

A run does not check mail connections before its first step. A step or an email that uses one checks it when it signs in:

  • The name resolves in the run's workspace. A DAG that never sends mail through the account or smtp runs as before, even when its workspace lacks the connection.
  • A pending or needs_reconnect connection, or one whose app is invalid, fails the step with reconnect required: connection "<name>": <reason>, without calling the provider. Steps that depend on it do not run and are marked aborted, unless continue_on says otherwise. Steps that do not depend on it keep running.
  • A server off the bound hosts, or one without a verified certificate, fails the step before the token is sent.
  • An IMAP or SMTP server that refuses the token is handled as an HTTP 401: Dagu refreshes once, when the refused token is still the stored one, and signs in again. A second refusal fails the step.

A mail_on email resolves the connection when it is sent, so a run that sends none reads no connection. A failed email is logged as Mail notification failed, and the run's status does not change.

No mail step prints the token, and its errors show none.

Gmail ​

A generic oauth2 connection signs in to Gmail over IMAP and SMTP. Gmail's IMAP and SMTP accept only a token with the https://mail.google.com/ scope:

  • Scopes: openid, email, and https://mail.google.com/.
  • Bound hosts: imap.gmail.com and smtp.gmail.com.
  • Google returns a refresh token only when the authorization request asks for offline access. Register the app's authorization URL with access_type=offline in its query, such as https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent.
yaml
labels:
  - workspace=ops

mail_accounts:
  ops@example.com:
    provider: google
    connection: ops-mail

steps:
  - id: find
    action: mail.search
    with:
      mailbox: ops@example.com
      within: 24h

provider: google supplies imap.gmail.com:993 and smtp.gmail.com:465. A Google connection preset, with Gmail's scopes and hosts filled in, is not available yet.

Notification SMTP Settings ​

The Web UI's email channels send through Email Delivery on the Notifications page. Email Delivery can sign in through the notification connection:

  • The notification connection is one oauth2 connection that belongs to no workspace. No DAG can name it.
  • Whoever may change the notification settings manages it: developer or above.
  • It takes the authorization code grant or, added by an admin, the client credentials grant. It does not take the device code grant.

To set it up:

  1. On Notifications, open Email Delivery and pick Notification connection under SMTP authentication.
  2. Enter the SMTP host, port, username, and default sender, and save. host, port, and username are required, and the host must be one of the connection's bound hosts. The port picks TLS or STARTTLS as a DAG's smtp does.
  3. The Notification connection panel appears. Pick an oauth2 app, enter the scopes and the bound hosts, which include the SMTP host, and click Create. An admin also picks the grant.
  4. Click Connect and sign in as Sign In describes. A client credentials connection fetches its token on create instead.

The panel shows the connection's status, account, and bound hosts:

  • Reconnect signs in again. For a client credentials connection it fetches a new token, and only an admin sees it.
  • Remove deletes the connection and its tokens. Email notifications that sign in through it then fail until a new one is connected.
  • The Notifications navigation item carries the badge while the connection needs a reconnect.

The process that sends notifications takes the token through the token broker and caches it, so a burst of notifications reads the token state once.

Its Connection needs attention and Connection restored events go through the global rules to every channel except email, since email would sign in through the broken connection. The message names the notification settings page as the place to reconnect.

The REST API serves it at /notification-settings/connection, with authorization paths that work as those of /connections/{name}. POST /notification-settings/connection/token fetches a client credentials token again and needs admin. dagu connection does not manage it yet.

Move From oauth Blocks ​

A mail account's or smtp's oauth block keeps working. While connections are on, the editor marks it deprecated, except a google_service_account block, which no connection replaces yet. Move From oauth Blocks moves its refresh token into a connection without a new consent.

Dagu is open source under the GNU General Public License v3.0.