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:
| Where | Field | Used by |
|---|---|---|
| Mail accounts | mail_accounts.<address>.connection | mail.search, mail.organize, and mail.send with mailbox |
A DAG's smtp | smtp.connection | mail.send without mailbox, and the DAG's mail_on emails |
| Notification SMTP settings | The notification connection | Email 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
openidto show the account and the scope that returns a refresh token, such asoffline_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:portfor any other port, such asmail.example.com:143for 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:
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: trueconnectioncannot be combined withpasswordoroauth, even an empty one.- The account keeps its
provider,imap, andsmtpblocks, as Mail Accounts describes. Animapaccount needsimap.host, andsmtp.hostto send. Agoogleormicrosoftaccount takes its provider's servers. skip_tls_verifyandtls_cert_sha256cannot be combined withconnection, 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
googleaccount withconnectionreads mail over IMAP and SMTP, not the Gmail API that agoogleaccount withoauthuses. 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:
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.hostandusernameare required.passwordandoauthare not allowed.portdefaults to 587. The port picks the transport: TLS from the first byte on 465, and STARTTLS on any other port.hostmust be one of the connection's bound hosts.- A DAG's
smtpthat names a connection replaces a base config'ssmtpas a whole, as anoauthblock does. A base config'ssmtp.connectionresolves 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
smtpruns as before, even when its workspace lacks the connection. - A
pendingorneeds_reconnectconnection, or one whose app isinvalid, fails the step withreconnect required: connection "<name>": <reason>, without calling the provider. Steps that depend on it do not run and are markedaborted, unlesscontinue_onsays 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, andhttps://mail.google.com/. - Bound hosts:
imap.gmail.comandsmtp.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=offlinein its query, such ashttps://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent.
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: 24hprovider: 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
oauth2connection that belongs to no workspace. No DAG can name it. - Whoever may change the notification settings manages it:
developeror 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:
- On Notifications, open Email Delivery and pick Notification connection under SMTP authentication.
- Enter the SMTP host, port, username, and default sender, and save.
host,port, andusernameare required, and the host must be one of the connection's bound hosts. The port picks TLS or STARTTLS as a DAG'ssmtpdoes. - The Notification connection panel appears. Pick an
oauth2app, enter the scopes and the bound hosts, which include the SMTP host, and click Create. An admin also picks the grant. - 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.

