blob: 876fd1aff7a4c8dd6293307f268a7d21a59b1e66 [file] [log] [blame]
package email
import (
"bytes"
"fmt"
"html/template"
"regexp"
"strings"
ttemplate "text/template"
"go.skia.org/infra/go/skerr"
)
const (
viewActionMarkupTemplate = `
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="potentialAction" itemscope itemtype="http://schema.org/ViewAction">
<link itemprop="target" href="{{.Link}}"/>
<meta itemprop="name" content="{{.Name}}"/>
</div>
<meta itemprop="description" content="{{.Description}}"/>
</div>
`
emailTemplate = `From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
Content-Type: text/html; charset=UTF-8
{{if .ThreadingReference}}References: {{.ThreadingReference}}
{{end}}{{if .ThreadingReference}}In-Reply-To: {{.ThreadingReference}}
{{end}}{{if .MessageID -}}Message-ID: {{.MessageID}}
{{end}}
<html>
<body>
{{.Markup}}
{{.Body}}
</body>
</html>
`
)
var (
viewActionMarkupTemplateParsed *template.Template = nil
emailTemplateParsed *ttemplate.Template = nil
)
func init() {
viewActionMarkupTemplateParsed = template.Must(template.New("view_action").Parse(viewActionMarkupTemplate))
emailTemplateParsed = ttemplate.Must(ttemplate.New("email").Parse(emailTemplate))
}
// GetViewActionMarkup returns a string that contains the required markup.
func GetViewActionMarkup(link, name, description string) (string, error) {
markupBytes := new(bytes.Buffer)
if err := viewActionMarkupTemplateParsed.Execute(markupBytes, struct {
Link string
Name string
Description string
}{
Link: link,
Name: name,
Description: description,
}); err != nil {
return "", skerr.Wrapf(err, "Could not execute template %s", name)
}
return markupBytes.String(), nil
}
// FormatAsRFC2822 returns a *bytes.Buffer that contains the email message
// formatted in RFC 2822 format.
func FormatAsRFC2822(fromDisplayName string, from string, to []string, subject, body, markup, threadingReference string, messageID string) (*bytes.Buffer, error) {
fromWithName := fmt.Sprintf("%s <%s>", fromDisplayName, from)
var msgBytes bytes.Buffer
if err := emailTemplateParsed.Execute(&msgBytes, struct {
From template.HTML
To string
Subject string
ThreadingReference string
Body template.HTML
Markup template.HTML
MessageID string
}{
From: template.HTML(fromWithName),
To: strings.Join(to, ","),
Subject: subject,
ThreadingReference: threadingReference,
Body: template.HTML(body),
Markup: template.HTML(markup),
MessageID: messageID,
}); err != nil {
return nil, skerr.Wrapf(err, "Failed to format email.")
}
return &msgBytes, nil
}
var fromRegex = regexp.MustCompile(`(?m)^From: (.*)$`)
var toRegex = regexp.MustCompile(`(?m)^To: (.*)$`)
var subjectRegex = regexp.MustCompile(`(?m)^Subject:(.*)$`)
var doubleNewLine = regexp.MustCompile(`\n\n`)
const defaultSubject = "(no subject)"
// ParseRFC2822Message returns the email address in the From:, To: and Subject:
// lines, and also returns the body of the message, which is presumed to be an
// HTML formatted email.
func ParseRFC2822Message(body []byte) (string, []string, string, string, error) {
// From: senderDisplayName <sender email>
// Subject: subject
// To: A Display Name <a@example.com>, B <b@example.org>
match := fromRegex.FindSubmatch(body)
if match == nil || len(match) < 2 {
return "", nil, "", "", skerr.Fmt("Failed to find a From: line in message.")
}
from := string(match[1])
match = subjectRegex.FindSubmatch(body)
subject := defaultSubject
if len(match) >= 2 {
subject = string(bytes.TrimSpace(match[1]))
}
match = toRegex.FindSubmatch(body)
if match == nil || len(match) < 2 {
return "", nil, "", "", skerr.Fmt("Failed to find a To: line in message.")
}
to := []string{}
for _, addr := range bytes.Split(match[1], []byte(",")) {
toAsString := string(bytes.TrimSpace(addr))
if toAsString != "" {
to = append(to, toAsString)
}
}
if len(to) < 1 {
return "", nil, "", "", skerr.Fmt("Failed to find any To: addresses.")
}
parts := doubleNewLine.Split(string(body), 2)
if len(parts) != 2 {
return "", nil, "", "", skerr.Fmt("Failed to find the body of the message.")
}
messageBody := parts[1]
return from, to, subject, messageBody, nil
}