Skip to content

Commit

Permalink
feat(EmailMessage): change EmailMessage parameter to pointer in Email…
Browse files Browse the repository at this point in the history
…Sender interface for efficiency.
  • Loading branch information
JRocabruna committed Jun 4, 2024
1 parent 7e90948 commit b0252ff
Show file tree
Hide file tree
Showing 37 changed files with 155 additions and 110 deletions.
2 changes: 1 addition & 1 deletion docs/AWS_SES_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage(sender,[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage(sender,[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").
AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_API_Key.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if err != nil {
}

// Define email message using an alias email address
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_JWT.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if err != nil {
}

// Define email message using an alias email address
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_OAuth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ if err != nil {
}

// Define email message using an alias email address
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_ServiceAccount copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ if err != nil {
}

// Define email message using an alias email address
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_ServiceAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ if err != nil {
}

// Define email message using an alias email address
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Mailgun_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Mandrill_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Microsoft365_Credentials_ROPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Microsoft365_Credentials_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Microsoft365_Credentials_credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Microsoft365_Credentials_managed_id.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Microsoft365_Credentials_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/Postmark_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/SMTP_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
2 changes: 1 addition & 1 deletion docs/SendGrid_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if err != nil {
}

// Define email message
message := *gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
message := gomail.NewEmailMessage("[email protected]",[]string{"[email protected]"}, "Test Email with attachment", "This is the plain text part of the email.").
SetHTML("<p>This is the <b>HTML</b> part of the <i>email</i>.</p>").AddAttachments(gomail.Attachment{
Filename: "attachment.jpg", Content: attachmentContent,
})
Expand Down
4 changes: 2 additions & 2 deletions email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ const DefaultMaxAttachmentSize = 25 * 1024 * 1024 // 25 MB
type EmailSender interface {
// SendEmail sends an email with the given message.
// Parameters:
// - message: An EmailMessage struct containing the details of the email to be sent.
// - message: A pointer to an EmailMessage struct containing the details of the email to be sent.
// Returns:
// - error: An error if sending the email fails, otherwise nil.
SendEmail(message EmailMessage) error
SendEmail(message *EmailMessage) error
}

// EmailMessage contains the fields for sending an email.
Expand Down
2 changes: 1 addition & 1 deletion providers/gmail/gmail_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (ms *gmailMessageSenderWrapper) send(message *gmail.Message) (*gmail.Messag
//
// Returns:
// - error: An error if sending the email fails.
func (s *gmailMessageSenderWrapper) SendEmail(message gomail.EmailMessage) error {
func (s *gmailMessageSenderWrapper) SendEmail(message *gomail.EmailMessage) error {
mimeMessage, err := providers.BuildMimeMessage(message)
if err != nil {
return fmt.Errorf("unable to build MIME message: %w", err)
Expand Down
15 changes: 10 additions & 5 deletions providers/gmail/gmail_email_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type MockTokenManager struct {
err error
}

// TestEmailSenderImplementation checks if gmailMessageSenderWrapper implements the EmailSender interface
func TestEmailSenderImplementation(t *testing.T) {
var _ gomail.EmailSender = (*gmailMessageSenderWrapper)(nil)
}

func (m *MockTokenManager) GetToken() ([]byte, error) {
return m.token, m.err
}
Expand Down Expand Up @@ -100,15 +105,15 @@ func (m *mockInvalidTokenManager) GetToken() ([]byte, error) {
func TestSendEmailWithMockService(t *testing.T) {
emailSender := buildMockGmailMessageSenderWrapper(nil)

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")

err := emailSender.SendEmail(message)
assert.NoError(t, err)
}

func TestSendEmailWithSendMessageError(t *testing.T) {
emailSender := buildMockGmailMessageSenderWrapper(errors.New("send message error"))
message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")

err := emailSender.SendEmail(message)
assert.Error(t, err)
Expand All @@ -117,7 +122,7 @@ func TestSendEmailWithSendMessageError(t *testing.T) {
func TestSendEmailWithMockServiceError(t *testing.T) {
emailSender := buildMockGmailMessageSenderWrapper(errors.New("mock service error"))

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")

err := emailSender.SendEmail(message)
assert.Error(t, err)
Expand All @@ -126,7 +131,7 @@ func TestSendEmailWithMockServiceError(t *testing.T) {
func TestSendEmailWithBCC(t *testing.T) {
emailSender := buildMockGmailMessageSenderWrapper(nil)

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
SetBCC([]string{"[email protected]"})

err := emailSender.SendEmail(message)
Expand Down Expand Up @@ -317,7 +322,7 @@ func TestNewGmailEmailSenderJWTAccessInvalidJson(t *testing.T) {
func TestSendEmailWithNilGmailService(t *testing.T) {
emailSender := &gmailMessageSenderWrapper{}

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")

err := emailSender.SendEmail(message)
assert.Error(t, err)
Expand Down
6 changes: 3 additions & 3 deletions providers/helper_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
// HTML content, and multiple attachments.
//
// Parameters:
// - message: An EmailMessage struct containing the details of the email to be sent.
// - message: A pointer to an EmailMessage struct containing the details of the email to be sent.
//
// Returns:
// - []byte: A byte slice containing the complete MIME message.
// - error: An error if constructing the MIME message fails, otherwise nil.
//
// Example:
//
// message := *gomail.NewEmailMessage(
// message := gomail.NewEmailMessage(
// "[email protected]",
// []string["[email protected]"],
// "Test Email",
Expand All @@ -36,7 +36,7 @@ import (
// log.Fatalf("Failed to build MIME message: %v", err)
// }
// fmt.Println(string(mimeMessage))
func BuildMimeMessage(message gomail.EmailMessage) ([]byte, error) {
func BuildMimeMessage(message *gomail.EmailMessage) ([]byte, error) {
var msg bytes.Buffer

// Determine boundaries
Expand Down
10 changes: 5 additions & 5 deletions providers/helper_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (

func TestBuildMimeMessage(t *testing.T) {
tests := []struct {
message gomail.EmailMessage
message *gomail.EmailMessage
contains []string
}{
{
*gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email."),
gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email."),
[]string{"From: [email protected]", "To: [email protected]", "Subject: Test Email", "This is a test email."},
},
{
*gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "<p>This is a test email.</p>"),
gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "<p>This is a test email.</p>"),
[]string{"From: [email protected]", "To: [email protected]", "Subject: Test Email", "Content-Type: text/html", "<p>This is a test email.</p>"},
},
{
*gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
SetCC([]string{"[email protected]"}).
SetBCC([]string{"[email protected]"}).
SetAttachments([]gomail.Attachment{
Expand All @@ -32,7 +32,7 @@ func TestBuildMimeMessage(t *testing.T) {
[]string{"From: [email protected]", "To: [email protected]", "Cc: [email protected]", "Subject: Test Email", "This is a test email.", "Content-Disposition: attachment; filename=\"test.txt\"", base64.StdEncoding.EncodeToString([]byte("This is a test attachment."))},
},
{
*gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
SetCC([]string{"[email protected]"}).
SetBCC([]string{"[email protected]"}).
SetReplyTo("[email protected]"),
Expand Down
4 changes: 2 additions & 2 deletions providers/mailgun/mailgun_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func NewMailgunEmailSender(domain, apiKey string) (*mailgunEmailSender, error) {
// It constructs the email message from the given EmailMessage and sends it using the Mailgun API.
//
// Parameters:
// - message: An EmailMessage struct that contains the details of the email to be sent, including the sender, recipients, subject, body content, and any attachments.
// - message: A pointer to an EmailMessage struct that contains the details of the email to be sent, including the sender, recipients, subject, body content, and any attachments.
//
// Returns:
// - error: An error if the email sending fails, otherwise nil.
func (s *mailgunEmailSender) SendEmail(message gomail.EmailMessage) error {
func (s *mailgunEmailSender) SendEmail(message *gomail.EmailMessage) error {
mg := s.mailgunClient

mGMessage := mg.NewMessage(
Expand Down
11 changes: 8 additions & 3 deletions providers/mailgun/mailgun_email_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/stretchr/testify/assert"
)

// TestEmailSenderImplementation checks if mailgunEmailSender implements the EmailSender interface
func TestEmailSenderImplementation(t *testing.T) {
var _ gomail.EmailSender = (*mailgunEmailSender)(nil)
}

// Mock implementations for Mailgun
type mockMailgun struct{}

Expand Down Expand Up @@ -45,7 +50,7 @@ func TestMailgunEmailSender_SendEmail(t *testing.T) {
mailgunClient: &mockMailgun{},
}

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.").
SetCC([]string{"[email protected]"}).
SetBCC([]string{"[email protected]"}).
SetReplyTo("[email protected]").
Expand All @@ -65,7 +70,7 @@ func TestMailgunEmailSender_SendEmailWithSendError(t *testing.T) {
mailgunClient: &mockMailgunWithError{},
}

message := *gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")
message := gomail.NewEmailMessage("[email protected]", []string{"[email protected]"}, "Test Email", "This is a test email.")

err := emailSender.SendEmail(message)
assert.Error(t, err)
Expand All @@ -77,7 +82,7 @@ func TestMailgunEmailSender_SendEmailWithEmptyFields(t *testing.T) {
mailgunClient: &mockMailgun{},
}

message := *gomail.NewEmailMessage(
message := gomail.NewEmailMessage(
"[email protected]",
[]string{},
"",
Expand Down
4 changes: 2 additions & 2 deletions providers/mandrill/mandrill_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func NewMandrillEmailSender(apiKey string) (*mandrillEmailSender, error) {
// It constructs the email message from the given EmailMessage and sends it using the Mandrill API.
//
// Parameters:
// - message: An EmailMessage struct that contains the details of the email to be sent, including the sender, recipients, subject, body content, and any attachments.
// - message: A pointer to an EmailMessage struct that contains the details of the email to be sent, including the sender, recipients, subject, body content, and any attachments.
//
// Returns:
// - error: An error if the email sending fails, otherwise nil.
func (s *mandrillEmailSender) SendEmail(message gomail.EmailMessage) error {
func (s *mandrillEmailSender) SendEmail(message *gomail.EmailMessage) error {
maMessage := mandrillMessage{
FromEmail: message.GetFrom(),
To: make([]mandrillRecipient, 0),
Expand Down
Loading

0 comments on commit b0252ff

Please sign in to comment.