2018-08-21 17:21:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-22 10:29:14 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"net/mail"
|
2018-08-21 17:21:46 +00:00
|
|
|
)
|
|
|
|
|
2018-08-22 10:29:14 +00:00
|
|
|
type EmailHeaders map[string]string
|
|
|
|
type EmailBody string
|
2018-08-21 17:21:46 +00:00
|
|
|
|
|
|
|
type EmailCtx struct {
|
2018-08-22 10:29:14 +00:00
|
|
|
Headers EmailHeaders
|
|
|
|
Body EmailBody
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (body EmailBody) String() string {
|
|
|
|
res := string(body)
|
|
|
|
if false {
|
|
|
|
spew.Dump(res)
|
|
|
|
}
|
|
|
|
return res
|
2018-08-21 17:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmail() *EmailCtx {
|
2018-08-22 10:29:14 +00:00
|
|
|
email := EmailCtx{}
|
|
|
|
email.Headers = make(EmailHeaders)
|
|
|
|
return &email
|
2018-08-21 17:21:46 +00:00
|
|
|
}
|
|
|
|
|
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(), " \"<@>")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (email *EmailCtx) MakeHeaders(config EmailConfig) {
|
|
|
|
email.Headers["Return-Path"] = config.From
|
2018-08-21 17:21:46 +00:00
|
|
|
email.Headers["From"] = config.From
|
|
|
|
email.Headers["To"] = config.To
|
2018-08-22 10:29:14 +00:00
|
|
|
email.Headers["Subject"] = encodeRFC2047(config.Subject)
|
|
|
|
// email.Headers["Content-Type"] = "text/plain; charset=\"us-ascii\";"
|
|
|
|
email.Headers["Content-Type"] = "text/plain; charset=\"utf-8\";"
|
|
|
|
email.Headers["Content-Transfer-Encoding"] = "base64"
|
|
|
|
email.Headers["MIME-Version"] = "1.0"
|
2018-08-21 17:21:46 +00:00
|
|
|
|
2018-08-22 10:29:14 +00:00
|
|
|
return
|
2018-08-21 17:21:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 10:29:14 +00:00
|
|
|
func (email *EmailCtx) MakeBody(content string) {
|
|
|
|
email.Body = EmailBody(content)
|
|
|
|
if false {
|
|
|
|
spew.Dump(email.Body)
|
|
|
|
}
|
|
|
|
return
|
2018-08-21 17:21:46 +00:00
|
|
|
}
|