Source file
src/net/mail/example_test.go
1
2
3
4
5 package mail_test
6
7 import (
8 "fmt"
9 "io"
10 "log"
11 "net/mail"
12 "strings"
13 "time"
14 )
15
16 func ExampleParseAddressList() {
17 const list = "Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>"
18 emails, err := mail.ParseAddressList(list)
19 if err != nil {
20 log.Fatal(err)
21 }
22
23 for _, v := range emails {
24 fmt.Println(v.Name, v.Address)
25 }
26
27
28
29
30
31 }
32
33 func ExampleParseAddress() {
34 e, err := mail.ParseAddress("Alice <alice@example.com>")
35 if err != nil {
36 log.Fatal(err)
37 }
38
39 fmt.Println(e.Name, e.Address)
40
41
42
43 }
44
45 func ExampleReadMessage() {
46 msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400
47 From: Gopher <from@example.com>
48 To: Another Gopher <to@example.com>
49 Subject: Gophers at Gophercon
50
51 Message body
52 `
53
54 r := strings.NewReader(msg)
55 m, err := mail.ReadMessage(r)
56 if err != nil {
57 log.Fatal(err)
58 }
59
60 header := m.Header
61 fmt.Println("Date:", header.Get("Date"))
62 fmt.Println("From:", header.Get("From"))
63 fmt.Println("To:", header.Get("To"))
64 fmt.Println("Subject:", header.Get("Subject"))
65
66 body, err := io.ReadAll(m.Body)
67 if err != nil {
68 log.Fatal(err)
69 }
70 fmt.Printf("%s", body)
71
72
73
74
75
76
77
78 }
79
80 func ExampleParseDate() {
81 dateStr := "Wed, 09 Oct 2024 09:55:06 -0700"
82
83 t, err := mail.ParseDate(dateStr)
84 if err != nil {
85 log.Fatalf("Failed to parse date: %v", err)
86 }
87
88 fmt.Println(t.Format(time.RFC3339))
89
90
91
92 }
93
View as plain text