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 4 commits
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
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"`
RequireTLS *bool `yaml:"require_tls,omitempty" json:"require_tls,omitempty"`

// Catches all undefined fields and must be empty after parsing.
Expand Down
33 changes: 29 additions & 4 deletions notify/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net"
"net/http"
"net/mail"
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Copy link
Contributor

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

Copy link
Contributor Author

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.



// 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"}})
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the documentation of CreatePart the io.Writer it returns should be used to write the output of the template into.
Same below when creating the part for the text template.

https://godoc.org/mime/multipart#Writer.CreatePart

The returned error should be checked as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
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.

multipartWriter.Close()

// writing multipart content
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above comment.

wc.Write(buffer.Bytes())

return false, nil
}

Expand Down