Skip to content

Commit

Permalink
Send mails that correspond to RFC2045 with a base64 line limit of 76 …
Browse files Browse the repository at this point in the history
…characters.
  • Loading branch information
stffabi committed Feb 27, 2018
1 parent 28dff65 commit 09f047a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion notifications/email.go
Expand Up @@ -72,7 +72,12 @@ func (e *emailTypeNotifier) buildMessage(entries []*log.Entry) []byte {
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

encodedBody := base64.StdEncoding.EncodeToString([]byte(body))
//RFC 2045 base64 encoding demands line no longer than 76 characters.
for _, line := range SplitSubN(encodedBody, 76) {
message += "\r\n" + line
}

return []byte(message)
}
Expand Down
24 changes: 24 additions & 0 deletions notifications/util.go
@@ -0,0 +1,24 @@
package notifications

import "bytes"

// SplitSubN splits a string into a list of string with each having
// a maximum number of characters n
func SplitSubN(s string, n int) []string {
sub := ""
subs := []string{}

runes := bytes.Runes([]byte(s))
l := len(runes)
for i, r := range runes {
sub = sub + string(r)
if (i+1)%n == 0 {
subs = append(subs, sub)
sub = ""
} else if (i + 1) == l {
subs = append(subs, sub)
}
}

return subs
}

0 comments on commit 09f047a

Please sign in to comment.