| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 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 <[email protected]>, Bob <[email protected]>, Eve <[email protected]>" |
| 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 |
// Output: |
| 28 |
// Alice [email protected] |
| 29 |
// Bob [email protected] |
| 30 |
// Eve [email protected] |
| 31 |
} |
| 32 |
|
| 33 |
func ExampleParseAddress() { |
| 34 |
e, err := mail.ParseAddress("Alice <[email protected]>") |
| 35 |
if err != nil { |
| 36 |
log.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
fmt.Println(e.Name, e.Address) |
| 40 |
|
| 41 |
// Output: |
| 42 |
// Alice [email protected] |
| 43 |
} |
| 44 |
|
| 45 |
func ExampleReadMessage() { |
| 46 |
msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400 |
| 47 |
From: Gopher <[email protected]> |
| 48 |
To: Another Gopher <[email protected]> |
| 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 |
// Output: |
| 73 |
// Date: Mon, 23 Jun 2015 11:40:36 -0400 |
| 74 |
// From: Gopher <[email protected]> |
| 75 |
// To: Another Gopher <[email protected]> |
| 76 |
// Subject: Gophers at Gophercon |
| 77 |
// Message body |
| 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 |
// Output: |
| 91 |
// 2024-10-09T09:55:06-07:00 |
| 92 |
} |