Skip to content

Commit 1665cba

Browse files
committed
Option BASE64_EMBED_IMAGES (default false) in mail settings to inline image attachments + tests
1 parent f528df9 commit 1665cba

File tree

9 files changed

+196
-18
lines changed

9 files changed

+196
-18
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,9 @@ LEVEL = Info
17041704
;;
17051705
;; convert \r\n to \n for Sendmail
17061706
;SENDMAIL_CONVERT_CRLF = true
1707+
;;
1708+
;; convert links of attached images to inline images
1709+
;BASE64_EMBED_IMAGES = true
17071710

17081711
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17091712
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

models/fixtures/attachment.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,16 @@
153153
download_count: 0
154154
size: 0
155155
created_unix: 946684800
156+
157+
-
158+
id: 13
159+
uuid: 1b267670-1793-4cd0-abc1-449269b7cff9
160+
repo_id: 1
161+
issue_id: 2
162+
release_id: 0
163+
uploader_id: 2
164+
comment_id: 0
165+
name: gitea.png
166+
download_count: 0
167+
size: 1458
168+
created_unix: 946684800

modules/setting/mailer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Mailer struct {
2828
SendAsPlainText bool `ini:"SEND_AS_PLAIN_TEXT"`
2929
SubjectPrefix string `ini:"SUBJECT_PREFIX"`
3030
OverrideHeader map[string][]string `ini:"-"`
31+
Base64EmbedImages bool `ini:"BASE64_EMBED_IMAGES"`
3132

3233
// SMTP sender
3334
Protocol string `ini:"PROTOCOL"`
@@ -150,6 +151,7 @@ func loadMailerFrom(rootCfg ConfigProvider) {
150151
sec.Key("SENDMAIL_TIMEOUT").MustDuration(5 * time.Minute)
151152
sec.Key("SENDMAIL_CONVERT_CRLF").MustBool(true)
152153
sec.Key("FROM").MustString(sec.Key("USER").String())
154+
sec.Key("BASE64_EMBED_IMAGES").MustBool(false)
153155

154156
// Now map the values on to the MailService
155157
MailService = &Mailer{}

services/mailer/mail.go

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ package mailer
77
import (
88
"bytes"
99
"context"
10+
"encoding/base64"
1011
"fmt"
1112
"html/template"
13+
"io"
1214
"mime"
15+
"net/http"
1316
"regexp"
1417
"strconv"
1518
"strings"
@@ -26,11 +29,13 @@ import (
2629
"code.gitea.io/gitea/modules/markup"
2730
"code.gitea.io/gitea/modules/markup/markdown"
2831
"code.gitea.io/gitea/modules/setting"
32+
"code.gitea.io/gitea/modules/storage"
2933
"code.gitea.io/gitea/modules/timeutil"
3034
"code.gitea.io/gitea/modules/translation"
3135
incoming_payload "code.gitea.io/gitea/services/mailer/incoming/payload"
3236
"code.gitea.io/gitea/services/mailer/token"
3337

38+
"golang.org/x/net/html"
3439
"gopkg.in/gomail.v2"
3540
)
3641

@@ -195,7 +200,7 @@ func SendCollaboratorMail(u, doer *user_model.User, repo *repo_model.Repository)
195200
SendAsync(msg)
196201
}
197202

198-
func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipients []*user_model.User, fromMention bool, info string) ([]*Message, error) {
203+
func composeIssueCommentMessages(ctx *MailCommentContext, lang string, recipients []*user_model.User, fromMention bool, info string) ([]*Message, error) {
199204
var (
200205
subject string
201206
link string
@@ -232,6 +237,15 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
232237
return nil, err
233238
}
234239

240+
if setting.MailService.Base64EmbedImages {
241+
bodyStr := string(body)
242+
bodyStr, err = Base64InlineImages(bodyStr, ctx)
243+
if err != nil {
244+
return nil, err
245+
}
246+
body = template.HTML(bodyStr)
247+
}
248+
235249
actType, actName, tplName := actionToTemplate(ctx.Issue, ctx.ActionType, commentType, reviewType)
236250

237251
if actName != "new" {
@@ -363,6 +377,81 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
363377
return msgs, nil
364378
}
365379

380+
func Base64InlineImages(body string, ctx *MailCommentContext) (string, error) {
381+
doc, err := html.Parse(strings.NewReader(body))
382+
if err != nil {
383+
log.Error("Failed to parse HTML body: %v", err)
384+
return "", err
385+
}
386+
387+
var processNode func(*html.Node)
388+
processNode = func(n *html.Node) {
389+
if n.Type == html.ElementNode {
390+
if n.Data == "img" {
391+
for i, attr := range n.Attr {
392+
if attr.Key == "src" {
393+
attachmentPath := attr.Val
394+
dataURI, err := AttachmentSrcToBase64DataURI(attachmentPath, ctx)
395+
if err != nil {
396+
log.Trace("attachmentSrcToDataURI not possible: %v", err) // Not an error, just skip. This is probably an image from outside the gitea instance.
397+
continue
398+
}
399+
log.Trace("Old value of src attribute: %s, new value (first 100 characters): %s", attr.Val, dataURI[:100])
400+
n.Attr[i].Val = dataURI
401+
}
402+
}
403+
}
404+
}
405+
406+
for c := n.FirstChild; c != nil; c = c.NextSibling {
407+
processNode(c)
408+
}
409+
}
410+
411+
processNode(doc)
412+
413+
var buf bytes.Buffer
414+
err = html.Render(&buf, doc)
415+
if err != nil {
416+
log.Error("Failed to render modified HTML: %v", err)
417+
return "", err
418+
}
419+
return buf.String(), nil
420+
}
421+
422+
func AttachmentSrcToBase64DataURI(attachmentPath string, ctx *MailCommentContext) (string, error) {
423+
if !strings.HasPrefix(attachmentPath, setting.AppURL) { // external image
424+
return "", fmt.Errorf("external image")
425+
}
426+
parts := strings.Split(attachmentPath, "/attachments/")
427+
if len(parts) <= 1 {
428+
return "", fmt.Errorf("invalid attachment path: %s", attachmentPath)
429+
}
430+
431+
attachmentUUID := parts[len(parts)-1]
432+
attachment, err := repo_model.GetAttachmentByUUID(ctx, attachmentUUID)
433+
if err != nil {
434+
return "", err
435+
}
436+
437+
fr, err := storage.Attachments.Open(attachment.RelativePath())
438+
if err != nil {
439+
return "", err
440+
}
441+
defer fr.Close()
442+
443+
content, err := io.ReadAll(fr)
444+
if err != nil {
445+
return "", err
446+
}
447+
448+
mimeType := http.DetectContentType(content)
449+
encoded := base64.StdEncoding.EncodeToString(content)
450+
dataURI := fmt.Sprintf("data:%s;base64,%s", mimeType, encoded)
451+
452+
return dataURI, nil
453+
}
454+
366455
func generateMessageIDForIssue(issue *issues_model.Issue, comment *issues_model.Comment, actionType activities_model.ActionType) string {
367456
var path string
368457
if issue.IsPull {
@@ -394,7 +483,7 @@ func generateMessageIDForRelease(release *repo_model.Release) string {
394483
return fmt.Sprintf("<%s/releases/%d@%s>", release.Repo.FullName(), release.ID, setting.Domain)
395484
}
396485

397-
func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient *user_model.User) map[string]string {
486+
func generateAdditionalHeaders(ctx *MailCommentContext, reason string, recipient *user_model.User) map[string]string {
398487
repo := ctx.Issue.Repo
399488

400489
return map[string]string{
@@ -458,7 +547,7 @@ func SendIssueAssignedMail(ctx context.Context, issue *issues_model.Issue, doer
458547
}
459548

460549
for lang, tos := range langMap {
461-
msgs, err := composeIssueCommentMessages(&mailCommentContext{
550+
msgs, err := composeIssueCommentMessages(&MailCommentContext{
462551
Context: ctx,
463552
Issue: issue,
464553
Doer: doer,

services/mailer/mail_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func MailParticipantsComment(ctx context.Context, c *issues_model.Comment, opTyp
2626
content = ""
2727
}
2828
if err := mailIssueCommentToParticipants(
29-
&mailCommentContext{
29+
&MailCommentContext{
3030
Context: ctx,
3131
Issue: issue,
3232
Doer: c.Poster,
@@ -49,7 +49,7 @@ func MailMentionsComment(ctx context.Context, pr *issues_model.PullRequest, c *i
4949
visited := make(container.Set[int64], len(mentions)+1)
5050
visited.Add(c.Poster.ID)
5151
if err = mailIssueCommentBatch(
52-
&mailCommentContext{
52+
&MailCommentContext{
5353
Context: ctx,
5454
Issue: pr.Issue,
5555
Doer: c.Poster,

services/mailer/mail_issue.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func fallbackMailSubject(issue *issues_model.Issue) string {
2222
return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
2323
}
2424

25-
type mailCommentContext struct {
25+
type MailCommentContext struct {
2626
context.Context
2727
Issue *issues_model.Issue
2828
Doer *user_model.User
@@ -41,7 +41,7 @@ const (
4141
// This function sends two list of emails:
4242
// 1. Repository watchers (except for WIP pull requests) and users who are participated in comments.
4343
// 2. Users who are not in 1. but get mentioned in current issue/comment.
44-
func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_model.User) error {
44+
func mailIssueCommentToParticipants(ctx *MailCommentContext, mentions []*user_model.User) error {
4545
// Required by the mail composer; make sure to load these before calling the async function
4646
if err := ctx.Issue.LoadRepo(ctx); err != nil {
4747
return fmt.Errorf("LoadRepo: %w", err)
@@ -120,7 +120,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo
120120
return nil
121121
}
122122

123-
func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, visited container.Set[int64], fromMention bool) error {
123+
func mailIssueCommentBatch(ctx *MailCommentContext, users []*user_model.User, visited container.Set[int64], fromMention bool) error {
124124
checkUnit := unit.TypeIssues
125125
if ctx.Issue.IsPull {
126126
checkUnit = unit.TypePullRequests
@@ -186,7 +186,7 @@ func MailParticipants(ctx context.Context, issue *issues_model.Issue, doer *user
186186
}
187187
forceDoerNotification := opType == activities_model.ActionAutoMergePullRequest
188188
if err := mailIssueCommentToParticipants(
189-
&mailCommentContext{
189+
&MailCommentContext{
190190
Context: ctx,
191191
Issue: issue,
192192
Doer: doer,

services/mailer/mail_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestComposeIssueCommentMessage(t *testing.T) {
8383
bodyTemplates = template.Must(template.New("issue/comment").Parse(bodyTpl))
8484

8585
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
86-
msgs, err := composeIssueCommentMessages(&mailCommentContext{
86+
msgs, err := composeIssueCommentMessages(&MailCommentContext{
8787
Context: context.TODO(), // TODO: use a correct context
8888
Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue,
8989
Content: fmt.Sprintf("test @%s %s#%d body", doer.Name, issue.Repo.FullName(), issue.Index),
@@ -129,7 +129,7 @@ func TestComposeIssueMessage(t *testing.T) {
129129
bodyTemplates = template.Must(template.New("issue/new").Parse(bodyTpl))
130130

131131
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}, {Name: "Test2", Email: "test2@gitea.com"}}
132-
msgs, err := composeIssueCommentMessages(&mailCommentContext{
132+
msgs, err := composeIssueCommentMessages(&MailCommentContext{
133133
Context: context.TODO(), // TODO: use a correct context
134134
Issue: issue, Doer: doer, ActionType: activities_model.ActionCreateIssue,
135135
Content: "test body",
@@ -176,14 +176,14 @@ func TestTemplateSelection(t *testing.T) {
176176
assert.Contains(t, wholemsg, expBody)
177177
}
178178

179-
msg := testComposeIssueCommentMessage(t, &mailCommentContext{
179+
msg := testComposeIssueCommentMessage(t, &MailCommentContext{
180180
Context: context.TODO(), // TODO: use a correct context
181181
Issue: issue, Doer: doer, ActionType: activities_model.ActionCreateIssue,
182182
Content: "test body",
183183
}, recipients, false, "TestTemplateSelection")
184184
expect(t, msg, "issue/new/subject", "issue/new/body")
185185

186-
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
186+
msg = testComposeIssueCommentMessage(t, &MailCommentContext{
187187
Context: context.TODO(), // TODO: use a correct context
188188
Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue,
189189
Content: "test body", Comment: comment,
@@ -192,14 +192,14 @@ func TestTemplateSelection(t *testing.T) {
192192

193193
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2, Repo: repo, Poster: doer})
194194
comment = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 4, Issue: pull})
195-
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
195+
msg = testComposeIssueCommentMessage(t, &MailCommentContext{
196196
Context: context.TODO(), // TODO: use a correct context
197197
Issue: pull, Doer: doer, ActionType: activities_model.ActionCommentPull,
198198
Content: "test body", Comment: comment,
199199
}, recipients, false, "TestTemplateSelection")
200200
expect(t, msg, "pull/comment/subject", "pull/comment/body")
201201

202-
msg = testComposeIssueCommentMessage(t, &mailCommentContext{
202+
msg = testComposeIssueCommentMessage(t, &MailCommentContext{
203203
Context: context.TODO(), // TODO: use a correct context
204204
Issue: issue, Doer: doer, ActionType: activities_model.ActionCloseIssue,
205205
Content: "test body", Comment: comment,
@@ -218,7 +218,7 @@ func TestTemplateServices(t *testing.T) {
218218
bodyTemplates = template.Must(template.New("issue/default").Parse(tplBody))
219219

220220
recipients := []*user_model.User{{Name: "Test", Email: "test@gitea.com"}}
221-
msg := testComposeIssueCommentMessage(t, &mailCommentContext{
221+
msg := testComposeIssueCommentMessage(t, &MailCommentContext{
222222
Context: context.TODO(), // TODO: use a correct context
223223
Issue: issue, Doer: doer, ActionType: actionType,
224224
Content: "test body", Comment: comment,
@@ -252,7 +252,7 @@ func TestTemplateServices(t *testing.T) {
252252
"//Re: //")
253253
}
254254

255-
func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recipients []*user_model.User, fromMention bool, info string) *Message {
255+
func testComposeIssueCommentMessage(t *testing.T, ctx *MailCommentContext, recipients []*user_model.User, fromMention bool, info string) *Message {
256256
msgs, err := composeIssueCommentMessages(ctx, "en-US", recipients, fromMention, info)
257257
assert.NoError(t, err)
258258
assert.Len(t, msgs, 1)
@@ -262,7 +262,7 @@ func testComposeIssueCommentMessage(t *testing.T, ctx *mailCommentContext, recip
262262
func TestGenerateAdditionalHeaders(t *testing.T) {
263263
doer, _, issue, _ := prepareMailerTest(t)
264264

265-
ctx := &mailCommentContext{Context: context.TODO() /* TODO: use a correct context */, Issue: issue, Doer: doer}
265+
ctx := &MailCommentContext{Context: context.TODO() /* TODO: use a correct context */, Issue: issue, Doer: doer}
266266
recipient := &user_model.User{Name: "test", Email: "test@gitea.com"}
267267

268268
headers := generateAdditionalHeaders(ctx, "dummy-reason", recipient)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
issues_model "code.gitea.io/gitea/models/issues"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/models/unittest"
13+
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/setting"
15+
mail "code.gitea.io/gitea/services/mailer"
16+
"code.gitea.io/gitea/tests"
17+
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func TestEmailEmbedBase64Images(t *testing.T) {
22+
defer tests.PrepareTestEnv(t)()
23+
tests.PrepareAttachmentsStorage(t)
24+
25+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
26+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1, Owner: user})
27+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2, Repo: repo, Poster: user})
28+
29+
attachment := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: 13, IssueID: issue.ID, RepoID: repo.ID})
30+
ctx0 := context.Background()
31+
32+
ctx := &mail.MailCommentContext{Context: ctx0 /* TODO: use a correct context */, Issue: issue, Doer: user}
33+
34+
img1ExternalURL := "https://via.placeholder.com/10"
35+
img1ExternalImg := "<img src=\"" + img1ExternalURL + "\"/>"
36+
37+
img2InternalURL := setting.AppURL + repo.Owner.Name + "/" + repo.Name + "/attachments/" + attachment.UUID
38+
img2InternalImg := "<img src=\"" + img2InternalURL + "\"/>"
39+
img2InternalBase64 := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAxCAYAAABNuS5SAAAAAXNSR0IArs4c6QAABWxJREFUaEPVmm1sU1UYx/9n3RsuxmS+ECdC71YYbCjohmMDNxyJONBNxaIo29qNlZqYbTFhi0FkmZpgogH0w1g7t46ZmEA0KIl+MQsvwQSM8QOJSGDtFO0XFTQE0tG11/Rqa9fel3Pf7+6H7cN9zjnP8zv/53nOvbcElFe7n/mOsKimNAfAfpWTm3NgtCP4Df0Ya1q2DpXdl2OL7yYEbrC4O+FlwBMiif/cH6Grc4RZEotj2lphsddAyB9gSbSlqvvvZx/tqQCQ5x4pBVgUGelrAqIgQJePYY10Rslam1d7w841fSWJsW5/qZIpVI0hBM5sgCyIy8/EVc1s0OCmh3eFt9X0mwYwK4V3+h2LZtnYVYPiV73MUw91hV9c+4Z1AM6HtE2nvmllR/il2jetAZAGHgu8M+4J7U0PgmacaqkJTPDkSvdv22v3PmBWDUylsMtnPwiQHqFAky2b776ZADdWtv26o25gkQUACndcMXgJx80E+MSKV35pW//2YssClIJnNsCG5dt/dj3+7hJTAbp8zAyAfL70tDrA+vJt0+76/XazAfIemGngma3A9eXOYGf9e9wJ2oyDNNdEhGoYPUB7P0D269VpxeatW/r8VNeG98vmNUAzVVhb1nLF03jAYRZAkhurVK1AowDeteBeRGZvYSZ6MyXKmtJnLns3HlpqFkDuZYLaFDYC4FhXMCuTkzUvec+MGjgvAPLBS9IcmuzB+akTZpRfbk3tAPqZU2BRr0ckYgDTVWg5BSYJ00LR44lEDN7E2X2Y/HECvo6LyLMVpNw0EOTtgCdUQDzDJXfcJgX/V+Y0YrRHGb3qIK36+DZZb5BzXukLqYeNo2HcGzpNo0KtFSgELx3MBy+fRXHR/aLu6QWSCqCZacwPkIXbz52bU5eYStPtOkcciLPavWjPBHgSQIPQVtKmslYqFIJy5tIxjJ7un+NmY0UbWtcN0CQJZ6OVIrO+ykkFTwNRag7aKGlqX+ZcK0rq0LflE6ol1EJMZ5H6qNQ+zNwiBAvEPDACohi8q39ewlufN6F13SAaK3ZwroZ+v4DB4y2yUloXgPSdlBwKeIK9QqDVqpBGfXw2gTN7cOqnTzm3pOqibgDpIf6Lj0+RagCKBX4jcg3dE9XYvMoL52N9vPt3I3Id3RNVogC1hJdwIuu7sPMobEV/MbNUxWSu0UzAEyqUuwm0HTXz2VfIv4Rd76ZRrFq8gddEd4CJVZs+dBQsLIxFFEBUPIQm7WrKmuFtPCi4hhRktfD4sk70tzFq0lEuSaW1L30dvQHylSxRgGrSUQ5AmqcOx8Iq7Gk+Jjjtzo+XIRafFax/eqiPtwbq1V3FgNIAlOqueqtPqGlKKpAvcK1TWy3Aw5O9ODf1JQa3fo0Hi8uzXNZLfbIUKKzM0jUAe15OymbaqgVolvo0ASgGTo5ShSDG2RhyiI3qSCL26l/NBos9gSlKYVpn5ABMzil1nOHrumJj9Uxf3RWopovTgMyEkzlGC3gkl1071jF9Tkg0uipQDUAaRb52pAo3Z65zpmakryEK1AKi1BGGTx1aqO/OiK3wo+4rid8OCV66K5ADOGZfjSj5gbZ2itkNuS6gME/6x/haAKR5fWcIQK1UmA5233MnYL+nkqo7K9k4GniGpXAyACVdWSr4rdW78fQjr6bMvr18HP6Tr0sNk7xvSYB6KFGShAIDWniGKzCxYLuPaSbAFwriMmSIHHimAPxPhTEAOYYQkbGIPRyyDQxA1rdPw5pIZhwuH2MpiCzIC+Oe4GcyeHOmpgHklOhnjoKFU67TWtvns8X5vl3fR5XMaypAPbszLQy5NS9zXksA5NQ4vGw5SPQibeBq7dSCS67/D+Q9UQwzW88cAAAAAElFTkSuQmCC"
40+
img2InternalBase64Img := "<img src=\"" + img2InternalBase64 + "\"/>"
41+
42+
// 1st Test: convert internal image to base64
43+
t.Run("replaceSpecifiedBase64ImagesInternal", func(t *testing.T) {
44+
defer tests.PrintCurrentTest(t)()
45+
46+
resultImg1Internal, err := mail.AttachmentSrcToBase64DataURI(img2InternalURL, ctx)
47+
assert.NoError(t, err)
48+
assert.Equal(t, img2InternalBase64, resultImg1Internal) // replace cause internal image
49+
})
50+
51+
// 2nd Test: convert external image to base64 -> abort cause external image
52+
t.Run("replaceSpecifiedBase64ImagesExternal", func(t *testing.T) {
53+
defer tests.PrintCurrentTest(t)()
54+
55+
resultImg1External, err := mail.AttachmentSrcToBase64DataURI(img1ExternalURL, ctx)
56+
assert.Error(t, err)
57+
assert.Equal(t, "", resultImg1External) // don't replace cause external image
58+
})
59+
60+
// 3rd Test: generate email body with 1 internal and 1 external image, expect the result to have the internal image replaced with base64 data and the external not replaced
61+
t.Run("generateEmailBody", func(t *testing.T) {
62+
defer tests.PrintCurrentTest(t)()
63+
64+
mailBody := "<html><head></head><body><p>Test1</p>" + img1ExternalImg + "<p>Test2</p>" + img2InternalImg + "<p>Test3</p></body></html>"
65+
expectedMailBody := "<html><head></head><body><p>Test1</p>" + img1ExternalImg + "<p>Test2</p>" + img2InternalBase64Img + "<p>Test3</p></body></html>"
66+
resultMailBody, err := mail.Base64InlineImages(mailBody, ctx)
67+
68+
assert.NoError(t, err)
69+
assert.Equal(t, expectedMailBody, resultMailBody)
70+
})
71+
}
Binary file not shown.

0 commit comments

Comments
 (0)