2020-05-20 12:28:08 +00:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
isHTMLRgx = regexp.MustCompile(`.*<html.*>.*`)
|
|
|
|
lineBreak = "\r\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EmailMessage struct {
|
|
|
|
Recipients []string
|
|
|
|
BCC []string
|
|
|
|
CC []string
|
|
|
|
SenderEmail string
|
|
|
|
Subject string
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *EmailMessage) GetContent() string {
|
|
|
|
headers := make(map[string]string)
|
|
|
|
headers["From"] = msg.SenderEmail
|
|
|
|
headers["To"] = strings.Join(msg.Recipients, ", ")
|
|
|
|
headers["Cc"] = strings.Join(msg.CC, ", ")
|
|
|
|
|
|
|
|
message := ""
|
|
|
|
for k, v := range headers {
|
|
|
|
message += fmt.Sprintf("%s: %s"+lineBreak, k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
//default mime-type is html
|
2021-01-04 13:52:13 +00:00
|
|
|
mime := "MIME-version: 1.0;" + lineBreak + "Content-KeyType: text/html; charset=\"UTF-8\";" + lineBreak + lineBreak
|
2020-05-20 12:28:08 +00:00
|
|
|
if !isHTML(msg.Content) {
|
2021-01-04 13:52:13 +00:00
|
|
|
mime = "MIME-version: 1.0;" + lineBreak + "Content-KeyType: text/plain; charset=\"UTF-8\";" + lineBreak + lineBreak
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
subject := "Subject: " + msg.Subject + lineBreak
|
|
|
|
message += subject + mime + lineBreak + msg.Content
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHTML(input string) bool {
|
|
|
|
return isHTMLRgx.MatchString(input)
|
|
|
|
}
|