Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to encode header entries #108

Merged
merged 2 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions processor/emailer/emailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func (e *Emailer) loadTemplate() (*template.Template, error) {
//
funcMap := template.FuncMap{
"env": env,
"quoteprintable": e.toQuotedPrintable,
"quoteprintable": toQuotedPrintable,
"split": split,
"encodeHeader": encodeHeader,
}

tmpl := template.Must(template.New("email.tmpl").Funcs(funcMap).Parse(string(content)))
Expand All @@ -115,7 +116,7 @@ func (e *Emailer) loadTemplate() (*template.Template, error) {
// body.
//
// NOTE: We use this function both directly, and from within our template.
func (e *Emailer) toQuotedPrintable(s string) (string, error) {
func toQuotedPrintable(s string) (string, error) {
var ac bytes.Buffer
w := quotedprintable.NewWriter(&ac)
_, err := w.Write([]byte(s))
Expand All @@ -129,6 +130,18 @@ func (e *Emailer) toQuotedPrintable(s string) (string, error) {
return ac.String(), nil
}

// Encode email header entries to comply with the 7bit ASCII restriction
// of RFC 5322 according to RFC 2047.
//
// We use quotedprintable encoding only if necessary.
func encodeHeader(s string) string {
se, err := toQuotedPrintable(s)
if (err != nil) || (len(se) == len(s)) {
return s
}
return "=?utf-8?Q?" + strings.Replace(strings.Replace(se, "?", "=3F", -1), " ", "=20", -1) + "?="
}

// Sendmail is a simple function that emails the given address.
//
// We send a MIME message with both a plain-text and a HTML-version of the
Expand Down Expand Up @@ -186,11 +199,11 @@ func (e *Emailer) Sendmail(addresses []string, textstr string, htmlstr string) e

// The real meat of the mail is the text & HTML
// parts. They need to be encoded, unconditionally.
x.Text, err = e.toQuotedPrintable(textstr)
x.Text, err = toQuotedPrintable(textstr)
if err != nil {
return err
}
x.HTML, err = e.toQuotedPrintable(html.UnescapeString(htmlstr))
x.HTML, err = toQuotedPrintable(html.UnescapeString(htmlstr))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion template/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

{{env "USER"}} -> Return the given environmental variable
{{quoteprintable .Link}} -> Quote the specified field.
{{encodeHeader .Subject}} -> Quote the specified field to be used in mail header.
{{split "STRING:HERE" ":"}} -> Split a string into an array by deliminator

This comment will be stripped from the generated email.
Expand All @@ -27,7 +28,7 @@
Content-Type: multipart/mixed; boundary=21ee3da964c7bf70def62adb9ee1a061747003c026e363e47231258c48f1
From: {{.From}}
To: {{.To}}
Subject: [rss2email] {{if .Tag}}{{.Tag}} {{end}}{{.Subject}}
Subject: [rss2email] {{if .Tag}}{{encodeHeader .Tag}} {{end}}{{encodeHeader .Subject}}
X-RSS-Link: {{.Link}}
X-RSS-Feed: {{.Feed}}
{{- if .Tag}}
Expand Down
2 changes: 1 addition & 1 deletion template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func TestTemplate(t *testing.T) {

// content and expected length
content := EmailTemplate()
length := 2529
length := 2645

if len(content) != length {
t.Fatalf("unexpected template size %d != %d", length, len(content))
Expand Down