github.com/mailgun/mailgun-go/v3@v3.6.4/examples/examples.go (about)

     1  package examples
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/mailgun/mailgun-go/v3"
     7  	"github.com/mailgun/mailgun-go/v3/events"
     8  	"os"
     9  	"time"
    10  )
    11  
    12  func AddBounce(domain, apiKey string) error {
    13  	mg := mailgun.NewMailgun(domain, apiKey)
    14  
    15  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    16  	defer cancel()
    17  
    18  	return mg.AddBounce(ctx, "bob@example.com", "550", "Undeliverable message error")
    19  }
    20  
    21  func CreateComplaint(domain, apiKey string) error {
    22  	mg := mailgun.NewMailgun(domain, apiKey)
    23  
    24  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    25  	defer cancel()
    26  
    27  	return mg.CreateComplaint(ctx, "bob@example.com")
    28  }
    29  
    30  func AddDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
    31  	mg := mailgun.NewMailgun(domain, apiKey)
    32  
    33  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    34  	defer cancel()
    35  
    36  	return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{
    37  		Password:   "super_secret",
    38  		SpamAction: mailgun.SpamActionTag,
    39  		Wildcard:   false,
    40  	})
    41  }
    42  
    43  func AddDomainIPS(domain, apiKey string) error {
    44  	mg := mailgun.NewMailgun(domain, apiKey)
    45  
    46  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    47  	defer cancel()
    48  
    49  	return mg.AddDomainIP(ctx, "127.0.0.1")
    50  }
    51  
    52  func AddListMember(domain, apiKey string) error {
    53  	mg := mailgun.NewMailgun(domain, apiKey)
    54  
    55  	memberJoe := mailgun.Member{
    56  		Address:    "joe@example.com",
    57  		Name:       "Joe Example",
    58  		Subscribed: mailgun.Subscribed,
    59  	}
    60  
    61  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    62  	defer cancel()
    63  
    64  	return mg.CreateMember(ctx, true, "mailingList@example.com", memberJoe)
    65  }
    66  
    67  func AddListMembers(domain, apiKey string) error {
    68  	mg := mailgun.NewMailgun(domain, apiKey)
    69  
    70  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    71  	defer cancel()
    72  
    73  	return mg.CreateMemberList(ctx, nil, "mailgunList@example.com", []interface{}{
    74  		mailgun.Member{
    75  			Address:    "alice@example.com",
    76  			Name:       "Alice's debugging account",
    77  			Subscribed: mailgun.Unsubscribed,
    78  		},
    79  		mailgun.Member{
    80  			Address:    "Bob Cool <bob@example.com>",
    81  			Name:       "Bob's Cool Account",
    82  			Subscribed: mailgun.Subscribed,
    83  		},
    84  		mailgun.Member{
    85  			Address: "joe.hamradio@example.com",
    86  			// Charlette is a ham radio packet BBS user.
    87  			// We attach her packet BBS e-mail address as an arbitrary var here.
    88  			Vars: map[string]interface{}{
    89  				"packet-email": "KW9ABC @ BOGUS-4.#NCA.CA.USA.NOAM",
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func CreateUnsubscribe(domain, apiKey string) error {
    96  	mg := mailgun.NewMailgun(domain, apiKey)
    97  
    98  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    99  	defer cancel()
   100  
   101  	return mg.CreateUnsubscribe(ctx, "bob@example.com", "*")
   102  }
   103  
   104  func CreateUnsubscribeWithTag(domain, apiKey string) error {
   105  	mg := mailgun.NewMailgun(domain, apiKey)
   106  
   107  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   108  	defer cancel()
   109  
   110  	return mg.CreateUnsubscribe(ctx, "bob@example.com", "tag1")
   111  }
   112  
   113  func CreateWebhook(domain, apiKey string) error {
   114  	mg := mailgun.NewMailgun(domain, apiKey)
   115  
   116  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   117  	defer cancel()
   118  
   119  	return mg.CreateWebhook(ctx, "clicked", []string{"https://your_domain.com/v1/clicked"})
   120  }
   121  
   122  func ChangePassword(domain, apiKey string) error {
   123  	mg := mailgun.NewMailgun(domain, apiKey)
   124  
   125  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   126  	defer cancel()
   127  
   128  	return mg.ChangeCredentialPassword(ctx, "alice", "super_secret")
   129  }
   130  
   131  func CreateCredential(domain, apiKey string) error {
   132  	mg := mailgun.NewMailgun(domain, apiKey)
   133  
   134  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   135  	defer cancel()
   136  
   137  	return mg.CreateCredential(ctx, "alice@example.com", "secret")
   138  }
   139  
   140  func CreateDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
   141  	mg := mailgun.NewMailgun(domain, apiKey)
   142  
   143  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   144  	defer cancel()
   145  
   146  	return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{
   147  		Password:   "super_secret",
   148  		SpamAction: mailgun.SpamActionTag,
   149  		Wildcard:   false,
   150  	})
   151  }
   152  
   153  func CreateExport(domain, apiKey string) error {
   154  	mg := mailgun.NewMailgun(domain, apiKey)
   155  
   156  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   157  	defer cancel()
   158  
   159  	return mg.CreateExport(ctx, "/v3/domains")
   160  }
   161  
   162  func CreateMailingList(domain, apiKey string) (mailgun.MailingList, error) {
   163  	mg := mailgun.NewMailgun(domain, apiKey)
   164  
   165  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   166  	defer cancel()
   167  
   168  	return mg.CreateMailingList(ctx, mailgun.MailingList{
   169  		Address:     "list@example.com",
   170  		Name:        "dev",
   171  		Description: "Mailgun developers list.",
   172  		AccessLevel: mailgun.AccessLevelMembers,
   173  	})
   174  }
   175  
   176  func CreateRoute(domain, apiKey string) (mailgun.Route, error) {
   177  	mg := mailgun.NewMailgun(domain, apiKey)
   178  
   179  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   180  	defer cancel()
   181  
   182  	return mg.CreateRoute(ctx, mailgun.Route{
   183  		Priority:    1,
   184  		Description: "Sample Route",
   185  		Expression:  "match_recipient(\".*@YOUR_DOMAIN_NAME\")",
   186  		Actions: []string{
   187  			"forward(\"http://example.com/messages/\")",
   188  			"stop()",
   189  		},
   190  	})
   191  }
   192  
   193  func DeleteCredential(domain, apiKey string) error {
   194  	mg := mailgun.NewMailgun(domain, apiKey)
   195  
   196  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   197  	defer cancel()
   198  
   199  	return mg.DeleteCredential(ctx, "alice")
   200  }
   201  
   202  func DeleteDomain(domain, apiKey string) error {
   203  	mg := mailgun.NewMailgun(domain, apiKey)
   204  
   205  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   206  	defer cancel()
   207  
   208  	return mg.DeleteDomain(ctx, "example.com")
   209  }
   210  
   211  func DeleteTag(domain, apiKey string) error {
   212  	mg := mailgun.NewMailgun(domain, apiKey)
   213  
   214  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   215  	defer cancel()
   216  
   217  	return mg.DeleteTag(ctx, "newsletter")
   218  }
   219  
   220  func DeleteWebhook(domain, apiKey string) error {
   221  	mg := mailgun.NewMailgun(domain, apiKey)
   222  
   223  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   224  	defer cancel()
   225  
   226  	return mg.DeleteWebhook(ctx, "clicked")
   227  }
   228  
   229  func PrintEventLog(domain, apiKey string) error {
   230  	mg := mailgun.NewMailgun(domain, apiKey)
   231  
   232  	// Create an iterator
   233  	it := mg.ListEvents(&mailgun.ListEventOptions{
   234  		Begin: time.Now().Add(-50 * time.Minute),
   235  		Limit: 100,
   236  		Filter: map[string]string{
   237  			"recipient": "joe@example.com",
   238  		},
   239  	})
   240  
   241  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   242  	defer cancel()
   243  
   244  	// Iterate through all the pages of events
   245  	var page []mailgun.Event
   246  	for it.Next(ctx, &page) {
   247  		for _, event := range page {
   248  			fmt.Printf("%+v\n", event)
   249  		}
   250  	}
   251  
   252  	// Did iteration end because of an error?
   253  	if it.Err() != nil {
   254  		return it.Err()
   255  	}
   256  
   257  	return nil
   258  }
   259  
   260  func PrintFailedEvents(domain, apiKey string) error {
   261  	mg := mailgun.NewMailgun(domain, apiKey)
   262  
   263  	// Create an iterator
   264  	it := mg.ListEvents(&mailgun.ListEventOptions{
   265  		Filter: map[string]string{
   266  			"event": "rejected OR failed",
   267  		},
   268  	})
   269  
   270  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   271  	defer cancel()
   272  
   273  	// Iterate through all the pages of events
   274  	var page []mailgun.Event
   275  	for it.Next(ctx, &page) {
   276  		for _, event := range page {
   277  			switch e := event.(type) {
   278  			case *events.Failed:
   279  				fmt.Printf("Failed Reason: %s", e.Reason)
   280  			case *events.Rejected:
   281  				fmt.Printf("Rejected Reason: %s", e.Reject.Reason)
   282  			}
   283  		}
   284  	}
   285  
   286  	// Did iteration end because of an error?
   287  	if it.Err() != nil {
   288  		return it.Err()
   289  	}
   290  	return nil
   291  }
   292  
   293  func PrintEvents(domain, apiKey string) error {
   294  	mg := mailgun.NewMailgun(domain, apiKey)
   295  
   296  	// Create an iterator
   297  	it := mg.ListEvents(nil)
   298  
   299  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   300  	defer cancel()
   301  
   302  	// Iterate through all the pages of events
   303  	var page []mailgun.Event
   304  	for it.Next(ctx, &page) {
   305  		for _, event := range page {
   306  			switch e := event.(type) {
   307  			case *events.Accepted:
   308  				fmt.Printf("Accepted ID: %s", e.Message.Headers.MessageID)
   309  			case *events.Rejected:
   310  				fmt.Printf("Rejected Reason: %s", e.Reject.Reason)
   311  				// Add other event types here
   312  			}
   313  			fmt.Printf("%+v\n", event.GetTimestamp())
   314  		}
   315  	}
   316  
   317  	// Did iteration end because of an error?
   318  	if it.Err() != nil {
   319  		return it.Err()
   320  	}
   321  	return nil
   322  }
   323  
   324  func GetBounce(domain, apiKey string) (mailgun.Bounce, error) {
   325  	mg := mailgun.NewMailgun(domain, apiKey)
   326  
   327  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   328  	defer cancel()
   329  
   330  	return mg.GetBounce(ctx, "foo@bar.com")
   331  }
   332  
   333  func ListBounces(domain, apiKey string) ([]mailgun.Bounce, error) {
   334  	mg := mailgun.NewMailgun(domain, apiKey)
   335  	it := mg.ListBounces(nil)
   336  
   337  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   338  	defer cancel()
   339  
   340  	var page, result []mailgun.Bounce
   341  	for it.Next(ctx, &page) {
   342  		result = append(result, page...)
   343  	}
   344  
   345  	if it.Err() != nil {
   346  		return nil, it.Err()
   347  	}
   348  	return result, nil
   349  }
   350  
   351  func GetComplaints(domain, apiKey string) (mailgun.Complaint, error) {
   352  	mg := mailgun.NewMailgun(domain, apiKey)
   353  
   354  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   355  	defer cancel()
   356  
   357  	return mg.GetComplaint(ctx, "baz@example.com")
   358  }
   359  
   360  func ListComplaints(domain, apiKey string) ([]mailgun.Complaint, error) {
   361  	mg := mailgun.NewMailgun(domain, apiKey)
   362  	it := mg.ListComplaints(nil)
   363  
   364  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   365  	defer cancel()
   366  
   367  	var page, result []mailgun.Complaint
   368  	for it.Next(ctx, &page) {
   369  		result = append(result, page...)
   370  	}
   371  
   372  	if it.Err() != nil {
   373  		return nil, it.Err()
   374  	}
   375  	return result, nil
   376  }
   377  
   378  func GetDomainConnection(domain, apiKey string) (mailgun.DomainConnection, error) {
   379  	mg := mailgun.NewMailgun(domain, apiKey)
   380  
   381  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   382  	defer cancel()
   383  
   384  	return mg.GetDomainConnection(ctx, domain)
   385  }
   386  
   387  func ListCredentials(domain, apiKey string) ([]mailgun.Credential, error) {
   388  	mg := mailgun.NewMailgun(domain, apiKey)
   389  	it := mg.ListCredentials(nil)
   390  
   391  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   392  	defer cancel()
   393  
   394  	var page, result []mailgun.Credential
   395  	for it.Next(ctx, &page) {
   396  		result = append(result, page...)
   397  	}
   398  
   399  	if it.Err() != nil {
   400  		return nil, it.Err()
   401  	}
   402  	return result, nil
   403  }
   404  
   405  func GetDomain(domain, apiKey string) (mailgun.DomainResponse, error) {
   406  	mg := mailgun.NewMailgun(domain, apiKey)
   407  
   408  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   409  	defer cancel()
   410  
   411  	return mg.GetDomain(ctx, domain)
   412  }
   413  
   414  func ListDomainIPS(domain, apiKey string) ([]mailgun.IPAddress, error) {
   415  	mg := mailgun.NewMailgun(domain, apiKey)
   416  
   417  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   418  	defer cancel()
   419  
   420  	return mg.ListDomainIPS(ctx)
   421  }
   422  
   423  func GetDomainTracking(domain, apiKey string) (mailgun.DomainTracking, error) {
   424  	mg := mailgun.NewMailgun(domain, apiKey)
   425  
   426  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   427  	defer cancel()
   428  
   429  	return mg.GetDomainTracking(ctx, domain)
   430  }
   431  
   432  func ListDomains(domain, apiKey string) ([]mailgun.Domain, error) {
   433  	mg := mailgun.NewMailgun(domain, apiKey)
   434  	it := mg.ListDomains(nil)
   435  
   436  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   437  	defer cancel()
   438  
   439  	var page, result []mailgun.Domain
   440  	for it.Next(ctx, &page) {
   441  		result = append(result, page...)
   442  	}
   443  
   444  	if it.Err() != nil {
   445  		return nil, it.Err()
   446  	}
   447  	return result, nil
   448  }
   449  
   450  func GetExport(domain, apiKey string) (mailgun.Export, error) {
   451  	mg := mailgun.NewMailgun(domain, apiKey)
   452  
   453  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   454  	defer cancel()
   455  
   456  	return mg.GetExport(ctx, "EXPORT_ID")
   457  }
   458  
   459  func GetIP(domain, apiKey string) (mailgun.IPAddress, error) {
   460  	mg := mailgun.NewMailgun(domain, apiKey)
   461  
   462  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   463  	defer cancel()
   464  
   465  	return mg.GetIP(ctx, "127.0.0.1")
   466  }
   467  
   468  func ListIPS(domain, apiKey string) ([]mailgun.IPAddress, error) {
   469  	mg := mailgun.NewMailgun(domain, apiKey)
   470  
   471  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   472  	defer cancel()
   473  
   474  	// Pass 'true' to only return dedicated ips
   475  	return mg.ListIPS(ctx, true)
   476  }
   477  
   478  func GetTagLimits(domain, apiKey string) (mailgun.TagLimits, error) {
   479  	mg := mailgun.NewMailgun(domain, apiKey)
   480  
   481  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   482  	defer cancel()
   483  
   484  	return mg.GetTagLimits(ctx, domain)
   485  }
   486  
   487  func ListExports(domain, apiKey string) ([]mailgun.Export, error) {
   488  	mg := mailgun.NewMailgun(domain, apiKey)
   489  
   490  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   491  	defer cancel()
   492  
   493  	// Optionally pass a url to filter by
   494  	return mg.ListExports(ctx, "")
   495  }
   496  
   497  func GetMembers(domain, apiKey string) ([]mailgun.Member, error) {
   498  	mg := mailgun.NewMailgun(domain, apiKey)
   499  	it := mg.ListMembers("list@example.com", nil)
   500  
   501  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   502  	defer cancel()
   503  
   504  	var page, result []mailgun.Member
   505  	for it.Next(ctx, &page) {
   506  		result = append(result, page...)
   507  	}
   508  
   509  	if it.Err() != nil {
   510  		return nil, it.Err()
   511  	}
   512  	return result, nil
   513  }
   514  
   515  func ListMailingLists(domain, apiKey string) ([]mailgun.MailingList, error) {
   516  	mg := mailgun.NewMailgun(domain, apiKey)
   517  	it := mg.ListMailingLists(nil)
   518  
   519  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   520  	defer cancel()
   521  
   522  	var page, result []mailgun.MailingList
   523  	for it.Next(ctx, &page) {
   524  		result = append(result, page...)
   525  	}
   526  
   527  	if it.Err() != nil {
   528  		return nil, it.Err()
   529  	}
   530  	return result, nil
   531  }
   532  
   533  func ParseAddress(apiKey string) ([]string, []string, error) {
   534  	mv := mailgun.NewEmailValidator(apiKey)
   535  
   536  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   537  	defer cancel()
   538  
   539  	return mv.ParseAddresses(ctx,
   540  		"Alice <alice@example.com>",
   541  		"bob@example.com",
   542  		// ...
   543  	)
   544  }
   545  
   546  func GetRoute(domain, apiKey string) (mailgun.Route, error) {
   547  	mg := mailgun.NewMailgun(domain, apiKey)
   548  
   549  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   550  	defer cancel()
   551  
   552  	return mg.GetRoute(ctx, "route_id")
   553  }
   554  
   555  func ListRoutes(domain, apiKey string) ([]mailgun.Route, error) {
   556  	mg := mailgun.NewMailgun(domain, apiKey)
   557  	it := mg.ListRoutes(nil)
   558  
   559  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   560  	defer cancel()
   561  
   562  	var page, result []mailgun.Route
   563  	for it.Next(ctx, &page) {
   564  		result = append(result, page...)
   565  	}
   566  
   567  	if it.Err() != nil {
   568  		return nil, it.Err()
   569  	}
   570  	return result, nil
   571  }
   572  
   573  func GetStats(domain, apiKey string) ([]mailgun.Stats, error) {
   574  	mg := mailgun.NewMailgun(domain, apiKey)
   575  
   576  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   577  	defer cancel()
   578  
   579  	return mg.GetStats(ctx, []string{"accepted", "delivered", "failed"}, &mailgun.GetStatOptions{
   580  		Duration: "1m",
   581  	})
   582  }
   583  
   584  func ListTags(domain, apiKey string) ([]mailgun.Tag, error) {
   585  	mg := mailgun.NewMailgun(domain, apiKey)
   586  	it := mg.ListTags(nil)
   587  
   588  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   589  	defer cancel()
   590  
   591  	var page, result []mailgun.Tag
   592  	for it.Next(ctx, &page) {
   593  		result = append(result, page...)
   594  	}
   595  
   596  	if it.Err() != nil {
   597  		return nil, it.Err()
   598  	}
   599  	return result, nil
   600  }
   601  
   602  func ListUnsubscribes(domain, apiKey string) ([]mailgun.Unsubscribe, error) {
   603  	mg := mailgun.NewMailgun(domain, apiKey)
   604  	it := mg.ListUnsubscribes(nil)
   605  
   606  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   607  	defer cancel()
   608  
   609  	var page, result []mailgun.Unsubscribe
   610  	for it.Next(ctx, &page) {
   611  		result = append(result, page...)
   612  	}
   613  
   614  	if it.Err() != nil {
   615  		return nil, it.Err()
   616  	}
   617  	return result, nil
   618  }
   619  
   620  func ValidateEmail(apiKey string) (mailgun.EmailVerification, error) {
   621  	mv := mailgun.NewEmailValidator(apiKey)
   622  
   623  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   624  	defer cancel()
   625  
   626  	return mv.ValidateEmail(ctx, "foo@mailgun.net", false)
   627  }
   628  
   629  func GetWebhook(domain, apiKey string) ([]string, error) {
   630  	mg := mailgun.NewMailgun(domain, apiKey)
   631  
   632  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   633  	defer cancel()
   634  
   635  	return mg.GetWebhook(ctx, "clicked")
   636  }
   637  
   638  func ListWebhooks(domain, apiKey string) (map[string][]string, error) {
   639  	mg := mailgun.NewMailgun(domain, apiKey)
   640  
   641  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   642  	defer cancel()
   643  
   644  	return mg.ListWebhooks(ctx)
   645  }
   646  
   647  func DeleteDomainIP(domain, apiKey string) error {
   648  	mg := mailgun.NewMailgun(domain, apiKey)
   649  
   650  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   651  	defer cancel()
   652  
   653  	return mg.DeleteDomainIP(ctx, "127.0.0.1")
   654  }
   655  
   656  func DeleteListMember(domain, apiKey string) error {
   657  	mg := mailgun.NewMailgun(domain, apiKey)
   658  
   659  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   660  	defer cancel()
   661  
   662  	return mg.DeleteMember(ctx, "joe@example.com", "list@example.com")
   663  }
   664  
   665  func DeleteMailingList(domain, apiKey string) error {
   666  	mg := mailgun.NewMailgun(domain, apiKey)
   667  
   668  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   669  	defer cancel()
   670  
   671  	return mg.DeleteMailingList(ctx, "list@example.com")
   672  }
   673  
   674  func ResendMessage(domain, apiKey string) (string, string, error) {
   675  	mg := mailgun.NewMailgun(domain, apiKey)
   676  
   677  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   678  	defer cancel()
   679  
   680  	return mg.ReSend(ctx, "STORAGE_URL", "bar@example.com")
   681  }
   682  
   683  func SendComplexMessage(domain, apiKey string) (string, error) {
   684  	mg := mailgun.NewMailgun(domain, apiKey)
   685  	m := mg.NewMessage(
   686  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   687  		"Hello",
   688  		"Testing some Mailgun awesomeness!",
   689  		"foo@example.com",
   690  	)
   691  	m.AddCC("baz@example.com")
   692  	m.AddBCC("bar@example.com")
   693  	m.SetHtml("<html>HTML version of the body</html>")
   694  	m.AddAttachment("files/test.jpg")
   695  	m.AddAttachment("files/test.txt")
   696  
   697  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   698  	defer cancel()
   699  
   700  	_, id, err := mg.Send(ctx, m)
   701  	return id, err
   702  }
   703  
   704  func SendWithConnectionOptions(domain, apiKey string) (string, error) {
   705  	mg := mailgun.NewMailgun(domain, apiKey)
   706  	m := mg.NewMessage(
   707  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   708  		"Hello",
   709  		"Testing some Mailgun awesomeness!",
   710  		"foo@example.com",
   711  	)
   712  
   713  	m.SetRequireTLS(true)
   714  	m.SetSkipVerification(true)
   715  
   716  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   717  	defer cancel()
   718  
   719  	_, id, err := mg.Send(ctx, m)
   720  	return id, err
   721  }
   722  
   723  func SendInlineImage(domain, apiKey string) (string, error) {
   724  	mg := mailgun.NewMailgun(domain, apiKey)
   725  	m := mg.NewMessage(
   726  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   727  		"Hello",
   728  		"Testing some Mailgun awesomeness!",
   729  		"foo@example.com",
   730  	)
   731  	m.AddCC("baz@example.com")
   732  	m.AddBCC("bar@example.com")
   733  	m.SetHtml("<html>HTML version of the body</html>")
   734  	m.AddInline("files/test.jpg")
   735  	m.AddInline("files/test.txt")
   736  
   737  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   738  	defer cancel()
   739  
   740  	_, id, err := mg.Send(ctx, m)
   741  	return id, err
   742  }
   743  
   744  func SendMessageNoTracking(domain, apiKey string) (string, error) {
   745  	mg := mailgun.NewMailgun(domain, apiKey)
   746  	m := mg.NewMessage(
   747  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   748  		"Hello",
   749  		"Testing some Mailgun awesomeness!",
   750  		"foo@example.com",
   751  	)
   752  	m.SetTracking(false)
   753  
   754  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   755  	defer cancel()
   756  
   757  	_, id, err := mg.Send(ctx, m)
   758  	return id, err
   759  }
   760  
   761  func SendMimeMessage(domain, apiKey string) (string, error) {
   762  	mg := mailgun.NewMailgun(domain, apiKey)
   763  	mimeMsgReader, err := os.Open("files/message.mime")
   764  	if err != nil {
   765  		return "", err
   766  	}
   767  
   768  	m := mg.NewMIMEMessage(mimeMsgReader, "bar@example.com")
   769  
   770  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   771  	defer cancel()
   772  
   773  	_, id, err := mg.Send(ctx, m)
   774  	return id, err
   775  }
   776  
   777  func SendScheduledMessage(domain, apiKey string) (string, error) {
   778  	mg := mailgun.NewMailgun(domain, apiKey)
   779  	m := mg.NewMessage(
   780  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   781  		"Hello",
   782  		"Testing some Mailgun awesomeness!",
   783  		"bar@example.com",
   784  	)
   785  	m.SetDeliveryTime(time.Now().Add(5 * time.Minute))
   786  
   787  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   788  	defer cancel()
   789  
   790  	_, id, err := mg.Send(ctx, m)
   791  	return id, err
   792  }
   793  
   794  func SendSimpleMessage(domain, apiKey string) (string, error) {
   795  	mg := mailgun.NewMailgun(domain, apiKey)
   796  	m := mg.NewMessage(
   797  		"Excited User <mailgun@YOUR_DOMAIN_NAME>",
   798  		"Hello",
   799  		"Testing some Mailgun awesomeness!",
   800  		"YOU@YOUR_DOMAIN_NAME",
   801  	)
   802  
   803  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   804  	defer cancel()
   805  
   806  	_, id, err := mg.Send(ctx, m)
   807  	return id, err
   808  }
   809  
   810  func SendTaggedMessage(domain, apiKey string) (string, error) {
   811  	mg := mailgun.NewMailgun(domain, apiKey)
   812  	m := mg.NewMessage(
   813  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   814  		"Hello",
   815  		"Testing some Mailgun awesomeness!",
   816  		"bar@example.com",
   817  	)
   818  
   819  	err := m.AddTag("FooTag", "BarTag", "BlortTag")
   820  	if err != nil {
   821  		return "", err
   822  	}
   823  
   824  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   825  	defer cancel()
   826  
   827  	_, id, err := mg.Send(ctx, m)
   828  	return id, err
   829  }
   830  
   831  func SendTemplateMessage(domain, apiKey string) (string, error) {
   832  	mg := mailgun.NewMailgun(domain, apiKey)
   833  	m := mg.NewMessage(
   834  		"Excited User <YOU@YOUR_DOMAIN_NAME>",
   835  		"Hey %recipient.first%",
   836  		"If you wish to unsubscribe, click http://mailgun/unsubscribe/%recipient.id%",
   837  	) // IMPORTANT: No To:-field recipients!
   838  
   839  	m.AddRecipientAndVariables("bob@example.com", map[string]interface{}{
   840  		"first": "bob",
   841  		"id":    1,
   842  	})
   843  
   844  	m.AddRecipientAndVariables("alice@example.com", map[string]interface{}{
   845  		"first": "alice",
   846  		"id":    2,
   847  	})
   848  
   849  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   850  	defer cancel()
   851  
   852  	_, id, err := mg.Send(ctx, m)
   853  	return id, err
   854  }
   855  
   856  func UpdateDomainConnection(domain, apiKey string) error {
   857  	mg := mailgun.NewMailgun(domain, apiKey)
   858  
   859  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   860  	defer cancel()
   861  
   862  	return mg.UpdateDomainConnection(ctx, domain, mailgun.DomainConnection{
   863  		RequireTLS:       true,
   864  		SkipVerification: true,
   865  	})
   866  }
   867  
   868  func UpdateMember(domain, apiKey string) error {
   869  	mg := mailgun.NewMailgun(domain, apiKey)
   870  
   871  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   872  	defer cancel()
   873  
   874  	_, err := mg.UpdateMember(ctx, "bar@example.com", "list@example.com", mailgun.Member{
   875  		Name:       "Foo Bar",
   876  		Subscribed: mailgun.Unsubscribed,
   877  	})
   878  	return err
   879  }
   880  
   881  func UpdateWebhook(domain, apiKey string) error {
   882  	mg := mailgun.NewMailgun(domain, apiKey)
   883  
   884  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   885  	defer cancel()
   886  
   887  	return mg.UpdateWebhook(ctx, "clicked", []string{"https://your_domain.com/clicked"})
   888  }
   889  
   890  func VerifyWebhookSignature(domain, apiKey, timestamp, token, signature string) (bool, error) {
   891  	mg := mailgun.NewMailgun(domain, apiKey)
   892  
   893  	return mg.VerifyWebhookSignature(mailgun.Signature{
   894  		TimeStamp: timestamp,
   895  		Token:     token,
   896  		Signature: signature,
   897  	})
   898  }
   899  
   900  func SendMessageWithTemplate(domain, apiKey string) error {
   901  	mg := mailgun.NewMailgun(domain, apiKey)
   902  	var err error
   903  
   904  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   905  	defer cancel()
   906  
   907  	// Create a new template
   908  	err = mg.CreateTemplate(ctx, &mailgun.Template{
   909  		Name: "my-template",
   910  		Version: mailgun.TemplateVersion{
   911  			Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
   912  			Engine:   mailgun.TemplateEngineGo,
   913  			Tag:      "v1",
   914  		},
   915  	})
   916  	if err != nil {
   917  		return err
   918  	}
   919  
   920  	// Give time for template to show up in the system.
   921  	time.Sleep(time.Second * 1)
   922  
   923  	// Create a new message with template
   924  	m := mg.NewMessage("Excited User <excited@example.com>", "Template example", "")
   925  	m.SetTemplate("my-template")
   926  
   927  	// Add recipients
   928  	m.AddRecipient("bob@example.com")
   929  	m.AddRecipient("alice@example.com")
   930  
   931  	// Add the variables to be used by the template
   932  	m.AddVariable("title", "Hello Templates")
   933  	m.AddVariable("body", "Body of the message")
   934  
   935  	_, id, err := mg.Send(ctx, m)
   936  	fmt.Printf("Queued: %s", id)
   937  	return err
   938  }
   939  
   940  func CreateTemplate(domain, apiKey string) error {
   941  	mg := mailgun.NewMailgun(domain, apiKey)
   942  
   943  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   944  	defer cancel()
   945  
   946  	return mg.CreateTemplate(ctx, &mailgun.Template{
   947  		Name: "my-template",
   948  		Version: mailgun.TemplateVersion{
   949  			Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
   950  			Engine:   mailgun.TemplateEngineGo,
   951  			Tag:      "v1",
   952  		},
   953  	})
   954  }
   955  
   956  func DeleteTemplate(domain, apiKey string) error {
   957  	mg := mailgun.NewMailgun(domain, apiKey)
   958  
   959  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   960  	defer cancel()
   961  
   962  	return mg.DeleteTemplate(ctx, "my-template")
   963  }
   964  
   965  func UpdateTemplate(domain, apiKey string) error {
   966  	mg := mailgun.NewMailgun(domain, apiKey)
   967  
   968  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   969  	defer cancel()
   970  
   971  	return mg.UpdateTemplate(ctx, &mailgun.Template{
   972  		Name:        "my-template",
   973  		Description: "Add a description to the template",
   974  	})
   975  }
   976  
   977  func GetTemplate(domain, apiKey string) (mailgun.Template, error) {
   978  	mg := mailgun.NewMailgun(domain, apiKey)
   979  
   980  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   981  	defer cancel()
   982  
   983  	return mg.GetTemplate(ctx, "my-template")
   984  }
   985  
   986  func ListActiveTemplates(domain, apiKey string) ([]mailgun.Template, error) {
   987  	mg := mailgun.NewMailgun(domain, apiKey)
   988  	it := mg.ListTemplates(&mailgun.ListTemplateOptions{Active: true})
   989  
   990  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
   991  	defer cancel()
   992  
   993  	var page, result []mailgun.Template
   994  	for it.Next(ctx, &page) {
   995  		result = append(result, page...)
   996  	}
   997  
   998  	if it.Err() != nil {
   999  		return nil, it.Err()
  1000  	}
  1001  	return result, nil
  1002  }
  1003  
  1004  func ListTemplates(domain, apiKey string) ([]mailgun.Template, error) {
  1005  	mg := mailgun.NewMailgun(domain, apiKey)
  1006  	it := mg.ListTemplates(nil)
  1007  
  1008  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1009  	defer cancel()
  1010  
  1011  	var page, result []mailgun.Template
  1012  	for it.Next(ctx, &page) {
  1013  		result = append(result, page...)
  1014  	}
  1015  
  1016  	if it.Err() != nil {
  1017  		return nil, it.Err()
  1018  	}
  1019  	return result, nil
  1020  }
  1021  
  1022  func AddTemplateVersion(domain, apiKey string) error {
  1023  	mg := mailgun.NewMailgun(domain, apiKey)
  1024  
  1025  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1026  	defer cancel()
  1027  
  1028  	return mg.AddTemplateVersion(ctx, "my-template", &mailgun.TemplateVersion{
  1029  		Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`,
  1030  		Engine:   mailgun.TemplateEngineGo,
  1031  		Tag:      "v2",
  1032  		Active:   true,
  1033  	})
  1034  }
  1035  
  1036  func DeleteTemplateVersion(domain, apiKey string) error {
  1037  	mg := mailgun.NewMailgun(domain, apiKey)
  1038  
  1039  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1040  	defer cancel()
  1041  
  1042  	// Delete the template version tagged as 'v2'
  1043  	return mg.DeleteTemplateVersion(ctx, "my-template", "v2")
  1044  }
  1045  
  1046  func GetTemplateVersion(domain, apiKey string) (mailgun.TemplateVersion, error) {
  1047  	mg := mailgun.NewMailgun(domain, apiKey)
  1048  
  1049  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1050  	defer cancel()
  1051  
  1052  	// Get the template version tagged as 'v2'
  1053  	return mg.GetTemplateVersion(ctx, "my-template", "v2")
  1054  }
  1055  
  1056  func UpdateTemplateVersion(domain, apiKey string) error {
  1057  	mg := mailgun.NewMailgun(domain, apiKey)
  1058  
  1059  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1060  	defer cancel()
  1061  
  1062  	return mg.UpdateTemplateVersion(ctx, "my-template", &mailgun.TemplateVersion{
  1063  		Comment: "Add a comment to the template and make it 'active'",
  1064  		Tag:     "v2",
  1065  		Active:  true,
  1066  	})
  1067  }
  1068  
  1069  func ListTemplateVersions(domain, apiKey string) ([]mailgun.TemplateVersion, error) {
  1070  	mg := mailgun.NewMailgun(domain, apiKey)
  1071  	it := mg.ListTemplateVersions("my-template", nil)
  1072  
  1073  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  1074  	defer cancel()
  1075  
  1076  	var page, result []mailgun.TemplateVersion
  1077  	for it.Next(ctx, &page) {
  1078  		result = append(result, page...)
  1079  	}
  1080  
  1081  	if it.Err() != nil {
  1082  		return nil, it.Err()
  1083  	}
  1084  	return result, nil
  1085  }