musala/cmd/trello2mail/email.go

109 lines
2.9 KiB
Go
Raw Normal View History

package main
2018-08-22 16:57:50 +00:00
// TODO: use https://github.com/domodwyer/mailyak as a base lib
import (
2018-08-22 10:29:14 +00:00
"bytes"
2018-08-22 16:57:50 +00:00
"encoding/base64"
2018-08-22 10:29:14 +00:00
"fmt"
2018-08-22 16:57:50 +00:00
// "github.com/davecgh/go-spew/spew"
2018-08-22 12:01:44 +00:00
"strings"
2018-08-22 10:29:14 +00:00
// "log"
// "strconv"
2018-08-22 16:57:50 +00:00
"math/rand"
2018-08-22 10:29:14 +00:00
"net/mail"
)
2018-08-22 10:29:14 +00:00
type EmailHeaders map[string]string
type EmailCtx struct {
2018-08-22 16:57:50 +00:00
Headers EmailHeaders
BodyPlain string
BodyHtml string
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
2018-08-22 10:29:14 +00:00
}
func (headers EmailHeaders) String() string {
var buffer bytes.Buffer
for k, v := range headers {
buffer.WriteString(fmt.Sprintf("%s: %s\r\n", k, v))
}
return buffer.String()
}
func NewEmail() *EmailCtx {
2018-08-22 10:29:14 +00:00
email := EmailCtx{}
email.Headers = make(EmailHeaders)
return &email
}
2018-08-22 10:29:14 +00:00
func encodeRFC2047(text string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{text, ""}
return strings.Trim(addr.String(), " \"<@>")
}
2018-08-22 16:57:50 +00:00
func (email *EmailCtx) SetHeaders(config EmailConfig) {
2018-08-22 10:29:14 +00:00
email.Headers["Return-Path"] = config.From
email.Headers["From"] = config.From
email.Headers["To"] = config.To
2018-08-22 10:29:14 +00:00
email.Headers["Subject"] = encodeRFC2047(config.Subject)
2018-08-22 16:57:50 +00:00
email.Headers["Content-Transfer-Encoding"] = "quoted-printable"
2018-08-22 10:29:14 +00:00
email.Headers["MIME-Version"] = "1.0"
2018-08-22 10:29:14 +00:00
return
}
2018-08-22 16:57:50 +00:00
func (email *EmailCtx) SetBody(html string, plain string) {
email.BodyPlain = plain
email.BodyHtml = html
2018-08-22 10:29:14 +00:00
return
}
2018-08-22 16:57:50 +00:00
func (email *EmailCtx) String() string {
var buffer bytes.Buffer
mixBoundary := RandStringBytes(16)
altBoundary := RandStringBytes(16)
buffer.WriteString(email.Headers.String())
buffer.WriteString(fmt.Sprintf("Content-Type: multipart/mixed;\r\n boundary=\"%s\"\r\n", mixBoundary))
buffer.WriteString("\r\n")
buffer.WriteString(fmt.Sprintf("--%s\r\n", mixBoundary))
buffer.WriteString(fmt.Sprintf("Content-Type: multipart/alternative;\r\n boundary=\"%s\"\r\n", altBoundary))
buffer.WriteString("\r\n")
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
buffer.WriteString(fmt.Sprintf("Content-Type: text/plain; charset=\"utf-8\"\r\n"))
buffer.WriteString(fmt.Sprintf("Content-Transfer-Encoding: base64\r\n"))
buffer.WriteString("\r\n")
buffer.WriteString(base64.StdEncoding.EncodeToString([]byte(email.BodyPlain)))
buffer.WriteString("\r\n")
buffer.WriteString("\r\n")
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
buffer.WriteString(fmt.Sprintf("Content-Type: text/html; charset=\"utf-8\"\r\n"))
buffer.WriteString(fmt.Sprintf("Content-Transfer-Encoding: base64\r\n"))
buffer.WriteString("\r\n")
buffer.WriteString(base64.StdEncoding.EncodeToString([]byte(email.BodyHtml)))
buffer.WriteString("\r\n")
buffer.WriteString("\r\n")
buffer.WriteString(fmt.Sprintf("--%s--\r\n", altBoundary))
buffer.WriteString("\r\n")
buffer.WriteString(fmt.Sprintf("--%s--\r\n", mixBoundary))
buffer.WriteString("\r\n")
return buffer.String()
}