Skip to content

Mail ​

mail.send sends one email from a step. It sends through the DAG's smtp server, or through a mail account named with mailbox.

For emails about run status without a step, use mail_on notifications. To read, reply to, or organize received email, see Mailbox.

Basic Usage ​

yaml
secrets:
  - name: SMTP_PASSWORD
    ref: mail/smtp-password

smtp:
  host: smtp.example.com
  port: 587
  username: alerts@example.com
  password: ${SMTP_PASSWORD}

steps:
  - id: notify
    action: mail.send
    with:
      from: alerts@example.com
      to: team@example.com
      subject: Workflow Completed
      message: The data processing workflow has completed.

The step succeeds once the SMTP server accepts the message. Its log lists the sender, recipients, subject, and body, then a success line. mail.send publishes no outputs.

Configure SMTP ​

The DAG-level smtp block serves every mail.send step and every email notification of the DAG. Set it in the base configuration to share it across DAGs.

FieldDescription
host, portSMTP server. Use the submission port, usually 587: the connection upgrades with STARTTLS when the server offers it, and implicit TLS (port 465) is not used.
username, passwordPassword sign-in. Read the password from secrets.
oauthOAuth 2.0 sign-in with provider: microsoft, google_service_account, or google_refresh, in place of password.
connectionExperimental. A connection whose token signs in, in place of password or oauth. See Use a Connection.

smtp fields resolve DAG values such as env, params, and secrets, not OS environment variables. Import an OS variable in env: first, such as - SMTP_USER: ${SMTP_USER}.

See SMTP Providers for Gmail, Microsoft 365, SendGrid, and AWS SES settings, and for the fields of each oauth provider.

Send to Several Recipients ​

to takes one address or a list:

yaml
steps:
  - id: daily_report
    action: mail.send
    with:
      from: reports@example.com
      to:
        - team@example.com
        - manager@example.com
      subject: Daily Report Ready
      message: The daily report has been generated.

Attach Files ​

yaml
steps:
  - id: build_report
    run: ./build-report.sh > ${context.paths.artifacts_dir}/report.csv

  - id: send_report
    depends: build_report
    action: mail.send
    with:
      from: reports@example.com
      to: management@example.com
      subject: Weekly Report
      message: The weekly report is attached.
      attachments:
        - ${context.paths.artifacts_dir}/report.csv
  • Use absolute paths. A relative path is read from the Dagu process's working directory, not the step's working_dir.
  • A missing or unreadable file fails the step before it connects. An empty file is attached as an empty file.
  • The attachment's content type comes from its file extension, or from its content when the extension implies none.

Write the Message ​

The body is sent as HTML. In plain text, line breaks become <br />. A body that starts with <!DOCTYPE html> is sent unchanged:

yaml
params:
  - ENVIRONMENT: production
  - VERSION: v1.2.3

steps:
  - id: announce
    action: mail.send
    with:
      from: deploy@example.com
      to: devops@example.com
      subject: Deployed ${params.VERSION} to ${params.ENVIRONMENT}
      message: |
        Deployment completed.
        Environment: ${params.ENVIRONMENT}
        Run: ${context.run.id}

Notify on Failure ​

A mail.send step in handler_on reports the run's result:

yaml
handler_on:
  failure:
    action: mail.send
    with:
      from: alerts@example.com
      to: oncall@example.com
      subject: "Failed: ${context.dag.name}"
      message: |
        Run ${context.run.id} failed.
        Log: ${context.paths.log_file}

steps:
  - id: main
    run: ./nightly-job.sh

To retry a failed send, add retry_policy to the step as for any other step.

Send Through a Mail Account ​

With mailbox, the step sends through that account in mail_accounts instead of smtp, and from defaults to the account's address. in_reply_to then makes the message a threaded reply to an email the account received; see Send and Reply.

yaml
secrets:
  - name: REPORTS_MAIL_PASSWORD
    ref: mail/reports

mail_accounts:
  reports@example.com:
    provider: google
    password: ${REPORTS_MAIL_PASSWORD}

steps:
  - id: send_report
    action: mail.send
    with:
      mailbox: reports@example.com
      to: team@example.com
      subject: Weekly Report
      message: The weekly report is ready.

Use a Connection ​

Experimental

Connections need experimental_connections: true in config.yaml. While they are off, a send through smtp.connection fails, and the run's other steps keep running.

smtp.connection signs in with a connection's token in place of password or oauth. It needs host and username. port defaults to 587, and port 465 uses TLS while any other uses STARTTLS. The token goes only to a host on the connection's bound hosts, with a verified certificate:

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

steps:
  - id: notify
    action: mail.send
    with:
      from: alerts@example.com
      to: team@example.com
      subject: Workflow Completed
      message: The workflow has completed.

See Use Connections for Mail.

Configuration ​

FieldRequiredDefaultDescription
toYes, unless in_reply_to supplies the senderRecipient address, or a list of addresses.
fromNoAccount address with mailboxSender address. Set it when sending through smtp.
subjectNoRe: <subject> with in_reply_toSubject line.
messageNoBody, sent as HTML. See Write the Message.
attachmentsNoList of file paths to attach. Use absolute paths.
mailboxNoAddress of a mail account in mail_accounts to send through instead of smtp.
in_reply_toNoOne email ID or email object to reply to. Requires mailbox.

Errors and Limits ​

  • No recipient fails the step with no valid recipients specified, before connecting.
  • A missing or unreadable attachment fails the step before connecting, naming the file.
  • in_reply_to without mailbox, or naming more than one email, fails the step.
  • smtp.oauth without username or beside password, and smtp.connection beside password or oauth, fail validation.
  • The SMTP session, from connecting to sending, times out after 30 seconds.
  • The body appears in the step log, so keep secrets out of message.

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