Skip to content

Commit

Permalink
added email notification text content support (prometheus#934)
Browse files Browse the repository at this point in the history
* added email notification text content support (configuration text)
added default email notification text content default value empty
converted email notification from html to multipart/alternatives with support to both html (first) and text (last)
ignore intellij IDE .idea working folder

* removed specific editor .gitignore entries

* renamed TEXT to Text as it's not an acronym
added TODO note to refactor multipart code to use standard go library

* refactored to use standard go mime/multipart library for text and html parts and bounderies

* use multipart createPart returned writer
added error handling while creating parts
removed unnecessary quotes from boundry string

* removed unnecessary comments
  • Loading branch information
awaragi authored and Corentin Chary committed Sep 14, 2017
1 parent 822a4a2 commit c2f1f2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
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
37 changes: 33 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,51 @@ 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
}

multipartWriter.Close()

wc.Write(buffer.Bytes())

return false, nil
}

Expand Down

0 comments on commit c2f1f2e

Please sign in to comment.