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 6 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
39 changes: 35 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,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
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