2021-12-24 00:03:04 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-26 14:34:25 +01:00
|
|
|
"mime"
|
2021-12-24 00:03:04 +01:00
|
|
|
"net"
|
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
2022-06-02 05:24:44 +02:00
|
|
|
"sync"
|
2021-12-25 00:13:09 +01:00
|
|
|
"time"
|
Convert mailer_emoji JSON file to map
This fixes a pending TODO comment regarding inefficient tags to emojis
mapping, by requiring a full scan over emoji aliases to determine
matches.
Instead, now the JSON file is a map, with aliases as keys, and emojis as
values. The script to convert the file with Python was:
```python
import json
with open("./mailer_emoji.json", "r", encoding="utf-8") as f:
content = json.load(f)
emoji_map = {}
for emoji in content:
for alias in emoji["aliases"]:
if alias in emoji_map:
print("WARNING: Duplicate alias:", alias)
continue
emoji_map[alias] = str(emoji["emoji"])
sorted_emoji_map = {k: emoji_map[k] for k in sorted(emoji_map)}
with open("./mailer_emoji_map.json", "w", encoding="utf-8") as f:
json.dump(sorted_emoji_map, f, indent=4, ensure_ascii=False)
```
2023-05-13 16:33:04 +02:00
|
|
|
|
|
|
|
"heckel.io/ntfy/log"
|
|
|
|
"heckel.io/ntfy/util"
|
2021-12-24 00:03:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mailer interface {
|
2022-06-02 05:24:44 +02:00
|
|
|
Send(v *visitor, m *message, to string) error
|
|
|
|
Counts() (total int64, success int64, failure int64)
|
2021-12-24 00:03:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-27 16:39:28 +01:00
|
|
|
type smtpSender struct {
|
2022-06-02 05:24:44 +02:00
|
|
|
config *Config
|
|
|
|
success int64
|
|
|
|
failure int64
|
|
|
|
mu sync.Mutex
|
2021-12-24 00:03:04 +01:00
|
|
|
}
|
|
|
|
|
2022-06-02 05:24:44 +02:00
|
|
|
func (s *smtpSender) Send(v *visitor, m *message, to string) error {
|
|
|
|
return s.withCount(v, m, func() error {
|
|
|
|
host, _, err := net.SplitHostPort(s.config.SMTPSenderAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-05 22:42:07 +02:00
|
|
|
message, err := formatMail(s.config.BaseURL, v.ip.String(), s.config.SMTPSenderFrom, to, m)
|
2022-06-02 05:24:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-03 02:25:13 +01:00
|
|
|
var auth smtp.Auth
|
|
|
|
if s.config.SMTPSenderUser != "" {
|
|
|
|
auth = smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host)
|
|
|
|
}
|
2023-02-15 16:55:01 +01:00
|
|
|
ev := logvm(v, m).
|
2023-02-04 04:21:50 +01:00
|
|
|
Tag(tagEmail).
|
2023-02-06 05:34:27 +01:00
|
|
|
Fields(log.Context{
|
2023-02-04 04:21:50 +01:00
|
|
|
"email_via": s.config.SMTPSenderAddr,
|
|
|
|
"email_user": s.config.SMTPSenderUser,
|
|
|
|
"email_to": to,
|
2023-02-15 16:55:01 +01:00
|
|
|
})
|
|
|
|
if ev.IsTrace() {
|
|
|
|
ev.Field("email_body", message).Trace("Sending email")
|
|
|
|
} else if ev.IsDebug() {
|
|
|
|
ev.Debug("Sending email")
|
|
|
|
}
|
2022-06-02 05:24:44 +02:00
|
|
|
return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSender) Counts() (total int64, success int64, failure int64) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
return s.success + s.failure, s.success, s.failure
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *smtpSender) withCount(v *visitor, m *message, fn func() error) error {
|
|
|
|
err := fn()
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-12-25 00:13:09 +01:00
|
|
|
if err != nil {
|
2023-02-04 04:21:50 +01:00
|
|
|
logvm(v, m).Err(err).Debug("Sending mail failed")
|
2022-06-02 05:24:44 +02:00
|
|
|
s.failure++
|
|
|
|
} else {
|
|
|
|
s.success++
|
2021-12-25 00:13:09 +01:00
|
|
|
}
|
2022-06-02 05:24:44 +02:00
|
|
|
return err
|
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
|
|
|
|
}
|
2021-12-26 14:34:25 +01:00
|
|
|
subject = mime.BEncoding.Encode("utf-8", subject)
|
|
|
|
body := `From: "{shortTopicURL}" <{from}>
|
2021-12-25 00:13:09 +01:00
|
|
|
To: {to}
|
|
|
|
Subject: {subject}
|
2021-12-26 14:34:25 +01:00
|
|
|
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
|
|
|
|
}
|