diff --git a/README.md b/README.md index 37ba3868..7012fece 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,6 @@ func main() { // Send the message with a 10-second timeout resp, id, err := mg.Send(ctx, message) - if err != nil { log.Fatal(err) } diff --git a/attachments_test.go b/attachments_test.go index 74d9d4e8..bf137e04 100644 --- a/attachments_test.go +++ b/attachments_test.go @@ -30,7 +30,7 @@ func TestMultipleAttachments(t *testing.T) { var ctx = context.Background() - m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "attachment@"+testDomain) + m := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "attachment@"+testDomain) // Add 2 attachments m.AddAttachment(createAttachment(t)) diff --git a/events_test.go b/events_test.go index ed655e9d..1c3b1f24 100644 --- a/events_test.go +++ b/events_test.go @@ -86,7 +86,7 @@ func TestEventPoller(t *testing.T) { }() // Send an email - m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain) + m := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -126,7 +126,7 @@ func ExampleMailgunImpl_ListEvents() { for it.Next(ctx, &page) { for _, e := range page { // You can access some fields via the interface - //fmt.Printf("Event: '%s' TimeStamp: '%s'\n", e.GetName(), e.GetTimestamp()) + // fmt.Printf("Event: '%s' TimeStamp: '%s'\n", e.GetName(), e.GetTimestamp()) // and you can act upon each event by type switch event := e.(type) { diff --git a/examples/examples.go b/examples/examples.go index b6b7fce9..c3f04f8a 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -697,7 +697,7 @@ func ResendMessage(domain, apiKey string) (string, string, error) { func SendComplexMessage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -718,7 +718,7 @@ func SendComplexMessage(domain, apiKey string) (string, error) { func SendWithConnectionOptions(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -737,7 +737,7 @@ func SendWithConnectionOptions(domain, apiKey string) (string, error) { func SendInlineImage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -757,7 +757,7 @@ func SendInlineImage(domain, apiKey string) (string, error) { func SendMessageNoTracking(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -790,7 +790,7 @@ func SendMimeMessage(domain, apiKey string) (string, error) { func SendScheduledMessage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -807,7 +807,7 @@ func SendScheduledMessage(domain, apiKey string) (string, error) { func SendSimpleMessage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -823,7 +823,7 @@ func SendSimpleMessage(domain, apiKey string) (string, error) { func SendTaggedMessage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello", "Testing some Mailgun awesomeness!", @@ -844,7 +844,7 @@ func SendTaggedMessage(domain, apiKey string) (string, error) { func SendTemplateMessage(domain, apiKey string) (string, error) { mg := mailgun.NewMailgun(domain, apiKey) - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hey %recipient.first%", "If you wish to unsubscribe, click http://mailgun/unsubscribe/%recipient.id%", @@ -938,7 +938,7 @@ func SendMessageWithTemplate(domain, apiKey string) error { time.Sleep(time.Second * 1) // Create a new message with template - m := mg.NewMessage("Excited User ", "Template example", "") + m := mailgun.NewMessage("Excited User ", "Template example", "") m.SetTemplate("my-template") // Add recipients diff --git a/examples_test.go b/examples_test.go index 72f9f02d..ba440c9c 100644 --- a/examples_test.go +++ b/examples_test.go @@ -80,7 +80,7 @@ func ExampleMailgunImpl_Send_constructed() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() - m := mg.NewMessage( + m := mailgun.NewMessage( "Excited User ", "Hello World", "Testing some Mailgun Awesomeness!", diff --git a/messages.go b/messages.go index b76170bb..84846e2c 100644 --- a/messages.go +++ b/messages.go @@ -143,11 +143,13 @@ type TrackingOptions struct { } // features abstracts the common characteristics between regular and MIME messages. -// addCC, addBCC, recipientCount, setHtml and setAMPHtml are invoked via the package-global AddCC, AddBCC, -// RecipientCount, SetHtml and SetAMPHtml calls, as these functions are ignored for MIME messages. +// addCC, addBCC, recipientCount, setHtml and setAMPHtml are invoked via the AddCC, AddBCC, +// RecipientCount, SetHTML and SetAMPHtml calls, as these functions are ignored for MIME messages. // Send() invokes addValues to add message-type-specific MIME headers for the API call -// to Mailgun. isValid yeilds true if and only if the message is valid enough for sending -// through the API. Finally, endpoint() tells Send() which endpoint to use to submit the API call. +// to Mailgun. +// isValid yields true if and only if the message is valid enough for sending +// through the API. +// Finally, endpoint() tells Send() which endpoint to use to submit the API call. type features interface { addCC(string) addBCC(string) @@ -433,6 +435,8 @@ func (m *Message) SetDeliveryTime(dt time.Time) { // Refer to the Mailgun documentation for more information. func (m *Message) SetSTOPeriod(stoPeriod string) error { validPattern := `^([2-6][4-9]|[3-6][0-9]|7[0-2])h$` + // TODO(vtopc): regexp.Compile, which is called by regexp.MatchString, is a heave operation, move into global variable + // or just parse using time.ParseDuration(). match, err := regexp.MatchString(validPattern, stoPeriod) if err != nil { return err diff --git a/messages_test.go b/messages_test.go index a4c4e10e..b617e912 100644 --- a/messages_test.go +++ b/messages_test.go @@ -58,7 +58,7 @@ func TestSendMGPlain(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) t.Log("TestSendPlain:MSG(" + msg + "),ID(" + id + ")") @@ -76,7 +76,7 @@ func TestSendMGPlainWithTracking(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetTracking(true) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -95,7 +95,7 @@ func TestSendMGPlainAt(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetDeliveryTime(time.Now().Add(5 * time.Minute)) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -114,7 +114,7 @@ func TestSendMGSTO(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetSTOPeriod("24h") msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -133,7 +133,7 @@ func TestSendMGHtml(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetHTML(exampleHtml) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -152,7 +152,7 @@ func TestSendMGAMPHtml(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetHTML(exampleHtml) m.SetAMPHtml(exampleAMPHtml) msg, id, err := mg.Send(ctx, m) @@ -172,7 +172,7 @@ func TestSendMGTracking(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText+"Tracking!\n", toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText+"Tracking!\n", toUser) m.SetTracking(false) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) @@ -191,7 +191,7 @@ func TestSendMGTrackingClicksHtmlOnly(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetHTML(exampleHtml) options := mailgun.TrackingOptions{ Tracking: true, @@ -216,7 +216,7 @@ func TestSendMGTag(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText+"Tags Galore!\n", toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText+"Tags Galore!\n", toUser) m.AddTag("FooTag") m.AddTag("BarTag") m.AddTag("BlortTag") @@ -251,14 +251,12 @@ func TestSendMGBatchFailRecipients(t *testing.T) { spendMoney(t, func() { toUser := os.Getenv("MG_EMAIL_TO") - mg, err := mailgun.NewMailgunFromEnv() - ensure.Nil(t, err) - m := mg.NewMessage(fromUser, exampleSubject, exampleText+"Batch\n") + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText+"Batch\n") for i := 0; i < mailgun.MaxNumberOfRecipients; i++ { m.AddRecipient("") // We expect this to indicate a failure at the API } - err = m.AddRecipientAndVariables(toUser, nil) + err := m.AddRecipientAndVariables(toUser, nil) // In case of error the SDK didn't send the message, // OR the API didn't check for empty To: headers. ensure.NotNil(t, err) @@ -276,7 +274,7 @@ func TestSendMGBatchRecipientVariables(t *testing.T) { ensure.Nil(t, err) ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, templateText) + m := mailgun.NewMessage(fromUser, exampleSubject, templateText) err = m.AddRecipientAndVariables(toUser, map[string]interface{}{ "name": "Joe Cool Example", "table": 42, @@ -311,7 +309,7 @@ func TestSendMGOffline(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) ensure.DeepEqual(t, msg, exampleMessage) @@ -344,7 +342,7 @@ func TestSendMGSeparateDomain(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.AddDomain(signingDomain) msg, id, err := mg.Send(ctx, m) @@ -397,7 +395,7 @@ func TestSendMGMessageVariables(t *testing.T) { mg := mailgun.NewMailgun(exampleDomain, exampleAPIKey) mg.SetAPIBase(srv.URL + "/v3") - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.AddVariable(exampleStrVarKey, exampleStrVarVal) m.AddVariable(exampleBoolVarKey, false) m.AddVariable(exampleMapVarKey, exampleMapVarVal) @@ -410,9 +408,7 @@ func TestSendMGMessageVariables(t *testing.T) { } func TestAddRecipientsError(t *testing.T) { - - mg := mailgun.NewMailgun(exampleDomain, exampleAPIKey) - m := mg.NewMessage(fromUser, exampleSubject, exampleText) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText) for i := 0; i < 1000; i++ { recipient := fmt.Sprintf("recipient_%d@example.com", i) @@ -427,8 +423,7 @@ func TestAddRecipientsError(t *testing.T) { func TestAddRecipientAndVariablesError(t *testing.T) { var err error - mg := mailgun.NewMailgun(exampleDomain, exampleAPIKey) - m := mg.NewMessage(fromUser, exampleSubject, exampleText) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText) for i := 0; i < 1000; i++ { recipient := fmt.Sprintf("recipient_%d@example.com", i) @@ -467,7 +462,7 @@ func TestSendDomainError(t *testing.T) { ctx := context.Background() mg := mailgun.NewMailgun(c.domain, exampleAPIKey) mg.SetAPIBase(srv.URL + "/v3") - m := mg.NewMessage(fromUser, exampleSubject, exampleText, "test@test.com") + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, "test@test.com") _, _, err := mg.Send(ctx, m) if c.isValid { @@ -493,7 +488,7 @@ func TestSendEOFError(t *testing.T) { mg := mailgun.NewMailgun(exampleDomain, exampleAPIKey) mg.SetAPIBase(srv.URL + "/v3") - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) _, _, err := mg.Send(context.Background(), m) ensure.NotNil(t, err) ensure.StringContains(t, err.Error(), "remote server prematurely closed connection: Post ") @@ -518,19 +513,19 @@ func TestHasRecipient(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") // No recipient - m := mg.NewMessage(fromUser, exampleSubject, exampleText) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText) _, _, err := mg.Send(context.Background(), m) ensure.NotNil(t, err) ensure.DeepEqual(t, err.Error(), "message not valid") // Provided Bcc - m = mg.NewMessage(fromUser, exampleSubject, exampleText) + m = mailgun.NewMessage(fromUser, exampleSubject, exampleText) m.AddBCC(recipient) _, _, err = mg.Send(context.Background(), m) ensure.Nil(t, err) // Provided cc - m = mg.NewMessage(fromUser, exampleSubject, exampleText) + m = mailgun.NewMessage(fromUser, exampleSubject, exampleText) m.AddCC(recipient) _, _, err = mg.Send(context.Background(), m) ensure.Nil(t, err) @@ -592,7 +587,7 @@ func TestAddOverrideHeader(t *testing.T) { mg.AddOverrideHeader("CustomHeader", "custom-value") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetRequireTLS(true) m.SetSkipVerification(true) @@ -631,7 +626,7 @@ func TestOnBehalfOfSubaccount(t *testing.T) { mg.SetOnBehalfOfSubaccount("mailgun.subaccount") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetRequireTLS(true) m.SetSkipVerification(true) @@ -663,7 +658,7 @@ func TestCaptureCurlOutput(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) ensure.DeepEqual(t, msg, exampleMessage) @@ -699,7 +694,7 @@ func TestSendTLSOptions(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, exampleText, toUser) m.SetRequireTLS(true) m.SetSkipVerification(true) @@ -729,7 +724,7 @@ func TestSendTemplate(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, "", toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, "", toUser) m.SetTemplate(templateName) msg, id, err := mg.Send(ctx, m) @@ -762,7 +757,7 @@ func TestSendTemplateOptions(t *testing.T) { mg.SetAPIBase(srv.URL + "/v3") ctx := context.Background() - m := mg.NewMessage(fromUser, exampleSubject, "", toUser) + m := mailgun.NewMessage(fromUser, exampleSubject, "", toUser) m.SetTemplate(templateName) m.SetTemplateRenderText(true) m.SetTemplateVersion(templateVersionTag) diff --git a/storage_test.go b/storage_test.go index 1a0b1ad5..e1d2263f 100644 --- a/storage_test.go +++ b/storage_test.go @@ -3,11 +3,12 @@ package mailgun_test import ( "context" "fmt" + "strings" + "testing" + "github.com/facebookgo/ensure" "github.com/mailgun/mailgun-go/v4" "github.com/mailgun/mailgun-go/v4/events" - "strings" - "testing" ) func TestStorage(t *testing.T) { @@ -16,7 +17,7 @@ func TestStorage(t *testing.T) { var ctx = context.Background() - m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "stored@"+testDomain) + m := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "stored@"+testDomain) msg, id, err := mg.Send(ctx, m) ensure.Nil(t, err) diff --git a/tags_test.go b/tags_test.go index 2c67f853..cae953b5 100644 --- a/tags_test.go +++ b/tags_test.go @@ -19,7 +19,7 @@ const ( func TestTags(t *testing.T) { mg := mailgun.NewMailgun(testDomain, testKey) mg.SetAPIBase(server.URL()) - msg := mg.NewMessage(fromUser, exampleSubject, exampleText, "test@example.com") + msg := mailgun.NewMessage(fromUser, exampleSubject, exampleText, "test@example.com") ensure.Nil(t, msg.AddTag("newsletter")) ensure.Nil(t, msg.AddTag("homer")) ensure.Nil(t, msg.AddTag("bart"))