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

added email notification text content support #934

Merged
merged 8 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/.build
/.release
/.tarballs
/.idea

!/doc/examples/simple.yml
!/circle.yml
Expand Down
2 changes: 2 additions & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
VSendResolved: false,
},
HTML: `{{ template "email.default.html" . }}`,
TEXT: ``,
}

// DefaultEmailSubject defines the default Subject header of an Email.
Expand Down Expand Up @@ -142,6 +143,7 @@ type EmailConfig struct {
AuthIdentity string `yaml:"auth_identity,omitempty" json:"auth_identity,omitempty"`
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`
HTML string `yaml:"html,omitempty" json:"html,omitempty"`
TEXT string `yaml:"text,omitempty" json:"text,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just Text as it's not an acronym.

Copy link
Contributor Author

@awaragi awaragi Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fabxc

I am not sure I understand your comment. I am not a Go dev (yet) so I need more details about what you are requesting as a change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the key here is that HTML is all caps since it it an initialism, but TEXT would be written as Text since it's just a word.

Does that make sense?
https://github.com/golang/go/wiki/CodeReviewComments#initialisms gives move info on this case

RequireTLS *bool `yaml:"require_tls,omitempty" json:"require_tls,omitempty"`

// Catches all undefined fields and must be empty after parsing.
Expand Down
25 changes: 23 additions & 2 deletions notify/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,18 @@ 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")
fmt.Fprintf(wc, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))
boundary := "=_NextPart_57bb889e057d551696ff582664f96f502da361c88cdc32a9e603f0af4b1c2b17" // SHA-256 of random string
fmt.Fprintf(wc, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n", 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
fmt.Fprintf(wc, "--%s\r\n", boundary)
fmt.Fprintf(wc, "Content-Type: text/html; charset=UTF-8\r\n")
body, err := n.tmpl.ExecuteHTMLString(n.conf.HTML, data)
if err != nil {
return false, fmt.Errorf("executing email html template: %s", err)
Expand All @@ -382,6 +386,23 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
return true, err
}

// Text template
// Last alternative based on recommendation in section 7.2.3
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
fmt.Fprintf(wc, "--%s\r\n", boundary)
fmt.Fprintf(wc, "Content-Type: text/plain; charset=UTF-8\r\n\r\n")
body, err = n.tmpl.ExecuteTextString(n.conf.TEXT, data)
if err != nil {
return false, fmt.Errorf("executing email text template: %s", err)
}
_, err = io.WriteString(wc, body)
if err != nil {
return true, err
}

// closing multi-part content
Copy link
Contributor

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.

fmt.Fprintf(wc, "--%s--\r\n", boundary)

return false, nil
}

Expand Down