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