smtp_sender.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package server
  2. import (
  3. _ "embed" // required by go:embed
  4. "encoding/json"
  5. "fmt"
  6. "heckel.io/ntfy/util"
  7. "mime"
  8. "net"
  9. "net/smtp"
  10. "strings"
  11. "time"
  12. )
  13. type mailer interface {
  14. Send(from, to string, m *message) error
  15. }
  16. type smtpSender struct {
  17. config *Config
  18. }
  19. func (s *smtpSender) Send(senderIP, to string, m *message) error {
  20. host, _, err := net.SplitHostPort(s.config.SMTPSenderAddr)
  21. if err != nil {
  22. return err
  23. }
  24. message, err := formatMail(s.config.BaseURL, senderIP, s.config.SMTPSenderFrom, to, m)
  25. if err != nil {
  26. return err
  27. }
  28. auth := smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host)
  29. return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message))
  30. }
  31. func formatMail(baseURL, senderIP, from, to string, m *message) (string, error) {
  32. topicURL := baseURL + "/" + m.Topic
  33. subject := m.Title
  34. if subject == "" {
  35. subject = m.Message
  36. }
  37. subject = strings.ReplaceAll(strings.ReplaceAll(subject, "\r", ""), "\n", " ")
  38. message := m.Message
  39. trailer := ""
  40. if len(m.Tags) > 0 {
  41. emojis, tags, err := toEmojis(m.Tags)
  42. if err != nil {
  43. return "", err
  44. }
  45. if len(emojis) > 0 {
  46. subject = strings.Join(emojis, " ") + " " + subject
  47. }
  48. if len(tags) > 0 {
  49. trailer = "Tags: " + strings.Join(tags, ", ")
  50. }
  51. }
  52. if m.Priority != 0 && m.Priority != 3 {
  53. priority, err := util.PriorityString(m.Priority)
  54. if err != nil {
  55. return "", err
  56. }
  57. if trailer != "" {
  58. trailer += "\n"
  59. }
  60. trailer += fmt.Sprintf("Priority: %s", priority)
  61. }
  62. if trailer != "" {
  63. message += "\n\n" + trailer
  64. }
  65. subject = mime.BEncoding.Encode("utf-8", subject)
  66. body := `From: "{shortTopicURL}" <{from}>
  67. To: {to}
  68. Subject: {subject}
  69. Content-Type: text/plain; charset="utf-8"
  70. {message}
  71. --
  72. This message was sent by {ip} at {time} via {topicURL}`
  73. body = strings.ReplaceAll(body, "{from}", from)
  74. body = strings.ReplaceAll(body, "{to}", to)
  75. body = strings.ReplaceAll(body, "{subject}", subject)
  76. body = strings.ReplaceAll(body, "{message}", message)
  77. body = strings.ReplaceAll(body, "{topicURL}", topicURL)
  78. body = strings.ReplaceAll(body, "{shortTopicURL}", util.ShortTopicURL(topicURL))
  79. body = strings.ReplaceAll(body, "{time}", time.Unix(m.Time, 0).UTC().Format(time.RFC1123))
  80. body = strings.ReplaceAll(body, "{ip}", senderIP)
  81. return body, nil
  82. }
  83. var (
  84. //go:embed "mailer_emoji.json"
  85. emojisJSON string
  86. )
  87. type emoji struct {
  88. Emoji string `json:"emoji"`
  89. Aliases []string `json:"aliases"`
  90. }
  91. func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) {
  92. var emojis []emoji
  93. if err = json.Unmarshal([]byte(emojisJSON), &emojis); err != nil {
  94. return nil, nil, err
  95. }
  96. tagsOut = make([]string, 0)
  97. emojisOut = make([]string, 0)
  98. nextTag:
  99. for _, t := range tags { // TODO Super inefficient; we should just create a .json file with a map
  100. for _, e := range emojis {
  101. if util.InStringList(e.Aliases, t) {
  102. emojisOut = append(emojisOut, e.Emoji)
  103. continue nextTag
  104. }
  105. }
  106. tagsOut = append(tagsOut, t)
  107. }
  108. return
  109. }