Skip to content

Commit

Permalink
Updated postmark package to use CTX
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 27, 2022
1 parent cd3bc50 commit 9ac2d3a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions postmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package gomail

import (
"bufio"
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"log"
"strings"

Expand All @@ -13,11 +14,11 @@ import (

// postmarkInterface is an interface for Postmark/mocking
type postmarkInterface interface {
SendEmail(email postmark.Email) (postmark.EmailResponse, error)
SendEmail(ctx context.Context, email postmark.Email) (postmark.EmailResponse, error)
}

// sendViaPostmark sends an email using the Postmark service
func sendViaPostmark(client postmarkInterface, email *Email) (err error) {
func sendViaPostmark(ctx context.Context, client postmarkInterface, email *Email) (err error) {

// Create the email struct
postmarkEmail := postmark.Email{
Expand Down Expand Up @@ -73,7 +74,7 @@ func sendViaPostmark(client postmarkInterface, email *Email) (err error) {
// 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 All @@ -96,7 +97,7 @@ func sendViaPostmark(client postmarkInterface, email *Email) (err error) {

// Send the email
var resp postmark.EmailResponse
if resp, err = client.SendEmail(postmarkEmail); err != nil {
if resp, err = client.SendEmail(ctx, postmarkEmail); err != nil {
return
}

Expand Down
5 changes: 3 additions & 2 deletions postmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gomail

import (
"context"
"fmt"
"net/http"
"os"
Expand All @@ -13,7 +14,7 @@ import (
type mockPostmarkInterface struct{}

// SendEmail is for mocking
func (m *mockPostmarkInterface) SendEmail(email postmark.Email) (postmark.EmailResponse, error) {
func (m *mockPostmarkInterface) SendEmail(_ context.Context, email postmark.Email) (postmark.EmailResponse, error) {

// Success
if email.To == "[email protected]" {
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestSendViaPostmark(t *testing.T) {
email.RecipientsCc = []string{test.input}
email.RecipientsBcc = []string{test.input}
email.ReplyToAddress = test.input
if err = sendViaPostmark(client, email); err != nil && !test.expectedError {
if err = sendViaPostmark(context.Background(), client, email); err != nil && !test.expectedError {
t.Fatalf("%s Failed: expected to NOT throw an error, inputted and [%s], error [%s]", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Fatalf("%s Failed: expected to throw an error, inputted and [%s]", t.Name(), test.input)
Expand Down

0 comments on commit 9ac2d3a

Please sign in to comment.