| 1 |
package mail |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"mime" |
| 6 |
"net" |
| 7 |
"net/smtp" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
|
| 12 |
"heckel.io/ntfy/v2/log" |
| 13 |
"heckel.io/ntfy/v2/model" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
tagMail = "mail" |
| 18 |
|
| 19 |
emailVerificationSubject = "Verify your email for ntfy" |
| 20 |
passwordResetSubject = "Reset your ntfy password" |
| 21 |
) |
| 22 |
|
| 23 |
// Config holds the SMTP configuration for the mail sender |
| 24 |
type Config struct { |
| 25 |
BaseURL string // ntfy base URL, used to build topic URLs in notification emails |
| 26 |
SMTPAddr string // SMTP server address (host:port) |
| 27 |
SMTPUser string // SMTP auth username |
| 28 |
SMTPPass string // SMTP auth password |
| 29 |
From string // Sender email address |
| 30 |
} |
| 31 |
|
| 32 |
// Sender sends all of ntfy's outgoing email: notification emails (the email-on-publish feature) |
| 33 |
// as well as the magic-link emails for email verification and password reset. realSender is the |
| 34 |
// SMTP-backed implementation; tests inject a fake. |
| 35 |
type Sender interface { |
| 36 |
SendNotification(to string, m *model.Message, senderIP string) error |
| 37 |
NotificationCounts() (total int64, success int64, failure int64) |
| 38 |
SendEmailVerification(to, link string) error |
| 39 |
SendPasswordReset(to, link string) error |
| 40 |
} |
| 41 |
|
| 42 |
// realSender is the SMTP-backed implementation of Sender. Pending verification/reset state lives |
| 43 |
// in the database (see user.Manager), not in this struct. |
| 44 |
type realSender struct { |
| 45 |
config *Config |
| 46 |
success int64 |
| 47 |
failure int64 |
| 48 |
mu sync.Mutex |
| 49 |
} |
| 50 |
|
| 51 |
// NewSender creates a new mail Sender with the given SMTP config |
| 52 |
func NewSender(config *Config) Sender { |
| 53 |
return &realSender{config: config} |
| 54 |
} |
| 55 |
|
| 56 |
// SendNotification formats a ntfy message into a notification email and sends it via SMTP. It |
| 57 |
// tracks success/failure counts, exposed via Counts (used for the server stats). |
| 58 |
func (s *realSender) SendNotification(to string, m *model.Message, senderIP string) error { |
| 59 |
message, err := formatMail(s.config.BaseURL, senderIP, s.config.From, to, m) |
| 60 |
if err != nil { |
| 61 |
s.count(false) |
| 62 |
return err |
| 63 |
} |
| 64 |
log.Tag(tagMail).Field("email_to", to).Debug("Sending notification email") |
| 65 |
err = s.sendRaw(to, []byte(message)) |
| 66 |
s.count(err == nil) |
| 67 |
return err |
| 68 |
} |
| 69 |
|
| 70 |
// NotificationCounts returns the number of notification emails sent, broken down into total, success and failure |
| 71 |
func (s *realSender) NotificationCounts() (total int64, success int64, failure int64) { |
| 72 |
s.mu.Lock() |
| 73 |
defer s.mu.Unlock() |
| 74 |
return s.success + s.failure, s.success, s.failure |
| 75 |
} |
| 76 |
|
| 77 |
// SendEmailVerification sends an email containing a magic link to verify ownership of the |
| 78 |
// recipient address. The link carries a one-time token validated against the database. |
| 79 |
func (s *realSender) SendEmailVerification(to, link string) error { |
| 80 |
body := fmt.Sprintf(`Click the link below to verify this email address for your ntfy account: |
| 81 |
|
| 82 |
%s |
| 83 |
|
| 84 |
This link expires in 24 hours. If you did not request this, you can safely ignore this email.`, link) |
| 85 |
return s.send(to, emailVerificationSubject, body) |
| 86 |
} |
| 87 |
|
| 88 |
// SendPasswordReset sends an email containing a magic link to set a new password. The link |
| 89 |
// carries a one-time token validated against the database. |
| 90 |
func (s *realSender) SendPasswordReset(to, link string) error { |
| 91 |
body := fmt.Sprintf(`Click the link below to set a new password for your ntfy account: |
| 92 |
|
| 93 |
%s |
| 94 |
|
| 95 |
This link expires in 1 hour. If you did not request this, you can safely ignore this email -- your password will not change.`, link) |
| 96 |
return s.send(to, passwordResetSubject, body) |
| 97 |
} |
| 98 |
|
| 99 |
// send sends a plain text email via SMTP |
| 100 |
func (s *realSender) send(to, subject, body string) error { |
| 101 |
date := time.Now().UTC().Format(time.RFC1123Z) |
| 102 |
encodedSubject := mime.BEncoding.Encode("utf-8", subject) |
| 103 |
message := `From: ntfy <{from}> |
| 104 |
To: {to} |
| 105 |
Date: {date} |
| 106 |
Subject: {subject} |
| 107 |
Content-Type: text/plain; charset="utf-8" |
| 108 |
|
| 109 |
{body}` |
| 110 |
message = strings.ReplaceAll(message, "{from}", s.config.From) |
| 111 |
message = strings.ReplaceAll(message, "{to}", to) |
| 112 |
message = strings.ReplaceAll(message, "{date}", date) |
| 113 |
message = strings.ReplaceAll(message, "{subject}", encodedSubject) |
| 114 |
message = strings.ReplaceAll(message, "{body}", body) |
| 115 |
log.Tag(tagMail).Field("email_to", to).Debug("Sending email") |
| 116 |
return s.sendRaw(to, []byte(message)) |
| 117 |
} |
| 118 |
|
| 119 |
// sendRaw sends a raw email message via SMTP |
| 120 |
func (s *realSender) sendRaw(to string, message []byte) error { |
| 121 |
host, _, err := net.SplitHostPort(s.config.SMTPAddr) |
| 122 |
if err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
var auth smtp.Auth |
| 126 |
if s.config.SMTPUser != "" { |
| 127 |
auth = smtp.PlainAuth("", s.config.SMTPUser, s.config.SMTPPass, host) |
| 128 |
} |
| 129 |
return smtp.SendMail(s.config.SMTPAddr, auth, s.config.From, []string{to}, message) |
| 130 |
} |
| 131 |
|
| 132 |
func (s *realSender) count(ok bool) { |
| 133 |
s.mu.Lock() |
| 134 |
defer s.mu.Unlock() |
| 135 |
if ok { |
| 136 |
s.success++ |
| 137 |
} else { |
| 138 |
s.failure++ |
| 139 |
} |
| 140 |
} |