sender.go89 lines, 2.3 KB
| 1 | package notification |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net" |
| 9 | "net/smtp" |
| 10 | "net/textproto" |
| 11 | "time" |
| 12 | |
| 13 | "go.opentelemetry.io/otel/codes" |
| 14 | "go.opentelemetry.io/otel/trace" |
| 15 | ) |
| 16 | |
| 17 | // A Sender delivers messages. In production it's an email provider's API; |
| 18 | // in development, Mailpit, which catches everything we send. |
| 19 | type Sender interface { |
| 20 | Send(ctx context.Context, m Message) error |
| 21 | } |
| 22 | |
| 23 | // SMTP sends messages through an SMTP server. |
| 24 | type SMTP struct { |
| 25 | Addr string // host:port |
| 26 | From string // an address, like no-reply@payments.example |
| 27 | } |
| 28 | |
| 29 | // Send sends m. A 5xx reply, like "no such mailbox", is permanent. Anything |
| 30 | // else, like a server that's down, might work later. |
| 31 | func (s SMTP) Send(ctx context.Context, m Message) error { |
| 32 | // No library traces net/smtp for us, so we make the span ourselves. |
| 33 | ctx, span := tracer.Start(ctx, "smtp send", trace.WithSpanKind(trace.SpanKindClient)) |
| 34 | defer span.End() |
| 35 | err := s.send(ctx, m) |
| 36 | if err != nil { |
| 37 | span.SetStatus(codes.Error, err.Error()) |
| 38 | } |
| 39 | var reply *textproto.Error |
| 40 | if errors.As(err, &reply) && reply.Code >= 500 { |
| 41 | return Permanent(err) |
| 42 | } |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | func (s SMTP) send(ctx context.Context, m Message) error { |
| 47 | var d net.Dialer |
| 48 | conn, err := d.DialContext(ctx, "tcp", s.Addr) |
| 49 | if err != nil { |
| 50 | return fmt.Errorf("smtp: %w", err) |
| 51 | } |
| 52 | defer conn.Close() |
| 53 | if deadline, ok := ctx.Deadline(); ok { |
| 54 | conn.SetDeadline(deadline) // net/smtp knows nothing about contexts |
| 55 | } |
| 56 | host, _, _ := net.SplitHostPort(s.Addr) |
| 57 | c, err := smtp.NewClient(conn, host) |
| 58 | if err != nil { |
| 59 | return fmt.Errorf("smtp: %w", err) |
| 60 | } |
| 61 | if err := c.Mail(s.From); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | if err := c.Rcpt(m.To); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | w, err := c.Data() |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if _, err := w.Write(format(s.From, m)); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if err := w.Close(); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | return c.Quit() |
| 78 | } |
| 79 | |
| 80 | // format writes m as an email. Its Message-ID is the event's ID: a mail |
| 81 | // system that sees the same ID twice can drop the copy. |
| 82 | func format(from string, m Message) []byte { |
| 83 | var b bytes.Buffer |
| 84 | fmt.Fprintf(&b, "From: Payments <%s>\r\nTo: %s\r\nSubject: %s\r\n", from, m.To, m.Subject) |
| 85 | fmt.Fprintf(&b, "Message-ID: <%s@payments.example>\r\n", m.EventID) |
| 86 | fmt.Fprintf(&b, "Date: %s\r\n", time.Now().Format(time.RFC1123Z)) |
| 87 | fmt.Fprintf(&b, "Content-Type: text/plain; charset=utf-8\r\n\r\n%s\r\n", m.Body) |
| 88 | return b.Bytes() |
| 89 | } |