Skip to content

Commit

Permalink
Minor fixes, removed deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 27, 2022
1 parent d5294cf commit cd3bc50
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
10 changes: 6 additions & 4 deletions email.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 15 additions & 14 deletions email_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package gomail

import (
"context"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -292,22 +293,22 @@ func TestMailService_SendEmail(t *testing.T) {
email.Recipients = append(email.Recipients, "[email protected]")

// 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())
}
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -375,7 +376,7 @@ func TestMailService_SendEmailInValid(t *testing.T) {
for recipients := 1; recipients <= maxToRecipients+1; recipients++ {
email.Recipients = append(email.Recipients, "[email protected]")
}
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{"[email protected]"}
Expand All @@ -384,7 +385,7 @@ func TestMailService_SendEmailInValid(t *testing.T) {
for recipients := 1; recipients <= maxCcRecipients+1; recipients++ {
email.RecipientsCc = append(email.RecipientsCc, "[email protected]")
}
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{"[email protected]"}
Expand All @@ -393,7 +394,7 @@ func TestMailService_SendEmailInValid(t *testing.T) {
for recipients := 1; recipients <= maxBccRecipients+1; recipients++ {
email.RecipientsBcc = append(email.RecipientsBcc, "[email protected]")
}
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))
}
}
4 changes: 2 additions & 2 deletions mandrill.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"strings"

"github.com/mattbaird/gochimp"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cd3bc50

Please sign in to comment.