···27522752func (m Model) launchReplyWithCC(extraCC string, replyAll bool) (tea.Model, tea.Cmd) {
27532753 e := m.openEmail
2754275427552755+ // Auto-select the From address that matches the email's To/CC field.
27562756+ // E.g. if email was sent to simon@ssp.sh, reply from simon@ssp.sh.
27572757+ if idx := m.matchFromIndex(e.To, e.CC); idx >= 0 {
27582758+ m.presendFromI = idx
27592759+ }
27602760+27552761 // Use Reply-To if present, else From
27562762 to := e.ReplyTo
27572763 if to == "" {
···28252831 }
28262832 return editorDoneMsg{to: pto, cc: pcc, bcc: "", subject: psubject, body: string(raw)}
28272833 })
28342834+}
28352835+28362836+// matchFromIndex returns the presendFroms() index whose email address matches
28372837+// one of the addresses in the email's To or CC fields. Returns -1 if no match.
28382838+// This auto-selects the correct From when replying to an email sent to an alias.
28392839+func (m Model) matchFromIndex(toField, ccField string) int {
28402840+ froms := m.presendFroms()
28412841+ recipients := make(map[string]bool)
28422842+ for _, addr := range splitAddrs(toField + "," + ccField) {
28432843+ if a := strings.ToLower(extractEmailAddr(addr)); a != "" {
28442844+ recipients[a] = true
28452845+ }
28462846+ }
28472847+ for i, from := range froms {
28482848+ if recipients[strings.ToLower(extractEmailAddr(from))] {
28492849+ return i
28502850+ }
28512851+ }
28522852+ return -1
28282853}
2829285428302855// extractEmailAddr returns the bare email address from "Name <addr>" or "addr".