2022-01-06 09:00:24 +01:00
|
|
|
package messages
|
2020-05-20 14:28:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-01-06 09:00:24 +01:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/notification/channels"
|
2020-05-20 14:28:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
isHTMLRgx = regexp.MustCompile(`.*<html.*>.*`)
|
|
|
|
lineBreak = "\r\n"
|
|
|
|
)
|
|
|
|
|
2022-01-06 09:00:24 +01:00
|
|
|
var _ channels.Message = (*Email)(nil)
|
|
|
|
|
|
|
|
type Email struct {
|
2020-05-20 14:28:08 +02:00
|
|
|
Recipients []string
|
|
|
|
BCC []string
|
|
|
|
CC []string
|
|
|
|
SenderEmail string
|
|
|
|
Subject string
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:00:24 +01:00
|
|
|
func (msg *Email) GetContent() string {
|
2020-05-20 14:28:08 +02:00
|
|
|
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-03-22 14:40:25 +01:00
|
|
|
mime := "MIME-version: 1.0;" + lineBreak + "Content-Type: text/html; charset=\"UTF-8\";" + lineBreak + lineBreak
|
2020-05-20 14:28:08 +02:00
|
|
|
if !isHTML(msg.Content) {
|
2021-03-22 14:40:25 +01:00
|
|
|
mime = "MIME-version: 1.0;" + lineBreak + "Content-Type: text/plain; charset=\"UTF-8\";" + lineBreak + lineBreak
|
2020-05-20 14:28:08 +02:00
|
|
|
}
|
|
|
|
subject := "Subject: " + msg.Subject + lineBreak
|
|
|
|
message += subject + mime + lineBreak + msg.Content
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHTML(input string) bool {
|
|
|
|
return isHTMLRgx.MatchString(input)
|
|
|
|
}
|