|
1 | 1 | package tasks
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 |
|
6 | 7 | gomail "github.com/ory/mail/v3"
|
7 | 8 | )
|
8 | 9 |
|
| 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 | + |
9 | 17 | // EmailConn structure represents a connection to a mail server and mail fields
|
10 | 18 | 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"` |
23 | 32 | }
|
24 | 33 |
|
25 | 34 | // Dialer implements DialAndSend function for mailer
|
@@ -65,6 +74,11 @@ func SendMail(ctx context.Context, conn EmailConn) error {
|
65 | 74 | for _, attachment := range conn.Attachments {
|
66 | 75 | mail.Attach(attachment)
|
67 | 76 | }
|
| 77 | + |
| 78 | + // Attach file with contents |
| 79 | + for _, attachmentData := range conn.AttachmentData { |
| 80 | + mail.AttachReader(attachmentData.Name, bytes.NewReader([]byte(attachmentData.Base64Data))) |
| 81 | + } |
68 | 82 | // Send Mail
|
69 | 83 | dialer := NewDialer(conn.ServerHost, conn.ServerPort, conn.Username, conn.Password)
|
70 | 84 | return dialer.DialAndSend(ctx, mail)
|
|
0 commit comments