package outreach import ( "context" "errors" "fmt" "strconv" "time" "supermarket/internal/db" "supermarket/internal/utm" ) // Sender sends composed emails. SMTP in the app; a fake in tests. type Sender interface { Send(ctx context.Context, e *Email) error // Saved puts a copy in the Sent folder; it may do nothing. Saved(ctx context.Context, e *Email, now time.Time) error } // Mailbox is the real sender: the Core's mail server. type Mailbox struct { Settings Settings Password string } func (m Mailbox) Send(ctx context.Context, e *Email) error { return Send(ctx, m.Settings, m.Password, e) } func (m Mailbox) Saved(ctx context.Context, e *Email, now time.Time) error { if !m.Settings.SaveToSent { return nil } return SaveToSent(ctx, m.Settings, m.Password, e, now) } // Window says whether emails may go out at this time, and if not, why. func (s Settings) Window(now time.Time) error { t := now.Local() if !s.Weekends && (t.Weekday() == time.Saturday || t.Weekday() == time.Sunday) { return errors.New("emails go out on weekdays") } if t.Hour() < s.StartHour || t.Hour() >= s.EndHour { return fmt.Errorf("emails go out between %d:00 and %d:00", s.StartHour, s.EndHour) } return nil } // Tags returns the UTM tags for a message's links to the company's sites: // the channel, "outreach", the signal's campaign tag and the message. func Tags(d *db.DB, m *Message) utm.Params { p := utm.Params{Source: m.Channel, Medium: "outreach", Content: "outreach-" + strconv.FormatInt(m.ID, 10)} if m.SignalID != 0 { _ = d.QueryRow(`SELECT slug FROM signals WHERE id = ?`, m.SignalID).Scan(&p.Campaign) } if p.Campaign == "" { p.Campaign = "outreach" } return p } // sendOne composes and sends one queued email, and records the result. func sendOne(ctx context.Context, d *db.DB, s Settings, snd Sender, tags *utm.Tagger, m *Message, now time.Time) error { if err := sendable(d, m); err != nil { _ = fail(d, m.ID, StatusFailed, err) return err } body := m.Body if tags != nil { body = tags.Text(body, Tags(d, m)) } e := Compose(s, m.Contact.Name(), m.To, m.Subject, body, newMessageID(m.ID, s.FromDomain()), now) if err := snd.Send(ctx, e); err != nil { _ = fail(d, m.ID, StatusFailed, err) return err } if err := markSent(d, m, e.MessageID, now); err != nil { return err } if err := snd.Saved(ctx, e, now); err != nil { // It went; only the copy is missing. _, _ = d.Exec(`UPDATE outreach SET error = ? WHERE id = ?`, "Sent, but no copy was saved in Sent: "+err.Error(), m.ID) } return nil } // SendDue sends the next queued email if it's time: within the sending // hours, under the daily limit, and the gap after the last one. It returns // the message it sent (or tried to), or nil. func SendDue(ctx context.Context, d *db.DB, s Settings, snd Sender, tags *utm.Tagger, now time.Time) (*Message, error) { if s.Window(now) != nil { return nil, nil } n, err := SentToday(d, now) if err != nil || n >= s.DailyLimit { return nil, err } last, err := lastSent(d) if err != nil { return nil, err } if !last.IsZero() && now.Sub(last) < time.Duration(s.GapMinutes)*time.Minute { return nil, nil } queue, err := List(d, StatusQueued, 1) if err != nil || len(queue) == 0 { return nil, err } m := &queue[0] return m, sendOne(ctx, d, s, snd, tags, m, now) } // SendNow sends one queued email at once, outside the sending hours and // the gap, but still within the daily limit. func SendNow(ctx context.Context, d *db.DB, s Settings, snd Sender, tags *utm.Tagger, id int64, now time.Time) error { m, err := Get(d, id) if err != nil { return err } if m.Status != StatusQueued { return errors.New("queue it first") } n, err := SentToday(d, now) if err != nil { return err } if n >= s.DailyLimit { return fmt.Errorf("today's limit of %d emails is reached; it goes out tomorrow", s.DailyLimit) } return sendOne(ctx, d, s, snd, tags, m, now) } // SendTest sends a test email to the From address itself. func SendTest(ctx context.Context, s Settings, snd Sender, now time.Time) error { e := Compose(s, s.FromName, s.FromAddress, "Supermarket test email", "This is a test from Supermarket. If you can read it, outreach emails can be sent from this mailbox.", newMessageID(0, s.FromDomain()), now) return snd.Send(ctx, e) }