13 "github.com/mjl-/mox/dns"
14 "github.com/mjl-/mox/message"
15 "github.com/mjl-/mox/smtp"
18func ExampleDecodeReader() {
19 // Convert from iso-8859-1 to utf-8.
20 input := []byte{'t', 0xe9, 's', 't'}
21 output, err := io.ReadAll(message.DecodeReader("iso-8859-1", bytes.NewReader(input)))
23 log.Fatalf("read from decoder: %v", err)
25 fmt.Printf("%s\n", string(output))
29func ExampleMessageIDCanonical() {
31 msgid, invalidAddress, err := message.MessageIDCanonical("<ok@localhost>")
33 fmt.Printf("invalid message-id: %v\n", err)
35 fmt.Printf("canonical: %s %v\n", msgid, invalidAddress)
39 msgid, invalidAddress, err = message.MessageIDCanonical("bogus@localhost")
41 fmt.Printf("invalid message-id: %v\n", err)
43 fmt.Printf("canonical: %s %v\n", msgid, invalidAddress)
46 // Invalid address, but returned as not being in error.
47 msgid, invalidAddress, err = message.MessageIDCanonical("<invalid>")
49 fmt.Printf("invalid message-id: %v\n", err)
51 fmt.Printf("canonical: %s %v\n", msgid, invalidAddress)
55 // canonical: ok@localhost false
56 // invalid message-id: not a message-id: missing <
57 // canonical: invalid true
60func ExampleThreadSubject() {
62 s, isResp := message.ThreadSubject("nothing special", false)
63 fmt.Printf("%s, response: %v\n", s, isResp)
65 // List tags and "re:" are stripped.
66 s, isResp = message.ThreadSubject("[list1] [list2] Re: test", false)
67 fmt.Printf("%s, response: %v\n", s, isResp)
69 // "fwd:" is stripped.
70 s, isResp = message.ThreadSubject("fwd: a forward", false)
71 fmt.Printf("%s, response: %v\n", s, isResp)
73 // Trailing "(fwd)" is also a forward.
74 s, isResp = message.ThreadSubject("another forward (fwd)", false)
75 fmt.Printf("%s, response: %v\n", s, isResp)
77 // [fwd: ...] is stripped.
78 s, isResp = message.ThreadSubject("[fwd: [list] fwd: re: it's complicated]", false)
79 fmt.Printf("%s, response: %v\n", s, isResp)
82 // nothing special, response: false
83 // test, response: true
84 // a forward, response: true
85 // another forward, response: true
86 // it's complicated, response: true
89func ExampleComposer() {
90 // We store in a buffer. We could also write to a file.
93 // NewComposer. Keep in mind that operations on a Composer will panic on error.
94 const smtputf8 = false
95 xc := message.NewComposer(&b, 10*1024*1024, smtputf8)
97 // Catch and handle errors when composing.
103 if err, ok := x.(error); ok && errors.Is(err, message.ErrCompose) {
104 log.Printf("compose: %v", err)
109 // Add an address header.
110 xc.HeaderAddrs("From", []message.NameAddress{{DisplayName: "Charlie", Address: smtp.NewAddress("root", dns.Domain{ASCII: "localhost"})}})
112 // Add subject header, with encoding
115 // Add Date and Message-ID headers, required.
116 tm, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
117 xc.Header("Date", tm.Format(message.RFC5322Z))
118 xc.Header("Message-ID", "<unique@host>") // Should generate unique id for each message.
120 xc.Header("MIME-Version", "1.0")
122 // Write content-* headers for the text body.
123 body, ct, cte := xc.TextPart("plain", "this is the body")
124 xc.Header("Content-Type", ct)
125 xc.Header("Content-Transfer-Encoding", cte)
127 // Header/Body separator
130 // The part body. Use mime/multipart to make messages with multiple parts.
133 // Flush any buffered writes to the original writer.
136 fmt.Println(strings.ReplaceAll(b.String(), "\r\n", "\n"))
138 // From: "Charlie" <root@localhost>
139 // Subject: hi =?utf-8?q?=E2=98=BA?=
140 // Date: 2 Jan 2006 15:04:05 +0700
141 // Message-ID: <unique@host>
143 // Content-Type: text/plain; charset=us-ascii
144 // Content-Transfer-Encoding: 7bit
150 // Parse a message from an io.ReaderAt, which could be a file.
152 r := strings.NewReader("header: value\r\nanother: value\r\n\r\nbody ...\r\n")
153 part, err := message.Parse(slog.Default(), strict, r)
155 log.Fatalf("parsing message: %v", err)
158 // The headers of the first part have been parsed, i.e. the message headers.
159 // A message can be multipart (e.g. alternative, related, mixed), and possibly
162 // By walking the entire message, all part metadata (like offsets into the file
163 // where a part starts) is recorded.
164 err = part.Walk(slog.Default(), nil)
166 log.Fatalf("walking message: %v", err)
169 // Messages can have a recursive multipart structure. Print the structure.
170 var printPart func(indent string, p message.Part)
171 printPart = func(indent string, p message.Part) {
172 log.Printf("%s- part: %v", indent, part)
173 for _, pp := range p.Parts {
174 printPart(" "+indent, pp)
180func ExampleWriter() {
181 // NewWriter on a string builder.
182 var b strings.Builder
183 w := message.NewWriter(&b)
185 // Write some lines, some with proper CRLF line ending, others without.
186 fmt.Fprint(w, "header: value\r\n")
187 fmt.Fprint(w, "another: value\n") // missing \r
188 fmt.Fprint(w, "\r\n")
189 fmt.Fprint(w, "hi ☺\n") // missing \r
191 fmt.Printf("%q\n", b.String())
192 fmt.Printf("%v %v", w.HaveBody, w.Has8bit)
194 // "header: value\r\nanother: value\r\n\r\nhi ☺\r\n"