all repos — navani @ ac7cf9514f9fa09c55057e16de23c946e6abcb05

forlater's primary mail processing service

mail/utils.go (view raw)

 1package mail
 2
 3import (
 4	"fmt"
 5
 6	"github.com/microcosm-cc/bluemonday"
 7	"mvdan.cc/xurls/v2"
 8)
 9
10type Mail struct {
11	From    string
12	Date    string
13	ReplyTo string
14	Parts   map[string]string
15}
16
17// Extracts URLs from given text.
18func ExtractURLs(text string) []string {
19	x := xurls.Strict()
20	return x.FindAllString(text, -1)
21}
22
23// Returns the main body of the email; hopefully containing URLs.
24func MailBody(parts map[string]string) (string, error) {
25	if plain, ok := parts["text/plain"]; ok {
26		return plain, nil
27	} else if html, ok := parts["text/html"]; ok {
28		p := bluemonday.NewPolicy()
29		p.AllowStandardURLs()
30		p.AddSpaceWhenStrippingTag(true)
31		clean := p.Sanitize(html)
32		return clean, nil
33	} else {
34		return "", fmt.Errorf("no good MIME type found")
35	}
36}