diff --git a/email.go b/email.go index 3543d88..7838486 100644 --- a/email.go +++ b/email.go @@ -1,11 +1,13 @@ +// Package gomail is a lightweight email package with multi-provider support package gomail import ( "bytes" + "context" "fmt" "html/template" "io" - "io/ioutil" + "os" "path/filepath" "github.com/aymerick/douceur/inliner" @@ -104,7 +106,7 @@ func (e *Email) ParseHTMLTemplate(htmlLocation string) (htmlTemplate *template.T // Read HTML template file var tempBytes []byte - if tempBytes, err = ioutil.ReadFile(htmlLocation); err != nil { + if tempBytes, err = os.ReadFile(htmlLocation); err != nil { return } @@ -151,7 +153,7 @@ func (m *MailService) NewEmail() (email *Email) { } // SendEmail will send an email using the given provider -func (m *MailService) SendEmail(email *Email, provider ServiceProvider) (err error) { +func (m *MailService) SendEmail(ctx context.Context, email *Email, provider ServiceProvider) (err error) { // Do we have that provider? if containsServiceProvider(m.AvailableProviders, provider) { @@ -184,7 +186,7 @@ func (m *MailService) SendEmail(email *Email, provider ServiceProvider) (err err } else if provider == Mandrill { err = sendViaMandrill(m.mandrillService, email, false) } else if provider == Postmark { - err = sendViaPostmark(m.postmarkService, email) + err = sendViaPostmark(ctx, m.postmarkService, email) } else if provider == SMTP { err = sendViaSMTP(m.smtpClient, email) } diff --git a/email_test.go b/email_test.go index fe491ae..7fa8d8d 100644 --- a/email_test.go +++ b/email_test.go @@ -1,9 +1,10 @@ package gomail import ( + "context" "fmt" "html/template" - "io/ioutil" + "os" "path/filepath" "testing" ) @@ -215,7 +216,7 @@ func TestEmail_ApplyTemplates(t *testing.T) { } // Set the css theme - if email.CSS, err = ioutil.ReadFile(filepath.Join("examples", "example_theme.css")); err != nil { + if email.CSS, err = os.ReadFile(filepath.Join("examples", "example_theme.css")); err != nil { t.Fatalf("%s: error occurred: %s", t.Name(), err.Error()) } @@ -292,22 +293,22 @@ func TestMailService_SendEmail(t *testing.T) { email.Recipients = append(email.Recipients, "someone@domain.com") // Valid (Postmark) - if err = mail.SendEmail(email, Postmark); err != nil { + if err = mail.SendEmail(context.Background(), email, Postmark); err != nil { t.Fatalf("%s: error occurred: %s", t.Name(), err.Error()) } // Valid (AWS SES) - if err = mail.SendEmail(email, AwsSes); err != nil { + if err = mail.SendEmail(context.Background(), email, AwsSes); err != nil { t.Fatalf("%s: error occurred: %s", t.Name(), err.Error()) } // Valid (Mandrill) - if err = mail.SendEmail(email, Mandrill); err != nil { + if err = mail.SendEmail(context.Background(), email, Mandrill); err != nil { t.Fatalf("%s: error occurred: %s", t.Name(), err.Error()) } // Valid (SMTP) - if err = mail.SendEmail(email, SMTP); err != nil { + if err = mail.SendEmail(context.Background(), email, SMTP); err != nil { t.Fatalf("%s: error occurred: %s", t.Name(), err.Error()) } } @@ -339,33 +340,33 @@ func TestMailService_SendEmailInValid(t *testing.T) { email := mail.NewEmail() // Invalid provider - err = mail.SendEmail(email, 999) + err = mail.SendEmail(context.Background(), email, 999) if err == nil { t.Fatalf("%s: error was expected, provider was invalid", t.Name()) } // Invalid provider - not in available list - err = mail.SendEmail(email, AwsSes) + err = mail.SendEmail(context.Background(), email, AwsSes) if err == nil { t.Fatalf("%s: error was expected, provider was not in available list", t.Name()) } // Invalid - subject - err = mail.SendEmail(email, Postmark) + err = mail.SendEmail(context.Background(), email, Postmark) if err == nil { t.Fatalf("%s: error was expected, subject was invalid", t.Name()) } email.Subject = "Subject exits now" // Invalid - plain text missing - err = mail.SendEmail(email, Postmark) + err = mail.SendEmail(context.Background(), email, Postmark) if err == nil { t.Fatalf("%s: error was expected, subject was invalid", t.Name()) } email.PlainTextContent = "Plain text exits now" // Invalid - recipients missing - err = mail.SendEmail(email, Postmark) + err = mail.SendEmail(context.Background(), email, Postmark) if err == nil { t.Fatalf("%s: error was expected, subject was invalid", t.Name()) } @@ -375,7 +376,7 @@ func TestMailService_SendEmailInValid(t *testing.T) { for recipients := 1; recipients <= maxToRecipients+1; recipients++ { email.Recipients = append(email.Recipients, "someone@domain.com") } - if err = mail.SendEmail(email, Postmark); err == nil { + if err = mail.SendEmail(context.Background(), email, Postmark); err == nil { t.Fatalf("%s: error was expected, too many recipients, amount: %d", t.Name(), len(email.Recipients)) } email.Recipients = []string{"someone@domain.com"} @@ -384,7 +385,7 @@ func TestMailService_SendEmailInValid(t *testing.T) { for recipients := 1; recipients <= maxCcRecipients+1; recipients++ { email.RecipientsCc = append(email.RecipientsCc, "someone@domain.com") } - if err = mail.SendEmail(email, Postmark); err == nil { + if err = mail.SendEmail(context.Background(), email, Postmark); err == nil { t.Fatalf("%s: error was expected, too many recipients, amount: %d", t.Name(), len(email.RecipientsCc)) } email.RecipientsCc = []string{"someone@domain.com"} @@ -393,7 +394,7 @@ func TestMailService_SendEmailInValid(t *testing.T) { for recipients := 1; recipients <= maxBccRecipients+1; recipients++ { email.RecipientsBcc = append(email.RecipientsBcc, "someone@domain.com") } - if err = mail.SendEmail(email, Postmark); err == nil { + if err = mail.SendEmail(context.Background(), email, Postmark); err == nil { t.Fatalf("%s: error was expected, too many recipients, amount: %d", t.Name(), len(email.RecipientsBcc)) } } diff --git a/mandrill.go b/mandrill.go index f5a88da..4a7a499 100644 --- a/mandrill.go +++ b/mandrill.go @@ -4,7 +4,7 @@ import ( "bufio" "encoding/base64" "fmt" - "io/ioutil" + "io" "strings" "github.com/mattbaird/gochimp" @@ -82,7 +82,7 @@ func sendViaMandrill(client mandrillInterface, email *Email, async bool) (err er // Read all content from the attachment reader := bufio.NewReader(attachment.FileReader) var content []byte - if content, err = ioutil.ReadAll(reader); err != nil { + if content, err = io.ReadAll(reader); err != nil { return }