mailer.go 2.9 KB

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