-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
987e81f
added email notification text content support (configuration text)
3dbe274
removed specific editor .gitignore entries
54ed71e
renamed TEXT to Text as it's not an acronym
3fdfc79
refactored to use standard go mime/multipart library for text and htm…
fbd8a54
Merge branch 'master-upstream'
c5c46f8
use multipart createPart returned writer
a039d6e
Merge branch 'master-upstream'
e4bba3b
removed unnecessary comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,53 @@ 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 | ||
w, err := multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/html; charset=UTF-8"}}) | ||
if err != nil { | ||
return false, fmt.Errorf("creating part for html template: %s", err) | ||
} | ||
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 = w.Write([]byte(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 | ||
w, err = multipartWriter.CreatePart(textproto.MIMEHeader{"Content-Type": {"text/plain; charset=UTF-8"}}) | ||
if err != nil { | ||
return false, fmt.Errorf("create part for text template: %s", err) | ||
} | ||
body, err = n.tmpl.ExecuteTextString(n.conf.Text, data) | ||
if err != nil { | ||
return false, fmt.Errorf("executing email text template: %s", err) | ||
} | ||
_, err = w.Write([]byte(body)) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
// closing multi-part content | ||
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 | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This comment probably isn't necessary, since the code says essentially the same thing.