Fix multipart email implementation: (#1009)

* Don't send parts with empty templates.
 * Add a MIME-Version: 1.0 header.
 * Place text/html part last, as parts are supposed to be in increasing preference order.
This commit is contained in:
Alin Sinpalean 2017-09-29 11:22:38 +02:00 committed by stuart nelson
parent 837cf5c6fe
commit 4931c9206e
1 changed files with 31 additions and 27 deletions

View File

@ -371,43 +371,47 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
fmt.Fprintf(wc, "Date: %s\r\n", time.Now().Format(time.RFC1123Z)) fmt.Fprintf(wc, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))
fmt.Fprintf(wc, "Content-Type: multipart/alternative; boundary=%s\r\n", multipartWriter.Boundary()) fmt.Fprintf(wc, "Content-Type: multipart/alternative; boundary=%s\r\n", multipartWriter.Boundary())
fmt.Fprintf(wc, "MIME-Version: 1.0\r\n")
// TODO: Add some useful headers here, such as URL of the alertmanager // TODO: Add some useful headers here, such as URL of the alertmanager
// and active/resolved. // and active/resolved.
fmt.Fprintf(wc, "\r\n") fmt.Fprintf(wc, "\r\n")
// Html template if len(n.conf.Text) > 0 {
w, err := multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/html; charset=UTF-8"}}) // Text template
if err != nil { w, err := multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/plain; charset=UTF-8"}})
return false, fmt.Errorf("creating part for html template: %s", err) if err != nil {
} return false, fmt.Errorf("creating part for text template: %s", err)
body, err := n.tmpl.ExecuteHTMLString(n.conf.HTML, data) }
if err != nil { body, err := n.tmpl.ExecuteTextString(n.conf.Text, data)
return false, fmt.Errorf("executing email html template: %s", err) if err != nil {
} return false, fmt.Errorf("executing email text template: %s", err)
_, err = w.Write([]byte(body)) }
if err != nil { _, err = w.Write([]byte(body))
return true, err if err != nil {
return true, err
}
} }
// Text template if len(n.conf.HTML) > 0 {
// Last alternative based on recommendation in section 7.2.3 of w3 rfc1341 protocol // Html template
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html // Preferred alternative placed last per section 5.1.4 of RFC 2046
w, err = multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/plain; charset=UTF-8"}}) // https://www.ietf.org/rfc/rfc2046.txt
if err != nil { w, err := multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/html; charset=UTF-8"}})
return false, fmt.Errorf("create part for text template: %s", err) if err != nil {
} return false, fmt.Errorf("creating part for html template: %s", err)
body, err = n.tmpl.ExecuteTextString(n.conf.Text, data) }
if err != nil { body, err := n.tmpl.ExecuteHTMLString(n.conf.HTML, data)
return false, fmt.Errorf("executing email text template: %s", err) if err != nil {
} return false, fmt.Errorf("executing email html template: %s", err)
_, err = w.Write([]byte(body)) }
if err != nil { _, err = w.Write([]byte(body))
return true, err if err != nil {
return true, err
}
} }
multipartWriter.Close() multipartWriter.Close()
wc.Write(buffer.Bytes()) wc.Write(buffer.Bytes())
return false, nil return false, nil