diff options
| author | Shulhan <ms@kilabit.info> | 2023-02-25 18:52:13 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-02-25 19:56:24 +0700 |
| commit | 4100f2dbd433ec885c3c9f5eb963ba98bb1ab1e4 (patch) | |
| tree | 3c72befdbda2fe4bf52fd7bb3326510eb1329550 /lib | |
| parent | 1b7acc58d0169b049802dba38b16ed7f3d7172a3 (diff) | |
| download | pakakeh.go-4100f2dbd433ec885c3c9f5eb963ba98bb1ab1e4.tar.xz | |
lib/email: add an example for Filter method on Header
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/email/header_example_test.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/email/header_example_test.go b/lib/email/header_example_test.go new file mode 100644 index 00000000..2f4e36c4 --- /dev/null +++ b/lib/email/header_example_test.go @@ -0,0 +1,62 @@ +package email_test + +import ( + "fmt" + "log" + + "github.com/shuLhan/share/lib/email" +) + +func ExampleHeader_Filter() { + // Overwrite the email.Epoch to make the example works. + email.Epoch = func() int64 { + return 1645600000 + } + + var ( + fromAddress = []byte("Noreply <noreply@example.com>") + toAddresses = []byte("John <john@example.com>, Jane <jane@example.com>") + subject = []byte(`Example subject`) + bodyText = []byte(`Email body as plain text`) + bodyHtml = []byte(`Email body as <b>HTML</b>`) + + recipients []string + mboxes []*email.Mailbox + msg *email.Message + err error + ) + + mboxes, err = email.ParseMailboxes(toAddresses) + if err != nil { + log.Fatal(err) + } + for _, mbox := range mboxes { + recipients = append(recipients, mbox.Address) + } + + msg, err = email.NewMultipart( + fromAddress, + toAddresses, + subject, + bodyText, + bodyHtml, + ) + if err != nil { + log.Fatal(err) + } + + _, _ = msg.Pack() + + var ( + fields []*email.Field + field *email.Field + ) + + fields = msg.Header.Filter(email.FieldTypeFrom) + + for _, field = range fields { + fmt.Printf("%s: %s\n", field.Name, field.Value) + } + // Output: + // from: Noreply <noreply@example.com> +} |
