Skip to content

Commit d7d227b

Browse files
committed
BUILTIN SendMail task now can read attachments data from parameters.
(pairs of name and base64-encoded content)
1 parent 1b54617 commit d7d227b

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

docs/components.rst

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Kind
132132
"toaddr": ["[email protected]"],
133133
"subject": "pg_timetable - No Reply",
134134
"attachment": ["/temp/attachments/Report.pdf","config.yaml"],
135+
"attachmentdata": [{"name": "File.txt", "base64data": "RmlsZSBDb250ZW50"}],
135136
"msgbody": "<h2>Hello User,</h2> <p>check some attachments!</p>",
136137
"contenttype": "text/html; charset=UTF-8"
137138
}'::jsonb

internal/tasks/mail.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
package tasks
22

33
import (
4+
"bytes"
45
"context"
56

67
gomail "github.com/ory/mail/v3"
78
)
89

10+
// Attachment file type
11+
// Has pair: name and content (base64-encoded) of attachment file
12+
type EmailAttachmentData struct {
13+
Name string `json:"name"`
14+
Base64Data []byte `json:"base64data"`
15+
}
16+
917
// EmailConn structure represents a connection to a mail server and mail fields
1018
type EmailConn struct {
11-
Username string `json:"username"`
12-
Password string `json:"password"`
13-
ServerHost string `json:"serverhost"`
14-
ServerPort int `json:"serverport"`
15-
SenderAddr string `json:"senderaddr"`
16-
CcAddr []string `json:"ccaddr"`
17-
BccAddr []string `json:"bccaddr"`
18-
ToAddr []string `json:"toaddr"`
19-
Subject string `json:"subject"`
20-
MsgBody string `json:"msgbody"`
21-
Attachments []string `json:"attachment"`
22-
ContentType string `json:"contenttype"`
19+
Username string `json:"username"`
20+
Password string `json:"password"`
21+
ServerHost string `json:"serverhost"`
22+
ServerPort int `json:"serverport"`
23+
SenderAddr string `json:"senderaddr"`
24+
CcAddr []string `json:"ccaddr"`
25+
BccAddr []string `json:"bccaddr"`
26+
ToAddr []string `json:"toaddr"`
27+
Subject string `json:"subject"`
28+
MsgBody string `json:"msgbody"`
29+
Attachments []string `json:"attachment"`
30+
AttachmentData []EmailAttachmentData `json:"attachmentdata"`
31+
ContentType string `json:"contenttype"`
2332
}
2433

2534
// Dialer implements DialAndSend function for mailer
@@ -65,6 +74,11 @@ func SendMail(ctx context.Context, conn EmailConn) error {
6574
for _, attachment := range conn.Attachments {
6675
mail.Attach(attachment)
6776
}
77+
78+
// Attach file with contents
79+
for _, attachmentData := range conn.AttachmentData {
80+
mail.AttachReader(attachmentData.Name, bytes.NewReader([]byte(attachmentData.Base64Data)))
81+
}
6882
// Send Mail
6983
dialer := NewDialer(conn.ServerHost, conn.ServerPort, conn.Username, conn.Password)
7084
return dialer.DialAndSend(ctx, mail)

internal/tasks/mail_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ func TestTaskSendMail(t *testing.T) {
3131
CcAddr: []string{"[email protected]"},
3232
BccAddr: []string{"[email protected]"},
3333
Attachments: []string{"mail.go"},
34+
AttachmentData: []EmailAttachmentData{{
35+
Name: "File1.txt",
36+
Base64Data: []byte("RmlsZSBDb250ZW50"), // "File Content" base64-encoded
37+
}},
3438
}), "Sending email with required json input should succeed")
3539
}

samples/Mail.sql

+13-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ BEGIN
1515
RETURNING task_id INTO v_mail_task_id;
1616

1717
-- Create the parameters for the SensMail task
18-
-- "username": The username used for authenticating on the mail server
19-
-- "password": The password used for authenticating on the mail server
20-
-- "serverhost": The IP address or hostname of the mail server
21-
-- "serverport": The port of the mail server
22-
-- "senderaddr": The email that will appear as the sender
23-
-- "ccaddr": String array of the recipients(Cc) email addresses
24-
-- "bccaddr": String array of the recipients(Bcc) email addresses
25-
-- "toaddr": String array of the recipients(To) email addresses
26-
-- "subject": Subject of the email
27-
-- "attachment": String array of the attachments
28-
-- "msgbody": The body of the email
18+
-- "username": The username used for authenticating on the mail server
19+
-- "password": The password used for authenticating on the mail server
20+
-- "serverhost": The IP address or hostname of the mail server
21+
-- "serverport": The port of the mail server
22+
-- "senderaddr": The email that will appear as the sender
23+
-- "ccaddr": String array of the recipients(Cc) email addresses
24+
-- "bccaddr": String array of the recipients(Bcc) email addresses
25+
-- "toaddr": String array of the recipients(To) email addresses
26+
-- "subject": Subject of the email
27+
-- "attachment": String array of the attachments (local file)
28+
-- "attachmentdata": Pairs of name and base64-encoded content
29+
-- "msgbody": The body of the email
2930

3031
INSERT INTO timetable.parameter (task_id, order_id, value)
3132
VALUES (v_mail_task_id, 1, '{
@@ -39,6 +40,7 @@ BEGIN
3940
"toaddr": ["[email protected]"],
4041
"subject": "pg_timetable - No Reply",
4142
"attachment": ["D:\\Go stuff\\Books\\Concurrency in Go.pdf","D:\\Go stuff\\Books\\The Way To Go.pdf"],
43+
"attachmentdata": [{"name": "File.txt", "base64data": "RmlsZSBDb250ZW50"}],
4244
"msgbody": "<b>Hello User,</b> <p>I got some Go books for you enjoy</p> <i>pg_timetable</i>!"
4345
}'::jsonb);
4446

0 commit comments

Comments
 (0)