Phase 2 part 6: outreach by email and LinkedIn

Drafts are written by the AI from the person's buying signal (hypothesis
and angle), their company and their history, then reviewed and edited.
Queued emails go out through the Core's mail server one at a time, within
the sending hours, the daily limit and a gap, never to a suppressed or
bouncing address or someone emailed in the last 14 days; with a signature,
an opt-out line, UTM-tagged links and a copy in Sent. The inbox is read
read-only every 10 minutes: replies (by Message-ID or sender) count as
Replied, bounces mark the address, and "stop" replies are suppressed.
LinkedIn messages are copied, sent by hand and marked sent. New pages:
Prospects → Outreach and Settings → Email (with an SMTP, IMAP, SPF, DKIM
and DMARC check). Schema 10; emersion/go-imap for IMAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 06:09:47 -07:00
co-authored by Claude Opus 5.5
parent 82d569da9c
commit c2772a7927
26 changed files with 3079 additions and 3 deletions
+140
View File
@@ -0,0 +1,140 @@
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)
}