1package message
2
3import (
4 "fmt"
5 "io"
6 "net/textproto"
7
8 "github.com/mjl-/mox/dns"
9 "github.com/mjl-/mox/mlog"
10 "github.com/mjl-/mox/smtp"
11)
12
13// From extracts the address in the From-header.
14//
15// An RFC5322 message must have a From header.
16// In theory, multiple addresses may be present. In practice zero or multiple
17// From headers may be present. From returns an error if there is not exactly
18// one address. This address can be used for evaluating a DMARC policy against
19// SPF and DKIM results.
20func From(log *mlog.Log, strict bool, r io.ReaderAt) (raddr smtp.Address, header textproto.MIMEHeader, rerr error) {
21 // ../rfc/7489:1243
22
23 // todo: only allow utf8 if enabled in session/message?
24
25 p, err := Parse(log, strict, r)
26 if err != nil {
27 // todo: should we continue with p, perhaps headers can be parsed?
28 return raddr, nil, fmt.Errorf("parsing message: %v", err)
29 }
30 header, err = p.Header()
31 if err != nil {
32 return raddr, nil, fmt.Errorf("parsing message header: %v", err)
33 }
34 from := p.Envelope.From
35 if len(from) != 1 {
36 return raddr, nil, fmt.Errorf("from header has %d addresses, need exactly 1 address", len(from))
37 }
38 d, err := dns.ParseDomain(from[0].Host)
39 if err != nil {
40 return raddr, nil, fmt.Errorf("bad domain in from address: %v", err)
41 }
42 addr := smtp.Address{Localpart: smtp.Localpart(from[0].User), Domain: d}
43 return addr, textproto.MIMEHeader(header), nil
44}
45