From e53ac59c1db5323c4543e1154f9e17d85ecedd6e Mon Sep 17 00:00:00 2001 From: pepa65 Date: Mon, 11 Mar 2024 13:57:27 +0700 Subject: [PATCH 1/3] Parse Subject --- mail/mail.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mail/mail.go b/mail/mail.go index a66e234..09313f9 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -39,10 +39,15 @@ func NewMailer(username, password, host, port string, skipCertValidation bool) M func NewMessage(from, to *mail.Address, subject string, files []string, templatePath, htmlTemplatePath string, context interface{}) (*email.Email, error) { + parsedSubject, err := parseTemplate(subject, context) + if err != nil { + return nil, err + } + msg := &email.Email{ From: from.String(), To: []string{to.String()}, - Subject: subject, + Subject: parsedSubject, } for _, file := range files { From 0a6390ecbdb51f6bba73170e7a19100403f57d63 Mon Sep 17 00:00:00 2001 From: "gitlab.com/pepa65" Date: Mon, 11 Mar 2024 14:09:45 +0700 Subject: [PATCH 2/3] Update locations --- mail.go | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mail.go b/mail.go index 64ed09f..43e6a7d 100644 --- a/mail.go +++ b/mail.go @@ -3,7 +3,7 @@ package main import ( stdMail "net/mail" - "github.com/zachlatta/postman/mail" + "github.com/pepa65/postman/mail" "gopkg.in/jordan-wright/email.v2" ) diff --git a/main.go b/main.go index b6e830a..bee4e59 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "strings" "text/template" - "github.com/zachlatta/postman/mail" + "github.com/pepa65/postman/mail" "gopkg.in/jordan-wright/email.v2" ) From 5ef6a2da1aae9737a0241cb5bf5a3e6a16891459 Mon Sep 17 00:00:00 2001 From: "gitlab.com/pepa65" Date: Mon, 11 Mar 2024 14:30:15 +0700 Subject: [PATCH 3/3] Parse Subject --- go.mod | 5 +++++ go.sum | 2 ++ mail/mail.go | 14 ++++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..de5b529 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/pepa65/postman + +go 1.21.5 + +require gopkg.in/jordan-wright/email.v2 v2.0.0-20160301001728-a62870b0c368 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e4f4ba3 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +gopkg.in/jordan-wright/email.v2 v2.0.0-20160301001728-a62870b0c368 h1:btqYnSwwBSj/7nX1bPxyHvyXoIaRxS1MZg8v93NLpwY= +gopkg.in/jordan-wright/email.v2 v2.0.0-20160301001728-a62870b0c368/go.mod h1:6YiNdSV9gi2BIEp0u8QxxGZU5nxz4RUJvjZlA9dnOYc= diff --git a/mail/mail.go b/mail/mail.go index 09313f9..63943f8 100644 --- a/mail/mail.go +++ b/mail/mail.go @@ -39,7 +39,7 @@ func NewMailer(username, password, host, port string, skipCertValidation bool) M func NewMessage(from, to *mail.Address, subject string, files []string, templatePath, htmlTemplatePath string, context interface{}) (*email.Email, error) { - parsedSubject, err := parseTemplate(subject, context) + parsedSubject, err := parseText([]byte(subject), context) if err != nil { return nil, err } @@ -47,7 +47,7 @@ func NewMessage(from, to *mail.Address, subject string, files []string, template msg := &email.Email{ From: from.String(), To: []string{to.String()}, - Subject: parsedSubject, + Subject: string(parsedSubject), } for _, file := range files { @@ -83,16 +83,18 @@ func parseTemplate(templatePath string, context interface{}) ([]byte, error) { if err != nil { return nil, err } + return parseText(tmplBytes, context) +} +func parseText(tmplBytes []byte, context interface{}) ([]byte, error) { t := template.Must(template.New("emailBody").Parse(string(tmplBytes))) - - var doc bytes.Buffer - err = t.Execute(&doc, context) + var txt bytes.Buffer + err := t.Execute(&txt, context) if err != nil { return nil, err } - return doc.Bytes(), nil + return txt.Bytes(), nil } // Send sends an email Message.