smtp_sender.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package server
  2. import (
  3. "fmt"
  4. "mime"
  5. "net"
  6. "net/smtp"
  7. "strings"
  8. "sync"
  9. "time"
  10. "heckel.io/ntfy/log"
  11. "heckel.io/ntfy/util"
  12. )
  13. type mailer interface {
  14. Send(v *visitor, m *message, to string) error
  15. Counts() (total int64, success int64, failure int64)
  16. }
  17. type smtpSender struct {
  18. config *Config
  19. success int64
  20. failure int64
  21. mu sync.Mutex
  22. }
  23. func (s *smtpSender) Send(v *visitor, m *message, to string) error {
  24. return s.withCount(v, m, func() error {
  25. host, _, err := net.SplitHostPort(s.config.SMTPSenderAddr)
  26. if err != nil {
  27. return err
  28. }
  29. message, err := formatMail(s.config.BaseURL, v.ip.String(), s.config.SMTPSenderFrom, to, m)
  30. if err != nil {
  31. return err
  32. }
  33. var auth smtp.Auth
  34. if s.config.SMTPSenderUser != "" {
  35. auth = smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host)
  36. }
  37. ev := logvm(v, m).
  38. Tag(tagEmail).
  39. Fields(log.Context{
  40. "email_via": s.config.SMTPSenderAddr,
  41. "email_user": s.config.SMTPSenderUser,
  42. "email_to": to,
  43. })
  44. if ev.IsTrace() {
  45. ev.Field("email_body", message).Trace("Sending email")
  46. } else if ev.IsDebug() {
  47. ev.Debug("Sending email")
  48. }
  49. return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message))
  50. })
  51. }
  52. func (s *smtpSender) Counts() (total int64, success int64, failure int64) {
  53. s.mu.Lock()
  54. defer s.mu.Unlock()
  55. return s.success + s.failure, s.success, s.failure
  56. }
  57. func (s *smtpSender) withCount(v *visitor, m *message, fn func() error) error {
  58. err := fn()
  59. s.mu.Lock()
  60. defer s.mu.Unlock()
  61. if err != nil {
  62. logvm(v, m).Err(err).Debug("Sending mail failed")
  63. s.failure++
  64. } else {
  65. s.success++
  66. }
  67. return err
  68. }
  69. func formatMail(baseURL, senderIP, from, to string, m *message) (string, error) {
  70. topicURL := baseURL + "/" + m.Topic
  71. subject := m.Title
  72. if subject == "" {
  73. subject = m.Message
  74. }
  75. subject = strings.ReplaceAll(strings.ReplaceAll(subject, "\r", ""), "\n", " ")
  76. message := m.Message
  77. trailer := ""
  78. if len(m.Tags) > 0 {
  79. emojis, tags, err := toEmojis(m.Tags)
  80. if err != nil {
  81. return "", err
  82. }
  83. if len(emojis) > 0 {
  84. subject = strings.Join(emojis, " ") + " " + subject
  85. }
  86. if len(tags) > 0 {
  87. trailer = "Tags: " + strings.Join(tags, ", ")
  88. }
  89. }
  90. if m.Priority != 0 && m.Priority != 3 {
  91. priority, err := util.PriorityString(m.Priority)
  92. if err != nil {
  93. return "", err
  94. }
  95. if trailer != "" {
  96. trailer += "\n"
  97. }
  98. trailer += fmt.Sprintf("Priority: %s", priority)
  99. }
  100. if trailer != "" {
  101. message += "\n\n" + trailer
  102. }
  103. subject = mime.BEncoding.Encode("utf-8", subject)
  104. body := `From: "{shortTopicURL}" <{from}>
  105. To: {to}
  106. Subject: {subject}
  107. Content-Type: text/plain; charset="utf-8"
  108. {message}
  109. --
  110. This message was sent by {ip} at {time} via {topicURL}`
  111. body = strings.ReplaceAll(body, "{from}", from)
  112. body = strings.ReplaceAll(body, "{to}", to)
  113. body = strings.ReplaceAll(body, "{subject}", subject)
  114. body = strings.ReplaceAll(body, "{message}", message)
  115. body = strings.ReplaceAll(body, "{topicURL}", topicURL)
  116. body = strings.ReplaceAll(body, "{shortTopicURL}", util.ShortTopicURL(topicURL))
  117. body = strings.ReplaceAll(body, "{time}", time.Unix(m.Time, 0).UTC().Format(time.RFC1123))
  118. body = strings.ReplaceAll(body, "{ip}", senderIP)
  119. return body, nil
  120. }