diff --git a/docs/source/examples.rst b/docs/source/examples.rst index 93ad4bfb4..612b1e594 100644 --- a/docs/source/examples.rst +++ b/docs/source/examples.rst @@ -230,6 +230,30 @@ Sending Email message: | Hello world +Sending Email with Attachments +------------------------------ + +.. code-block:: yaml + + smtp: + host: "smtp.foo.bar" + port: "587" + username: "" + password: "" + + steps: + - name: step1 + executor: + type: mail + config: + to: + from: + subject: "Sample Email" + message: | + Hello world + attachments: + - /tmp/email-attachment.txt + Customizing Signal Handling on Stop ----------------------------------- diff --git a/internal/digraph/executor/mail.go b/internal/digraph/executor/mail.go index 89bbb2f82..16ccb1ac7 100644 --- a/internal/digraph/executor/mail.go +++ b/internal/digraph/executor/mail.go @@ -25,6 +25,7 @@ type mailConfig struct { To string `mapstructure:"to"` Subject string `mapstructure:"subject"` Message string `mapstructure:"message"` + Attachments []string `json:"attachments,omitempty"` } func newMail(ctx context.Context, step digraph.Step) (Executor, error) { diff --git a/internal/digraph/executor/mail_test.go b/internal/digraph/executor/mail_test.go index 49edb0a66..8dd147f2d 100644 --- a/internal/digraph/executor/mail_test.go +++ b/internal/digraph/executor/mail_test.go @@ -3,6 +3,7 @@ package executor import ( "context" "os" + "path/filepath" "testing" "github.com/dagu-org/dagu/internal/digraph" @@ -12,9 +13,22 @@ import ( func TestMail(t *testing.T) { t.Parallel() + // Create temporary directory for test files + tmpDir, err := os.MkdirTemp("", "email-test") + if err != nil { + t.Fatalf("failed to create temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Create a test email attachment + attachFile := filepath.Join(tmpDir, "email.txt") + content := []byte("Test email") + os.Setenv("MAIL_SUBJECT", "Test Subject") + os.WriteFile(attachFile, content, 0644) t.Cleanup(func() { os.Unsetenv("MAIL_SUBJECT") + os.Remove(attachFile) }) t.Run("NewMail", func(t *testing.T) { @@ -27,10 +41,11 @@ func TestMail(t *testing.T) { step: digraph.Step{ ExecutorConfig: digraph.ExecutorConfig{ Config: map[string]any{ - "from": "test@example.com", - "to": "recipient@example.com", - "subject": "Test Subject", - "message": "Test Message", + "from": "test@example.com", + "to": "recipient@example.com", + "subject": "Test Subject", + "message": "Test Message", + "attachments": attachFile, }, }, }, @@ -40,10 +55,11 @@ func TestMail(t *testing.T) { step: digraph.Step{ ExecutorConfig: digraph.ExecutorConfig{ Config: map[string]any{ - "from": "test@example.com", - "to": "recipient@example.com", - "subject": "${MAIL_SUBJECT}", - "message": "Test Message", + "from": "test@example.com", + "to": "recipient@example.com", + "subject": "${MAIL_SUBJECT}", + "message": "Test Message", + "attachments": attachFile, }, }, }, @@ -68,6 +84,7 @@ func TestMail(t *testing.T) { assert.Equal(t, "recipient@example.com", mailExec.cfg.To) assert.Equal(t, "Test Subject", mailExec.cfg.Subject) assert.Equal(t, "Test Message", mailExec.cfg.Message) + assert.Equal(t, attachFile, mailExec.cfg.Attachments[0]) }) } })