// Package outreach writes to prospects directly: emails sent through the // Core's mail server and LinkedIn messages the user sends by hand. Every // message is drafted from its buying signal's angle, reviewed, and only // then sent, one at a time within a daily limit and working hours. Replies, // bounces and requests to stop are read from the mailbox (read-only) and // recorded in the funnel. package outreach import ( "errors" "strings" "supermarket/internal/core" "supermarket/internal/db" "supermarket/internal/profile" ) // Settings are the mailbox and the sending rules. type Settings struct { SMTPHost string `json:"smtp_host"` SMTPPort int `json:"smtp_port"` // 587 (STARTTLS) or 465 (TLS) IMAPHost string `json:"imap_host"` IMAPPort int `json:"imap_port"` // 993 (TLS) Username string `json:"username"` FromName string `json:"from_name"` FromAddress string `json:"from_address"` DailyLimit int `json:"daily_limit"` GapMinutes int `json:"gap_minutes"` // at least this long between two emails StartHour int `json:"start_hour"` // send from (local time) EndHour int `json:"end_hour"` // until Weekends bool `json:"weekends"` Footer string `json:"footer"` // added under every email SchedulingLink string `json:"scheduling_link"` // offered as the next step, if set SaveToSent bool `json:"save_to_sent"` // put a copy in the mailbox's Sent folder ReadReplies bool `json:"read_replies"` // BookingFrom is the address Cal.com sends booking notices from; its // emails in the inbox count as bookings. BookingFrom string `json:"booking_from"` // Insecure allows a mail server without TLS. Only tests set it. Insecure bool `json:"-"` } const settingsKey = "outreach:settings" // Defaults suggest the Core's mail server and the user's own address. func Defaults(c *core.Core, p *profile.Profile) Settings { s := Settings{ SMTPPort: 587, IMAPPort: 993, DailyLimit: 20, GapMinutes: 4, StartHour: 9, EndHour: 17, SaveToSent: true, ReadReplies: true, } if c != nil { if c.Domain != "" { s.SMTPHost, s.IMAPHost = "mail."+c.Domain, "mail."+c.Domain } s.Username, s.FromAddress, s.FromName = c.Email, c.Email, c.Name } s.Footer = defaultFooter(s.FromName, p) return s } func defaultFooter(name string, p *profile.Profile) string { var lines []string if name != "" { lines = append(lines, name) } if p != nil { if p.Name != "" { lines = append(lines, p.Name) } if len(p.Websites) > 0 && p.Websites[0].URL != "" { lines = append(lines, p.Websites[0].URL) } } lines = append(lines, "", `If you'd rather not hear from me, reply "stop" and I won't write again.`) return strings.Join(lines, "\n") } // Load returns the saved settings, or the defaults. func Load(d *db.DB, c *core.Core, p *profile.Profile) (Settings, bool, error) { s := Defaults(c, p) ok, err := d.GetJSON(settingsKey, &s) return s, ok, err } // Save stores the settings, after checking them. func Save(d *db.DB, s Settings) error { if err := s.Check(); err != nil { return err } return d.PutJSON(settingsKey, s) } // Check finds settings that can't work. func (s *Settings) Check() error { for _, f := range []*string{&s.SMTPHost, &s.IMAPHost, &s.Username, &s.FromName, &s.FromAddress, &s.SchedulingLink, &s.BookingFrom} { *f = strings.TrimSpace(*f) } s.FromAddress, s.BookingFrom = strings.ToLower(s.FromAddress), strings.ToLower(s.BookingFrom) switch { case s.FromAddress != "" && !strings.Contains(s.FromAddress, "@"): return errors.New("the From address isn't an email address") case s.BookingFrom != "" && !strings.Contains(s.BookingFrom, "@"): return errors.New("Cal.com's sending address isn't an email address") case s.SMTPPort < 1 || s.SMTPPort > 65535 || s.IMAPPort < 1 || s.IMAPPort > 65535: return errors.New("a port must be between 1 and 65535") case s.DailyLimit < 1 || s.DailyLimit > 200: return errors.New("the daily limit must be between 1 and 200") case s.GapMinutes < 0 || s.GapMinutes > 120: return errors.New("the gap must be between 0 and 120 minutes") case s.StartHour < 0 || s.EndHour > 24 || s.StartHour >= s.EndHour: return errors.New("the sending hours must start before they end") case s.SchedulingLink != "" && !strings.HasPrefix(s.SchedulingLink, "https://") && !strings.HasPrefix(s.SchedulingLink, "http://"): return errors.New("the scheduling link must start with https://") } return nil } // Ready says why emails can't be sent yet, or nil. func (s Settings) Ready(password string) error { switch { case s.SMTPHost == "": return errors.New("give the mail server under Settings → Email") case s.Username == "" || password == "": return errors.New("give the mailbox's user name and password under Settings → Email") case s.FromAddress == "": return errors.New("give the From address under Settings → Email") } return nil } // FromDomain is the domain of the From address. func (s Settings) FromDomain() string { if i := strings.LastIndex(s.FromAddress, "@"); i >= 0 { return s.FromAddress[i+1:] } return "" }