Skip to content

Commit 4de93d1

Browse files
committed
feat: support imageWithSize
1 parent 14dff38 commit 4de93d1

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

internal/email/template.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func (tm *TemplateManager) LoadTemplate(name, filename string) error {
4343
"image": func(imageSrc string) string {
4444
return ""
4545
},
46+
"imageWithSize": func(imageSrc, width, height string) string {
47+
return ""
48+
},
4649
}).ParseFiles(fullPath)
4750
if err != nil {
4851
return fmt.Errorf("failed to parse template file %s: %v", fullPath, err)
@@ -84,7 +87,28 @@ func (tm *TemplateManager) RenderTemplate(name string, data interface{}) (string
8487
ContentType: fmt.Sprintf("image/%s", filepath.Ext(imageSrc)[1:]),
8588
Header: header,
8689
})
87-
return template.HTML(fmt.Sprintf("<img src='cid:%s' alt='%s'>", imageSrc, imageSrc))
90+
return template.HTML(fmt.Sprintf("<img src=\"cid:%s\" alt=\"%s\">", imageSrc, imageSrc))
91+
},
92+
"imageWithSize": func(imageSrc, width, height string) template.HTML {
93+
imagePath := filepath.Join(tm.imageDir, imageSrc)
94+
if _, err := os.Stat(imagePath); err != nil {
95+
return template.HTML(fmt.Sprintf("Image not found: %s", imageSrc))
96+
}
97+
content, err := os.ReadFile(imagePath)
98+
if err != nil {
99+
return template.HTML(fmt.Sprintf("Failed to read image: %s", imageSrc))
100+
}
101+
header := textproto.MIMEHeader{
102+
"Content-ID": {fmt.Sprintf("<%s>", imageSrc)},
103+
}
104+
attachments = append(attachments, email.Attachment{
105+
Filename: imageSrc,
106+
Content: content,
107+
HTMLRelated: true,
108+
ContentType: fmt.Sprintf("image/%s", filepath.Ext(imageSrc)[1:]),
109+
Header: header,
110+
})
111+
return template.HTML(fmt.Sprintf("<img src=\"cid:%s\" alt=\"%s\" width=\"%s\" height=\"%s\">", imageSrc, imageSrc, width, height))
88112
},
89113
})
90114
if err := tmpl.Execute(&buf, data); err != nil {

internal/web/handlers.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ func (h *APIHandler) PreviewTemplateHandler(w http.ResponseWriter, r *http.Reque
196196
if reqData.Content != "" {
197197
tpl, err = template.New(tmplName).Funcs(template.FuncMap{
198198
"image": func(imageSrc string) template.HTML {
199-
return template.HTML(fmt.Sprintf("<img src=\"%s\" alt=\"\" style=\"max-width: 100%%; height: auto;\">",
199+
return template.HTML(fmt.Sprintf("<img src=\"%s\" style=\"max-width: 100%%; height: auto;\">",
200200
html.EscapeString("/api/images/"+imageSrc)))
201201
},
202+
"imageWithSize": func(imageSrc, width, height string) template.HTML {
203+
return template.HTML(fmt.Sprintf("<img src=\"%s\" width=\"%s\" height=\"%s\">",
204+
html.EscapeString("/api/images/"+imageSrc), html.EscapeString(width), html.EscapeString(height)))
205+
},
202206
}).Parse(reqData.Content)
203207
if err != nil {
204208
http.Error(w, fmt.Sprintf("템플릿 파싱 실패: %v", err), http.StatusBadRequest)
@@ -309,12 +313,12 @@ func (h *APIHandler) EmailHandler(w http.ResponseWriter, r *http.Request) {
309313
log.Printf("프리메일러 생성 실패 (%s): %v", rec.Email, err)
310314
continue
311315
}
312-
html, err := premail.Transform()
316+
htm, err := premail.Transform()
313317
if err != nil {
314318
log.Printf("프리메일러 변환 실패 (%s): %v", rec.Email, err)
315319
}
316320
for i := 0; i < 10; i++ {
317-
if err := h.SMTPClient.SendEmail([]string{rec.Email}, reqData.Subject, html, attachments); err != nil {
321+
if err := h.SMTPClient.SendEmail([]string{rec.Email}, reqData.Subject, htm, attachments); err != nil {
318322
log.Printf("이메일 발송 실패 (%s): %v", rec.Email, err)
319323
time.Sleep(10 * time.Second)
320324
continue

0 commit comments

Comments
 (0)