1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-29 13:12:36 +01:00
ntfy/server/smtp_sender.go

120 lines
3 KiB
Go
Raw Normal View History

2021-12-24 00:03:04 +01:00
package server
import (
2021-12-25 00:13:09 +01:00
_ "embed" // required by go:embed
"encoding/json"
2021-12-24 00:03:04 +01:00
"fmt"
2021-12-25 00:13:09 +01:00
"heckel.io/ntfy/util"
"mime"
2021-12-24 00:03:04 +01:00
"net"
"net/smtp"
"strings"
2021-12-25 00:13:09 +01:00
"time"
2021-12-24 00:03:04 +01:00
)
type mailer interface {
2021-12-24 15:01:29 +01:00
Send(from, to string, m *message) error
2021-12-24 00:03:04 +01:00
}
2021-12-27 16:39:28 +01:00
type smtpSender struct {
2021-12-24 00:03:04 +01:00
config *Config
}
2021-12-27 16:39:28 +01:00
func (s *smtpSender) Send(senderIP, to string, m *message) error {
host, _, err := net.SplitHostPort(s.config.SMTPSenderAddr)
2021-12-24 00:03:04 +01:00
if err != nil {
return err
}
2021-12-27 16:39:28 +01:00
message, err := formatMail(s.config.BaseURL, senderIP, s.config.SMTPSenderFrom, to, m)
2021-12-25 00:13:09 +01:00
if err != nil {
return err
}
2021-12-27 16:39:28 +01:00
auth := smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host)
return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message))
2021-12-25 00:13:09 +01:00
}
func formatMail(baseURL, senderIP, from, to string, m *message) (string, error) {
topicURL := baseURL + "/" + m.Topic
2021-12-24 00:03:04 +01:00
subject := m.Title
if subject == "" {
subject = m.Message
}
subject = strings.ReplaceAll(strings.ReplaceAll(subject, "\r", ""), "\n", " ")
2021-12-24 15:01:29 +01:00
message := m.Message
2021-12-25 00:13:09 +01:00
trailer := ""
2021-12-24 15:01:29 +01:00
if len(m.Tags) > 0 {
2021-12-25 00:13:09 +01:00
emojis, tags, err := toEmojis(m.Tags)
if err != nil {
return "", err
}
if len(emojis) > 0 {
subject = strings.Join(emojis, " ") + " " + subject
}
if len(tags) > 0 {
trailer = "Tags: " + strings.Join(tags, ", ")
}
2021-12-24 15:01:29 +01:00
}
if m.Priority != 0 && m.Priority != 3 {
2021-12-25 00:13:09 +01:00
priority, err := util.PriorityString(m.Priority)
if err != nil {
return "", err
}
if trailer != "" {
trailer += "\n"
}
trailer += fmt.Sprintf("Priority: %s", priority)
2021-12-24 15:01:29 +01:00
}
2021-12-25 00:13:09 +01:00
if trailer != "" {
message += "\n\n" + trailer
}
subject = mime.BEncoding.Encode("utf-8", subject)
body := `From: "{shortTopicURL}" <{from}>
2021-12-25 00:13:09 +01:00
To: {to}
Subject: {subject}
Content-Type: text/plain; charset="utf-8"
2021-12-25 00:13:09 +01:00
{message}
--
This message was sent by {ip} at {time} via {topicURL}`
body = strings.ReplaceAll(body, "{from}", from)
body = strings.ReplaceAll(body, "{to}", to)
body = strings.ReplaceAll(body, "{subject}", subject)
body = strings.ReplaceAll(body, "{message}", message)
body = strings.ReplaceAll(body, "{topicURL}", topicURL)
body = strings.ReplaceAll(body, "{shortTopicURL}", util.ShortTopicURL(topicURL))
body = strings.ReplaceAll(body, "{time}", time.Unix(m.Time, 0).UTC().Format(time.RFC1123))
body = strings.ReplaceAll(body, "{ip}", senderIP)
return body, nil
}
var (
//go:embed "mailer_emoji.json"
emojisJSON string
)
type emoji struct {
Emoji string `json:"emoji"`
Aliases []string `json:"aliases"`
}
func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) {
var emojis []emoji
if err = json.Unmarshal([]byte(emojisJSON), &emojis); err != nil {
return nil, nil, err
}
tagsOut = make([]string, 0)
emojisOut = make([]string, 0)
nextTag:
for _, t := range tags { // TODO Super inefficient; we should just create a .json file with a map
for _, e := range emojis {
if util.InStringList(e.Aliases, t) {
emojisOut = append(emojisOut, e.Emoji)
continue nextTag
}
}
tagsOut = append(tagsOut, t)
}
return
2021-12-24 00:03:04 +01:00
}