apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: email-send
  labels:
    dagweave.io/catalog-item: email-send
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.1"
    dagweave.io/catalog-category: "Notifications"
spec:
  entrypoint: send
  templates:
    - name: send
      inputs:
        parameters:
          # Recipient address, e.g. "ops@example.com".
          - name: to
          # Subject line.
          - name: subject
          # Plain-text message body.
          - name: body
          # Envelope sender / From header, e.g. "alerts@example.com".
          - name: from
          # SMTP submission host, e.g. "smtp.example.com".
          - name: smtp-host
          # SMTP submission port. 587 (STARTTLS submission) is the usual default.
          - name: smtp-port
            default: "587"
          # SMTP AUTH username. Often the same as `from`; not itself secret.
          - name: smtp-username
          # Name/key of the Kubernetes Secret in the run namespace holding the SMTP
          # password. Defaults let a user leave them blank in the Inspector.
          - name: password-secret-name
            default: smtp-password
          - name: password-secret-key
            default: password
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # curlimages/curl:8.23.0 — curl speaks SMTP, so no custom image is needed.
        image: curlimages/curl@sha256:3fb7e13e5d179a845c97d78204f3f1064d1907037f26a3ebf91710571151fd93
        command: [sh, -c]
        # Values are passed as env vars (below), never interpolated into the shell,
        # so a subject/body containing quotes or shell metacharacters is safe. The
        # RFC 5322 message is assembled with printf "%s" (which does not re-parse its
        # arguments) and streamed to curl over an --ssl-reqd (STARTTLS) connection.
        args:
          - |
            set -eu
            {
              printf 'From: %s\r\n' "$MAIL_FROM"
              printf 'To: %s\r\n' "$MAIL_TO"
              printf 'Subject: %s\r\n' "$MAIL_SUBJECT"
              printf 'MIME-Version: 1.0\r\n'
              printf 'Content-Type: text/plain; charset=UTF-8\r\n'
              printf '\r\n'
              printf '%s\r\n' "$MAIL_BODY"
            } | curl -sSf --ssl-reqd \
                  --url "smtp://${SMTP_HOST}:${SMTP_PORT}" \
                  --user "${SMTP_USERNAME}:${SMTP_PASSWORD}" \
                  --mail-from "$MAIL_FROM" \
                  --mail-rcpt "$MAIL_TO" \
                  --upload-file -
        env:
          - name: MAIL_TO
            value: "{{inputs.parameters.to}}"
          - name: MAIL_SUBJECT
            value: "{{inputs.parameters.subject}}"
          - name: MAIL_BODY
            value: "{{inputs.parameters.body}}"
          - name: MAIL_FROM
            value: "{{inputs.parameters.from}}"
          - name: SMTP_HOST
            value: "{{inputs.parameters.smtp-host}}"
          - name: SMTP_PORT
            value: "{{inputs.parameters.smtp-port}}"
          - name: SMTP_USERNAME
            value: "{{inputs.parameters.smtp-username}}"
          - name: SMTP_PASSWORD
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.password-secret-name}}"
                key: "{{inputs.parameters.password-secret-key}}"
