From 5c8d874a82d841c5bbe38f0f99b51dee263bee9e Mon Sep 17 00:00:00 2001 From: Dmytro Kasianenko Date: Wed, 16 Nov 2022 16:38:28 +0100 Subject: [PATCH] feat(dsn): add example --- dsn/example_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 dsn/example_test.go diff --git a/dsn/example_test.go b/dsn/example_test.go new file mode 100644 index 00000000..386941c1 --- /dev/null +++ b/dsn/example_test.go @@ -0,0 +1,39 @@ +package dsn_test + +import ( + "fmt" + "os" + + "github.com/jhillyerd/enmime" + "github.com/jhillyerd/enmime/dsn" +) + +// ExampleParseReport shows how to parse message as Delivery Status Notification (DSN). +func ExampleParseReport() { + f, err := os.Open("testdata/simple_dsn.raw") + if err != nil { + fmt.Println(err) + return + } + defer f.Close() + + env, err := enmime.ReadEnvelope(f) + if err != nil { + fmt.Print(err) + return + } + + rep, err := dsn.ParseReport(env.Root) + if err != nil { + fmt.Print(err) + return + } + + fmt.Printf("Original message: %s", rep.OriginalMessage) + fmt.Printf("Failed?: %t\n", dsn.IsFailed(rep.DeliveryStatus.RecipientDSNs[0])) + fmt.Printf("Why?: %s", rep.Explanation.Text) + // Output: + // Original message: [original message goes here] + // Failed?: true + // Why?: [human-readable explanation goes here] +}