github.com/mailgun/mailgun-go/v3@v3.6.4/examples_test.go (about) 1 package mailgun_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "log" 9 "net/http" 10 "os" 11 "strings" 12 "time" 13 14 "github.com/mailgun/mailgun-go/v3" 15 "github.com/mailgun/mailgun-go/v3/events" 16 ) 17 18 func ExampleMailgunImpl_ValidateEmail() { 19 v := mailgun.NewEmailValidator("my_public_validation_api_key") 20 21 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 22 defer cancel() 23 24 ev, err := v.ValidateEmail(ctx, "joe@example.com", false) 25 if err != nil { 26 log.Fatal(err) 27 } 28 if !ev.IsValid { 29 log.Fatal("Expected valid e-mail address") 30 } 31 log.Printf("Parts local_part=%s domain=%s display_name=%s", ev.Parts.LocalPart, ev.Parts.Domain, ev.Parts.DisplayName) 32 if ev.DidYouMean != "" { 33 log.Printf("The address is syntactically valid, but perhaps has a typo.") 34 log.Printf("Did you mean %s instead?", ev.DidYouMean) 35 } 36 } 37 38 func ExampleMailgunImpl_ParseAddresses() { 39 v := mailgun.NewEmailValidator("my_public_validation_api_key") 40 41 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 42 defer cancel() 43 44 addressesThatParsed, unparsableAddresses, err := v.ParseAddresses(ctx, "Alice <alice@example.com>", "bob@example.com", "example.com") 45 if err != nil { 46 log.Fatal(err) 47 } 48 hittest := map[string]bool{ 49 "Alice <alice@example.com>": true, 50 "bob@example.com": true, 51 } 52 for _, a := range addressesThatParsed { 53 if !hittest[a] { 54 log.Fatalf("Expected %s to be parsable", a) 55 } 56 } 57 if len(unparsableAddresses) != 1 { 58 log.Fatalf("Expected 1 address to be unparsable; got %d", len(unparsableAddresses)) 59 } 60 } 61 62 func ExampleMailgunImpl_UpdateList() { 63 mg := mailgun.NewMailgun("example.com", "my_api_key") 64 65 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 66 defer cancel() 67 68 _, err := mg.UpdateMailingList(ctx, "joe-stat@example.com", mailgun.MailingList{ 69 Name: "Joe Stat", 70 Description: "Joe's status report list", 71 }) 72 if err != nil { 73 log.Fatal(err) 74 } 75 } 76 77 func ExampleMailgunImpl_Send_constructed() { 78 mg := mailgun.NewMailgun("example.com", "my_api_key") 79 80 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 81 defer cancel() 82 83 m := mg.NewMessage( 84 "Excited User <me@example.com>", 85 "Hello World", 86 "Testing some Mailgun Awesomeness!", 87 "baz@example.com", 88 "bar@example.com", 89 ) 90 m.SetTracking(true) 91 m.SetDeliveryTime(time.Now().Add(24 * time.Hour)) 92 m.SetHtml("<html><body><h1>Testing some Mailgun Awesomeness!!</h1></body></html>") 93 _, id, err := mg.Send(ctx, m) 94 if err != nil { 95 log.Fatal(err) 96 } 97 log.Printf("Message id=%s", id) 98 } 99 100 func ExampleMailgunImpl_Send_mime() { 101 exampleMime := `Content-Type: text/plain; charset="ascii" 102 Subject: Joe's Example Subject 103 From: Joe Example <joe@example.com> 104 To: BARGLEGARF <bargle.garf@example.com> 105 Content-Transfer-Encoding: 7bit 106 Date: Thu, 6 Mar 2014 00:37:52 +0000 107 108 Testing some Mailgun MIME awesomeness! 109 ` 110 111 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 112 defer cancel() 113 114 mg := mailgun.NewMailgun("example.com", "my_api_key") 115 m := mg.NewMIMEMessage(ioutil.NopCloser(strings.NewReader(exampleMime)), "bargle.garf@example.com") 116 _, id, err := mg.Send(ctx, m) 117 if err != nil { 118 log.Fatal(err) 119 } 120 log.Printf("Message id=%s", id) 121 } 122 123 func ExampleMailgunImpl_GetRoutes() { 124 mg := mailgun.NewMailgun("example.com", "my_api_key") 125 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) 126 defer cancel() 127 128 it := mg.ListRoutes(nil) 129 var page []mailgun.Route 130 for it.Next(ctx, &page) { 131 for _, r := range page { 132 log.Printf("Route pri=%d expr=%s desc=%s", r.Priority, r.Expression, r.Description) 133 } 134 } 135 if it.Err() != nil { 136 log.Fatal(it.Err()) 137 } 138 } 139 140 func ExampleMailgunImpl_VerifyWebhookSignature() { 141 // Create an instance of the Mailgun Client 142 mg, err := mailgun.NewMailgunFromEnv() 143 if err != nil { 144 fmt.Printf("mailgun error: %s\n", err) 145 os.Exit(1) 146 } 147 148 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 149 150 var payload mailgun.WebhookPayload 151 if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { 152 fmt.Printf("decode JSON error: %s", err) 153 w.WriteHeader(http.StatusNotAcceptable) 154 return 155 } 156 157 verified, err := mg.VerifyWebhookSignature(payload.Signature) 158 if err != nil { 159 fmt.Printf("verify error: %s\n", err) 160 w.WriteHeader(http.StatusNotAcceptable) 161 return 162 } 163 164 if !verified { 165 w.WriteHeader(http.StatusNotAcceptable) 166 fmt.Printf("failed verification %+v\n", payload.Signature) 167 return 168 } 169 170 fmt.Printf("Verified Signature\n") 171 172 // Parse the raw event to extract the 173 174 e, err := mailgun.ParseEvent(payload.EventData) 175 if err != nil { 176 fmt.Printf("parse event error: %s\n", err) 177 return 178 } 179 180 switch event := e.(type) { 181 case *events.Accepted: 182 fmt.Printf("Accepted: auth: %t\n", event.Flags.IsAuthenticated) 183 case *events.Delivered: 184 fmt.Printf("Delivered transport: %s\n", event.Envelope.Transport) 185 } 186 }) 187 188 fmt.Println("Running...") 189 if err := http.ListenAndServe(":9090", nil); err != nil { 190 fmt.Printf("serve error: %s\n", err) 191 os.Exit(1) 192 } 193 }