2022-01-06 08:00:24 +00:00
|
|
|
package messages
|
2020-05-20 12:28:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-22 14:11:55 +00:00
|
|
|
"mime"
|
2020-05-20 12:28:08 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2023-08-11 09:17:24 +00:00
|
|
|
"time"
|
2022-01-06 08:00:24 +00:00
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels"
|
2020-05-20 12:28:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
isHTMLRgx = regexp.MustCompile(`.*<html.*>.*`)
|
|
|
|
lineBreak = "\r\n"
|
|
|
|
)
|
|
|
|
|
2022-01-06 08:00:24 +00:00
|
|
|
var _ channels.Message = (*Email)(nil)
|
|
|
|
|
|
|
|
type Email struct {
|
2023-03-28 22:09:06 +00:00
|
|
|
Recipients []string
|
|
|
|
BCC []string
|
|
|
|
CC []string
|
|
|
|
SenderEmail string
|
|
|
|
SenderName string
|
2023-08-29 07:08:24 +00:00
|
|
|
ReplyToAddress string
|
2023-03-28 22:09:06 +00:00
|
|
|
Subject string
|
|
|
|
Content string
|
|
|
|
TriggeringEvent eventstore.Event
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
func (msg *Email) GetContent() (string, error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
headers := make(map[string]string)
|
2022-02-16 15:49:17 +00:00
|
|
|
from := msg.SenderEmail
|
|
|
|
if msg.SenderName != "" {
|
|
|
|
from = fmt.Sprintf("%s <%s>", msg.SenderName, msg.SenderEmail)
|
|
|
|
}
|
|
|
|
headers["From"] = from
|
2023-08-29 07:08:24 +00:00
|
|
|
if msg.ReplyToAddress != "" {
|
|
|
|
headers["Reply-to"] = msg.ReplyToAddress
|
|
|
|
}
|
2022-10-17 15:05:03 +00:00
|
|
|
headers["Return-Path"] = msg.SenderEmail
|
2020-05-20 12:28:08 +00:00
|
|
|
headers["To"] = strings.Join(msg.Recipients, ", ")
|
|
|
|
headers["Cc"] = strings.Join(msg.CC, ", ")
|
2023-08-11 09:17:24 +00:00
|
|
|
headers["Date"] = time.Now().Format(time.RFC1123Z)
|
2020-05-20 12:28:08 +00:00
|
|
|
|
|
|
|
message := ""
|
|
|
|
for k, v := range headers {
|
|
|
|
message += fmt.Sprintf("%s: %s"+lineBreak, k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
//default mime-type is html
|
2024-05-02 08:36:21 +00:00
|
|
|
mime := "MIME-Version: 1.0" + lineBreak + "Content-Type: text/html; charset=\"UTF-8\"" + lineBreak + lineBreak
|
2020-05-20 12:28:08 +00:00
|
|
|
if !isHTML(msg.Content) {
|
2024-05-02 08:36:21 +00:00
|
|
|
mime = "MIME-Version: 1.0" + lineBreak + "Content-Type: text/plain; charset=\"UTF-8\"" + lineBreak + lineBreak
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
2024-02-22 14:11:55 +00:00
|
|
|
subject := "Subject: " + bEncodeSubject(msg.Subject) + lineBreak
|
2020-05-20 12:28:08 +00:00
|
|
|
message += subject + mime + lineBreak + msg.Content
|
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Email) GetTriggeringEvent() eventstore.Event {
|
|
|
|
return msg.TriggeringEvent
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isHTML(input string) bool {
|
|
|
|
return isHTMLRgx.MatchString(input)
|
|
|
|
}
|
2023-09-29 08:53:45 +00:00
|
|
|
|
2024-02-22 14:11:55 +00:00
|
|
|
// returns a RFC1342 "B" encoded string to allow non-ascii characters
|
|
|
|
func bEncodeSubject(subject string) string {
|
|
|
|
return mime.BEncoding.Encode("UTF-8", subject)
|
2023-09-29 08:53:45 +00:00
|
|
|
}
|