| 1 |
package enmime_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/smtp" |
| 6 |
"os" |
| 7 |
"sort" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/jhillyerd/enmime" |
| 11 |
) |
| 12 |
|
| 13 |
// ExampleBuilder illustrates how to build and send a MIME encoded message. |
| 14 |
func ExampleBuilder() { |
| 15 |
// Create an SMTP Sender which relies on Go's built-in net/smtp package. Advanced users |
| 16 |
// may provide their own Sender, or mock it in unit tests. |
| 17 |
smtpHost := "smtp.relay.host:25" |
| 18 |
smtpAuth := smtp.PlainAuth("", "user", "pw", "host") |
| 19 |
sender := enmime.NewSMTP(smtpHost, smtpAuth) |
| 20 |
|
| 21 |
// MailBuilder is (mostly) immutable, each method below returns a new MailBuilder without |
| 22 |
// modifying the original. |
| 23 |
master := enmime.Builder(). |
| 24 |
From("Do Not Reply", "[email protected]"). |
| 25 |
Subject("Inbucket Newsletter"). |
| 26 |
Text([]byte("Text body")). |
| 27 |
HTML([]byte("<p>HTML body</p>")) |
| 28 |
|
| 29 |
// master is immutable, causing each msg below to have a single recipient. |
| 30 |
msg := master.To("Esteemed Customer", "[email protected]") |
| 31 |
msg.Send(sender) |
| 32 |
|
| 33 |
msg = master.To("Another Customer", "[email protected]") |
| 34 |
msg.Send(sender) |
| 35 |
} |
| 36 |
|
| 37 |
func ExampleReadEnvelope() { |
| 38 |
// Open a sample message file. |
| 39 |
r, err := os.Open("testdata/mail/qp-utf8-header.raw") |
| 40 |
if err != nil { |
| 41 |
fmt.Print(err) |
| 42 |
return |
| 43 |
} |
| 44 |
|
| 45 |
// Parse message body with enmime. |
| 46 |
env, err := enmime.ReadEnvelope(r) |
| 47 |
if err != nil { |
| 48 |
fmt.Print(err) |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
// Headers can be retrieved via Envelope.GetHeader(name). |
| 53 |
fmt.Printf("From: %v\n", env.GetHeader("From")) |
| 54 |
|
| 55 |
// Address-type headers can be parsed into a list of decoded mail.Address structs. |
| 56 |
alist, _ := env.AddressList("To") |
| 57 |
for _, addr := range alist { |
| 58 |
fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address) |
| 59 |
} |
| 60 |
|
| 61 |
// enmime can decode quoted-printable headers. |
| 62 |
fmt.Printf("Subject: %v\n", env.GetHeader("Subject")) |
| 63 |
|
| 64 |
// The plain text body is available as mime.Text. |
| 65 |
fmt.Printf("Text Body: %v chars\n", len(env.Text)) |
| 66 |
|
| 67 |
// The HTML body is stored in mime.HTML. |
| 68 |
fmt.Printf("HTML Body: %v chars\n", len(env.HTML)) |
| 69 |
|
| 70 |
// mime.Inlines is a slice of inlined attacments. |
| 71 |
fmt.Printf("Inlines: %v\n", len(env.Inlines)) |
| 72 |
|
| 73 |
// mime.Attachments contains the non-inline attachments. |
| 74 |
fmt.Printf("Attachments: %v\n", len(env.Attachments)) |
| 75 |
|
| 76 |
// Output: |
| 77 |
// From: James Hillyerd <[email protected]>, André Pirard <[email protected]> |
| 78 |
// To: Mirosław Marczak <[email protected]> |
| 79 |
// Subject: MIME UTF8 Test ¢ More Text |
| 80 |
// Text Body: 1300 chars |
| 81 |
// HTML Body: 1736 chars |
| 82 |
// Inlines: 0 |
| 83 |
// Attachments: 0 |
| 84 |
} |
| 85 |
|
| 86 |
// ExampleEnvelope demonstrates the relationship between Envelope and Parts. |
| 87 |
func ExampleEnvelope() { |
| 88 |
// Create sample message in memory |
| 89 |
raw := `From: [email protected] |
| 90 |
Subject: Example message |
| 91 |
Content-Type: multipart/alternative; boundary=Enmime-100 |
| 92 |
|
| 93 |
--Enmime-100 |
| 94 |
Content-Type: text/plain |
| 95 |
X-Comment: part1 |
| 96 |
|
| 97 |
hello! |
| 98 |
--Enmime-100 |
| 99 |
Content-Type: text/html |
| 100 |
X-Comment: part2 |
| 101 |
|
| 102 |
<b>hello!</b> |
| 103 |
--Enmime-100 |
| 104 |
Content-Type: text/plain |
| 105 |
Content-Disposition: attachment; |
| 106 |
filename=hi.txt |
| 107 |
X-Comment: part3 |
| 108 |
|
| 109 |
hello again! |
| 110 |
--Enmime-100-- |
| 111 |
` |
| 112 |
|
| 113 |
// Parse message body with enmime.ReadEnvelope |
| 114 |
r := strings.NewReader(raw) |
| 115 |
env, err := enmime.ReadEnvelope(r) |
| 116 |
if err != nil { |
| 117 |
fmt.Print(err) |
| 118 |
return |
| 119 |
} |
| 120 |
|
| 121 |
// The root Part contains the message header, which is also available via the |
| 122 |
// Envelope.GetHeader() method. |
| 123 |
fmt.Printf("Root Part Subject: %q\n", env.Root.Header.Get("Subject")) |
| 124 |
fmt.Printf("Envelope Subject: %q\n", env.GetHeader("Subject")) |
| 125 |
fmt.Println() |
| 126 |
|
| 127 |
// The text from part1 is consumed and placed into the Envelope.Text field. |
| 128 |
fmt.Printf("Text Content: %q\n", env.Text) |
| 129 |
|
| 130 |
// But part1 is also available as a child of the root Part. Only the headers may be accessed, |
| 131 |
// because the content has been consumed. |
| 132 |
part1 := env.Root.FirstChild |
| 133 |
fmt.Printf("Part 1 X-Comment: %q\n", part1.Header.Get("X-Comment")) |
| 134 |
fmt.Println() |
| 135 |
|
| 136 |
// The HTML from part2 is consumed and placed into the Envelope.HTML field. |
| 137 |
fmt.Printf("HTML Content: %q\n", env.HTML) |
| 138 |
|
| 139 |
// And part2 is available as the second child of the root Part. Only the headers may be |
| 140 |
// accessed, because the content has been consumed. |
| 141 |
part2 := env.Root.FirstChild.NextSibling |
| 142 |
fmt.Printf("Part 2 X-Comment: %q\n", part2.Header.Get("X-Comment")) |
| 143 |
fmt.Println() |
| 144 |
|
| 145 |
// Because part3 has a disposition of attachment, it is added to the Envelope.Attachments |
| 146 |
// slice |
| 147 |
fmt.Printf("Attachment 1 X-Comment: %q\n", env.Attachments[0].Header.Get("X-Comment")) |
| 148 |
|
| 149 |
// And is still available as the third child of the root Part |
| 150 |
part3 := env.Root.FirstChild.NextSibling.NextSibling |
| 151 |
fmt.Printf("Part 3 X-Comment: %q\n", part3.Header.Get("X-Comment")) |
| 152 |
|
| 153 |
// The content of Attachments, Inlines and OtherParts are available as a slice of bytes |
| 154 |
fmt.Printf("Part 3 Content: %q\n", part3.Content) |
| 155 |
|
| 156 |
// part3 contained a malformed header line, enmime has attached an Error to it |
| 157 |
p3error := part3.Errors[0] |
| 158 |
fmt.Println(p3error.Error()) |
| 159 |
fmt.Println() |
| 160 |
|
| 161 |
// All Part errors are collected and placed into Envelope.Errors |
| 162 |
fmt.Println("Envelope errors:") |
| 163 |
for _, e := range env.Errors { |
| 164 |
fmt.Println(e.Error()) |
| 165 |
} |
| 166 |
|
| 167 |
// Output: |
| 168 |
// Root Part Subject: "Example message" |
| 169 |
// Envelope Subject: "Example message" |
| 170 |
// |
| 171 |
// Text Content: "hello!" |
| 172 |
// Part 1 X-Comment: "part1" |
| 173 |
// |
| 174 |
// HTML Content: "<b>hello!</b>" |
| 175 |
// Part 2 X-Comment: "part2" |
| 176 |
// |
| 177 |
// Attachment 1 X-Comment: "part3" |
| 178 |
// Part 3 X-Comment: "part3" |
| 179 |
// Part 3 Content: "hello again!" |
| 180 |
// [W] Malformed Header: Continued line "filename=hi.txt" was not indented |
| 181 |
// |
| 182 |
// Envelope errors: |
| 183 |
// [W] Malformed Header: Continued line "filename=hi.txt" was not indented |
| 184 |
} |
| 185 |
|
| 186 |
func ExampleEnvelope_GetHeaderKeys() { |
| 187 |
// Open a sample message file. |
| 188 |
r, err := os.Open("testdata/mail/qp-utf8-header.raw") |
| 189 |
if err != nil { |
| 190 |
fmt.Print(err) |
| 191 |
return |
| 192 |
} |
| 193 |
|
| 194 |
// Parse message with enmime. |
| 195 |
env, err := enmime.ReadEnvelope(r) |
| 196 |
if err != nil { |
| 197 |
fmt.Print(err) |
| 198 |
return |
| 199 |
} |
| 200 |
|
| 201 |
// A list of headers is retrieved via Envelope.GetHeaderKeys(). |
| 202 |
headers := env.GetHeaderKeys() |
| 203 |
sort.Strings(headers) |
| 204 |
|
| 205 |
// Print each header, key and value. |
| 206 |
for _, header := range headers { |
| 207 |
fmt.Printf("%s: %v\n", header, env.GetHeader(header)) |
| 208 |
} |
| 209 |
|
| 210 |
// Output: |
| 211 |
// Content-Type: multipart/alternative; boundary="------------020203040006070307010003" |
| 212 |
// Date: Fri, 19 Oct 2012 12:22:49 -0700 |
| 213 |
// From: James Hillyerd <[email protected]>, André Pirard <[email protected]> |
| 214 |
// Message-Id: <[email protected]> |
| 215 |
// Mime-Version: 1.0 |
| 216 |
// Sender: André Pirard <[email protected]> |
| 217 |
// Subject: MIME UTF8 Test ¢ More Text |
| 218 |
// To: Mirosław Marczak <[email protected]> |
| 219 |
// User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 |
| 220 |
} |