-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
added email notification text content support #934
Changes from 4 commits
987e81f
3dbe274
54ed71e
3fdfc79
fbd8a54
c5c46f8
a039d6e
e4bba3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,9 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"mime" | ||
"mime/multipart" | ||
"net" | ||
"net/http" | ||
"net/mail" | ||
|
@@ -40,6 +40,7 @@ import ( | |
"github.com/prometheus/alertmanager/config" | ||
"github.com/prometheus/alertmanager/template" | ||
"github.com/prometheus/alertmanager/types" | ||
"net/textproto" | ||
) | ||
|
||
type notifierConfig interface { | ||
|
@@ -365,23 +366,47 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) { | |
fmt.Fprintf(wc, "%s: %s\r\n", header, mime.QEncoding.Encode("utf-8", value)) | ||
} | ||
|
||
fmt.Fprintf(wc, "Content-Type: text/html; charset=UTF-8\r\n") | ||
buffer := &bytes.Buffer{} | ||
multipartWriter := multipart.NewWriter(buffer) | ||
|
||
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()) | ||
|
||
|
||
// TODO: Add some useful headers here, such as URL of the alertmanager | ||
// and active/resolved. | ||
fmt.Fprintf(wc, "\r\n") | ||
|
||
// TODO(fabxc): do a multipart write that considers the plain template. | ||
// Html template | ||
multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/html; charset=UTF-8"}}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the documentation of https://godoc.org/mime/multipart#Writer.CreatePart The returned error should be checked as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabxc implemented as suggested and added basic error handling. |
||
body, err := n.tmpl.ExecuteHTMLString(n.conf.HTML, data) | ||
if err != nil { | ||
return false, fmt.Errorf("executing email html template: %s", err) | ||
} | ||
_, err = io.WriteString(wc, body) | ||
_, err = buffer.WriteString(body) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
// Text template | ||
// Last alternative based on recommendation in section 7.2.3 of w3 rfc1341 protocol | ||
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html | ||
multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/plain; charset=UTF-8"}}) | ||
body, err = n.tmpl.ExecuteTextString(n.conf.Text, data) | ||
if err != nil { | ||
return false, fmt.Errorf("executing email text template: %s", err) | ||
} | ||
_, err = buffer.WriteString(body) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
// closing multi-part content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment probably isn't necessary, since the code says essentially the same thing. |
||
multipartWriter.Close() | ||
|
||
// writing multipart content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above comment. |
||
wc.Write(buffer.Bytes()) | ||
|
||
return false, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this can be replaced with this convenience function: https://godoc.org/mime/multipart#Writer.FormDataContentType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Fabian @fabxc, your initial thought is correct however for multipart email content, you need the multipart/alternative instead of multipart/form-data which is what the FormDataContentType return. I did however notice an additional quotes that were problematic that I took out from around the boundary string.