From 96ab8e71d1440eebdd91747010a79b6058a83664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Rocabruna?= Date: Tue, 4 Jun 2024 22:21:37 +0200 Subject: [PATCH] refactor(email_sender): encapsulate EmailMessage struct --- docs/AWS_SES_Credentials.md | 15 +- docs/Gmail_Credentials_API_Key.md | 14 +- docs/Gmail_Credentials_JWT.md | 12 +- docs/Gmail_Credentials_OAuth2.md | 12 +- docs/Gmail_Credentials_ServiceAccount copy.md | 12 +- docs/Gmail_Credentials_ServiceAccount.md | 12 +- docs/Mailgun_Credentials.md | 12 +- docs/Mandrill_Credentials.md | 12 +- docs/Microsoft365_Credentials_ROPC.md | 12 +- docs/Microsoft365_Credentials_app.md | 12 +- docs/Microsoft365_Credentials_credentials.md | 12 +- docs/Microsoft365_Credentials_managed_id.md | 12 +- docs/Microsoft365_Credentials_user.md | 12 +- docs/Postmark_Credentials.md | 12 +- docs/SMTP_Credentials.md | 12 +- docs/SendGrid_Credentials.md | 12 +- email_sender.go | 197 +++++++++++++++- email_sender_test.go | 223 ++++++++++++++++++ providers/doc.go | 2 +- providers/gmail/gmail_email_sender_test.go | 38 +-- providers/helper_functions.go | 22 +- providers/helper_functions_test.go | 43 +--- .../mailgun/mailgun_email_sender_test.go | 45 ++-- .../mandrill/mandrill_email_sender_test.go | 78 ++---- .../microsoft365_email_sender_test.go | 147 ++++++------ .../postmark/postmark_email_sender_test.go | 71 ++---- .../sendgrid/sendgrid_email_sender_test.go | 73 +++--- providers/ses/ses_email_sender_test.go | 68 +++--- providers/smtp/smtp_email_sender_test.go | 140 ++++------- .../sparkpost/sparkpost_email_sender_test.go | 101 ++++---- 30 files changed, 799 insertions(+), 646 deletions(-) diff --git a/docs/AWS_SES_Credentials.md b/docs/AWS_SES_Credentials.md index 1806735..f056c0f 100644 --- a/docs/AWS_SES_Credentials.md +++ b/docs/AWS_SES_Credentials.md @@ -44,14 +44,13 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: sender, - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage(sender,[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

"). + AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) + + // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Gmail_Credentials_API_Key.md b/docs/Gmail_Credentials_API_Key.md index 78b1193..8279d12 100644 --- a/docs/Gmail_Credentials_API_Key.md +++ b/docs/Gmail_Credentials_API_Key.md @@ -43,14 +43,12 @@ if err != nil { } // Define email message using an alias email address -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with Alias", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) + + // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Gmail_Credentials_JWT.md b/docs/Gmail_Credentials_JWT.md index 1f706db..0edb7e6 100644 --- a/docs/Gmail_Credentials_JWT.md +++ b/docs/Gmail_Credentials_JWT.md @@ -83,14 +83,10 @@ if err != nil { } // Define email message using an alias email address -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with Alias", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Gmail_Credentials_OAuth2.md b/docs/Gmail_Credentials_OAuth2.md index ac2d7ca..8c5a528 100644 --- a/docs/Gmail_Credentials_OAuth2.md +++ b/docs/Gmail_Credentials_OAuth2.md @@ -143,14 +143,10 @@ if err != nil { } // Define email message using an alias email address -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with Alias", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Gmail_Credentials_ServiceAccount copy.md b/docs/Gmail_Credentials_ServiceAccount copy.md index 0627796..5f47fc7 100644 --- a/docs/Gmail_Credentials_ServiceAccount copy.md +++ b/docs/Gmail_Credentials_ServiceAccount copy.md @@ -84,14 +84,10 @@ if err != nil { } // Define email message using an alias email address -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with Alias", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Gmail_Credentials_ServiceAccount.md b/docs/Gmail_Credentials_ServiceAccount.md index e6cbbea..c001a08 100644 --- a/docs/Gmail_Credentials_ServiceAccount.md +++ b/docs/Gmail_Credentials_ServiceAccount.md @@ -77,14 +77,10 @@ if err != nil { } // Define email message using an alias email address -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with Alias", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Mailgun_Credentials.md b/docs/Mailgun_Credentials.md index 30b70d8..04a5710 100644 --- a/docs/Mailgun_Credentials.md +++ b/docs/Mailgun_Credentials.md @@ -41,14 +41,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Mandrill_Credentials.md b/docs/Mandrill_Credentials.md index 818f9d2..a103132 100644 --- a/docs/Mandrill_Credentials.md +++ b/docs/Mandrill_Credentials.md @@ -37,14 +37,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Microsoft365_Credentials_ROPC.md b/docs/Microsoft365_Credentials_ROPC.md index 5954c1a..9f17f35 100644 --- a/docs/Microsoft365_Credentials_ROPC.md +++ b/docs/Microsoft365_Credentials_ROPC.md @@ -51,14 +51,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Microsoft365_Credentials_app.md b/docs/Microsoft365_Credentials_app.md index 0ab7ab3..4fd43bd 100644 --- a/docs/Microsoft365_Credentials_app.md +++ b/docs/Microsoft365_Credentials_app.md @@ -51,14 +51,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Microsoft365_Credentials_credentials.md b/docs/Microsoft365_Credentials_credentials.md index 263125b..a16dbc9 100644 --- a/docs/Microsoft365_Credentials_credentials.md +++ b/docs/Microsoft365_Credentials_credentials.md @@ -50,14 +50,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Microsoft365_Credentials_managed_id.md b/docs/Microsoft365_Credentials_managed_id.md index b3c7ebc..91fc4e5 100644 --- a/docs/Microsoft365_Credentials_managed_id.md +++ b/docs/Microsoft365_Credentials_managed_id.md @@ -38,14 +38,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Microsoft365_Credentials_user.md b/docs/Microsoft365_Credentials_user.md index d3012be..66ee827 100644 --- a/docs/Microsoft365_Credentials_user.md +++ b/docs/Microsoft365_Credentials_user.md @@ -51,14 +51,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/Postmark_Credentials.md b/docs/Postmark_Credentials.md index 6a0c41c..1e3ea52 100644 --- a/docs/Postmark_Credentials.md +++ b/docs/Postmark_Credentials.md @@ -39,14 +39,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/SMTP_Credentials.md b/docs/SMTP_Credentials.md index 0830fbb..26cc14c 100644 --- a/docs/SMTP_Credentials.md +++ b/docs/SMTP_Credentials.md @@ -44,14 +44,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email_or_alias@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/docs/SendGrid_Credentials.md b/docs/SendGrid_Credentials.md index 94267b3..a02ea9e 100644 --- a/docs/SendGrid_Credentials.md +++ b/docs/SendGrid_Credentials.md @@ -36,14 +36,10 @@ if err != nil { } // Define email message -message := gomail.EmailMessage{ - From: "your-email@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email with attachment", - Text: "This is the plain text part of the email.", - HTML: "

This is the HTML part of the email.

", - Attachments: []gomail.Attachment{{Filename: "attachment.jpg", Content: attachmentContent}}, -} +message := *gomail.NewEmailMessage("your-email_or_alias@example.com",[]string{"recipient@example.com"}, "Test Email with attachment", "This is the plain text part of the email."). + SetHTML("

This is the HTML part of the email.

").AddAttachments(gomail.Attachment{ + Filename: "attachment.jpg", Content: attachmentContent, + }) // Send email if err := emailSender.SendEmail(message); err != nil { diff --git a/email_sender.go b/email_sender.go index febc3ee..7ec9d5e 100644 --- a/email_sender.go +++ b/email_sender.go @@ -28,7 +28,7 @@ // // func main() { // sender := sendgrid.NewSendGridEmailSender("your-api-key") -// err := sender.SendEmail(EmailMessage{To:[]string{"recipient@example.com"}, Subject:"Subject", Text:"Email body"}) +// err := sender.SendEmail(gomail.NewEmailMessage([]string{"recipient@example.com"},"Subject","Email body")) // if err != nil { // log.Fatal(err) // } @@ -88,6 +88,190 @@ type EmailMessage struct { Attachments []Attachment `json:"attachments"` // Attachments to be included in the email (optional). } +// NewEmailMessage creates a new EmailMessage with the required fields. +// If the body contains HTML tags, it sets the HTML field; otherwise, it sets the Text field. +// Parameters: +// - from: The sender email address. +// - to: A slice of recipient email addresses. +// - subject: The email subject. +// - body: The content of the email, which can be plain text or HTML. +// +// Returns: +// - *EmailMessage: A pointer to the newly created EmailMessage struct. +func NewEmailMessage(from string, to []string, subject string, body string) *EmailMessage { + email := &EmailMessage{ + From: from, + To: to, + Subject: subject, + } + + if isHTML(body) { + email.HTML = body + } else { + email.Text = body + } + + return email +} + +// NewFullEmailMessage creates a new EmailMessage with all fields. +// Parameters: +// - from: The sender email address. +// - to: A slice of recipient email addresses. +// - subject: The email subject. +// - cc: A slice of CC recipient email addresses (optional). +// - bcc: A slice of BCC recipient email addresses (optional). +// - replyTo: The reply-to email address (optional). +// - textBody: The plain text content of the email. +// - htmlBody: The HTML content of the email (optional). +// - attachments: A slice of attachments (optional). +// +// Returns: +// - *EmailMessage: A pointer to the newly created EmailMessage struct. +func NewFullEmailMessage(from string, to []string, subject string, cc []string, bcc []string, replyTo string, textBody string, htmlBody string, attachments []Attachment) *EmailMessage { + return &EmailMessage{ + From: from, + To: to, + CC: cc, + BCC: bcc, + ReplyTo: replyTo, + Subject: subject, + Text: textBody, + HTML: htmlBody, + Attachments: attachments, + } +} + +// SetFrom sets the sender email address. +// Parameters: +// - from: The sender email address. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetFrom(from string) *EmailMessage { + e.From = from + return e +} + +// SetSubject sets the email subject. +// Parameters: +// - subject: The email subject. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetSubject(subject string) *EmailMessage { + e.Subject = subject + return e +} + +// SetTo sets the recipient email addresses. +// Parameters: +// - to: A slice of recipient email addresses. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetTo(to []string) *EmailMessage { + e.To = to + return e +} + +// SetCC sets the CC recipients email addresses. +// Parameters: +// - cc: A slice of CC recipient email addresses. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetCC(cc []string) *EmailMessage { + e.CC = cc + return e +} + +// SetBCC sets the BCC recipients email addresses. +// Parameters: +// - bcc: A slice of BCC recipient email addresses. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetBCC(bcc []string) *EmailMessage { + e.BCC = bcc + return e +} + +// SetReplyTo sets the reply-to email address. +// Parameters: +// - replyTo: The reply-to email address. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetReplyTo(replyTo string) *EmailMessage { + e.ReplyTo = replyTo + return e +} + +// SetText sets the plain text content of the email. +// Parameters: +// - text: The plain text content of the email. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetText(text string) *EmailMessage { + e.Text = text + return e +} + +// SetHTML sets the HTML content of the email. +// Parameters: +// - html: The HTML content of the email. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetHTML(html string) *EmailMessage { + e.HTML = html + return e +} + +// SetAttachments sets the attachments for the email. +// Parameters: +// - attachments: A slice of Attachment structs to be included in the email. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) SetAttachments(attachments []Attachment) *EmailMessage { + e.Attachments = attachments + return e +} + +// AddToRecipient adds a recipient email address to the To field. +// Parameters: +// - recipient: A recipient email address to be added. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) AddToRecipient(recipient string) *EmailMessage { + e.To = append(e.To, recipient) + return e +} + +// AddCCRecipient adds a recipient email address to the CC field. +// Parameters: +// - recipient: A recipient email address to be added. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) AddCCRecipient(recipient string) *EmailMessage { + e.CC = append(e.CC, recipient) + return e +} + +// AddBCCRecipient adds a recipient email address to the BCC field. +// Parameters: +// - recipient: A recipient email address to be added. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) AddBCCRecipient(recipient string) *EmailMessage { + e.BCC = append(e.BCC, recipient) + return e +} + +// AddAttachment adds an attachment to the email. +// Parameters: +// - attachment: An Attachment struct representing the file to be attached. +// Returns: +// - *EmailMessage: The EmailMessage struct pointer. +func (e *EmailMessage) AddAttachment(attachment Attachment) *EmailMessage { + e.Attachments = append(e.Attachments, attachment) + return e +} + // GetFrom returns the trimmed and validated sender email address. // Returns an empty string if the email is invalid. // @@ -307,3 +491,14 @@ func ValidateEmailSlice(emails []string) []string { func SanitizeInput(input string) string { return html.EscapeString(strings.TrimSpace(input)) } + +// isHTML checks if a string contains HTML tags. +// Parameters: +// - str: The string to check. +// +// Returns: +// - bool: True if the string contains HTML tags, otherwise false. +func isHTML(str string) bool { + htmlRegex := regexp.MustCompile(`(?i)<\/?[a-z][\s\S]*>`) + return htmlRegex.MatchString(str) +} diff --git a/email_sender_test.go b/email_sender_test.go index 529dc0a..a1abfbd 100644 --- a/email_sender_test.go +++ b/email_sender_test.go @@ -287,3 +287,226 @@ func TestEmailHTMLBodySanitzers(t *testing.T) { }) } + +func TestNewEmailMessage(t *testing.T) { + t.Run("create plain text email", func(t *testing.T) { + from := "sender@example.com" + to := []string{"recipient@example.com"} + subject := "Subject" + body := "Email body" + email := NewEmailMessage(from, to, subject, body) + + assert.Equal(t, from, email.From) + assert.Equal(t, to, email.To) + assert.Equal(t, subject, email.Subject) + assert.Equal(t, body, email.Text) + assert.Equal(t, "", email.HTML) + }) + + t.Run("create HTML email", func(t *testing.T) { + from := "sender@example.com" + to := []string{"recipient@example.com"} + subject := "Subject" + body := "

Email body

" + email := NewEmailMessage(from, to, subject, body) + + assert.Equal(t, from, email.From) + assert.Equal(t, to, email.To) + assert.Equal(t, subject, email.Subject) + assert.Equal(t, "", email.Text) + assert.Equal(t, body, email.HTML) + }) +} + +func TestNewFullEmailMessage(t *testing.T) { + t.Run("create full email message", func(t *testing.T) { + from := "sender@example.com" + to := []string{"recipient@example.com"} + cc := []string{"cc@example.com"} + bcc := []string{"bcc@example.com"} + replyTo := "replyto@example.com" + subject := "Subject" + text := "Text body" + html := "

HTML body

" + attachments := []Attachment{ + {Filename: "test.txt", Content: []byte("test content")}, + } + email := NewFullEmailMessage(from, to, subject, cc, bcc, replyTo, text, html, attachments) + + assert.Equal(t, from, email.From) + assert.Equal(t, to, email.To) + assert.Equal(t, cc, email.CC) + assert.Equal(t, bcc, email.BCC) + assert.Equal(t, replyTo, email.ReplyTo) + assert.Equal(t, subject, email.Subject) + assert.Equal(t, text, email.Text) + assert.Equal(t, html, email.HTML) + assert.Equal(t, attachments, email.Attachments) + }) +} + +func TestEmailMessageSetters(t *testing.T) { + email := &EmailMessage{} + + t.Run("SetFrom", func(t *testing.T) { + expected := "sender@example.com" + email.SetFrom(expected) + assert.Equal(t, expected, email.From) + }) + + t.Run("SetSubject", func(t *testing.T) { + expected := "Subject" + email.SetSubject(expected) + assert.Equal(t, expected, email.Subject) + }) + + t.Run("SetTo", func(t *testing.T) { + expected := []string{"recipient@example.com"} + email.SetTo(expected) + assert.Equal(t, expected, email.To) + }) + + t.Run("SetCC", func(t *testing.T) { + expected := []string{"cc@example.com"} + email.SetCC(expected) + assert.Equal(t, expected, email.CC) + }) + + t.Run("SetBCC", func(t *testing.T) { + expected := []string{"bcc@example.com"} + email.SetBCC(expected) + assert.Equal(t, expected, email.BCC) + }) + + t.Run("SetReplyTo", func(t *testing.T) { + expected := "replyto@example.com" + email.SetReplyTo(expected) + assert.Equal(t, expected, email.ReplyTo) + }) + + t.Run("SetText", func(t *testing.T) { + expected := "Text body" + email.SetText(expected) + assert.Equal(t, expected, email.Text) + }) + + t.Run("SetHTML", func(t *testing.T) { + expected := "

HTML body

" + email.SetHTML(expected) + assert.Equal(t, expected, email.HTML) + }) + + t.Run("SetAttachments", func(t *testing.T) { + attachment := Attachment{Filename: "test.txt", Content: []byte("test content")} + email.SetAttachments([]Attachment{attachment}) + assert.Contains(t, email.Attachments, attachment) + assert.EqualValues(t, email.Attachments, []Attachment{attachment}) + }) + + t.Run("AddAttachment", func(t *testing.T) { + attachment := Attachment{Filename: "test.txt", Content: []byte("test content")} + email.AddAttachment(attachment) + assert.Contains(t, email.Attachments, attachment) + }) + + t.Run("AddToRecipient", func(t *testing.T) { + recipient := "newrecipient@example.com" + email.AddToRecipient(recipient) + assert.Contains(t, email.To, recipient) + }) + + t.Run("AddCCRecipient", func(t *testing.T) { + recipient := "newcc@example.com" + email.AddCCRecipient(recipient) + assert.Contains(t, email.CC, recipient) + }) + + t.Run("AddBCCRecipient", func(t *testing.T) { + recipient := "newbcc@example.com" + email.AddBCCRecipient(recipient) + assert.Contains(t, email.BCC, recipient) + }) +} + +func TestAddsEmailMessageToNils(t *testing.T) { + t.Run("create full email message", func(t *testing.T) { + from := "sender@example.com" + to := "recipient@example.com" + cc := "cc@example.com" + bcc := "bcc@example.com" + replyTo := "replyto@example.com" + subject := "Subject" + text := "Text body" + html := "

HTML body

" + attachment := Attachment{Filename: "test.txt", Content: []byte("test content")} + email := NewFullEmailMessage(from, nil, subject, nil, nil, replyTo, text, html, nil) + + email.AddToRecipient(to) + email.AddCCRecipient(cc) + email.AddBCCRecipient(bcc) + email.AddAttachment(attachment) + + assert.Equal(t, from, email.From) + assert.Equal(t, []string{to}, email.To) + assert.Equal(t, []string{cc}, email.CC) + assert.Equal(t, []string{bcc}, email.BCC) + assert.Equal(t, replyTo, email.ReplyTo) + assert.Equal(t, subject, email.Subject) + assert.Equal(t, text, email.Text) + assert.Equal(t, html, email.HTML) + assert.Equal(t, []Attachment{attachment}, email.Attachments) + }) +} + +func TestAttachmentGetters(t *testing.T) { + t.Run("GetFilename", func(t *testing.T) { + attachment := Attachment{Filename: "test.txt"} + assert.Equal(t, "test.txt", attachment.GetFilename()) + assert.Equal(t, "nil_attachment", (*Attachment)(nil).GetFilename()) + }) + + t.Run("GetBase64Content", func(t *testing.T) { + attachment := Attachment{Filename: "test.txt", Content: []byte("hello")} + expected := []byte(base64.StdEncoding.EncodeToString([]byte("hello"))) + assert.Equal(t, expected, attachment.GetBase64Content()) + assert.Equal(t, []byte{}, (*Attachment)(nil).GetBase64Content()) + }) + + t.Run("GetRawContent", func(t *testing.T) { + attachment := Attachment{Filename: "test.txt", Content: []byte("hello")} + expected := []byte("hello") + assert.Equal(t, expected, attachment.GetRawContent()) + assert.Equal(t, []byte{}, (*Attachment)(nil).GetRawContent()) + }) +} + +func TestEmailHTMLBodySanitizers(t *testing.T) { + message := EmailMessage{ + HTML: `
XSS
`, + } + + t.Run("remove potential XSS attack", func(t *testing.T) { + expected := `
XSS
` + result := message.GetHTML() + assert.Equal(t, expected, result) + }) + + message2 := EmailMessage{ + HTML: `
Google`, + } + + t.Run("on methods not allowed", func(t *testing.T) { + expected := `Google` + result := message2.GetHTML() + assert.Equal(t, expected, result) + }) + + message3 := EmailMessage{ + HTML: `

Google

`, + } + t.Run("

can't have href", func(t *testing.T) { + expected := `

Google

` + result := message3.GetHTML() + assert.Equal(t, expected, result) + }) +} diff --git a/providers/doc.go b/providers/doc.go index 784c58f..e0365b6 100644 --- a/providers/doc.go +++ b/providers/doc.go @@ -22,7 +22,7 @@ // // func main() { // sender := sendgrid.NewSendGridEmailSender("your-api-key") -// err := sender.SendEmail(EmailMessage{To:[]string{"recipient@example.com"}, Subject:"Subject", Text:"Email body"}) +// err := sender.SendEmail(gomail.NewEmailMessage([]string{"recipient@example.com"},"Subject","Email body")) // if err != nil { // log.Fatal(err) // } diff --git a/providers/gmail/gmail_email_sender_test.go b/providers/gmail/gmail_email_sender_test.go index 8f5cac3..24e5898 100644 --- a/providers/gmail/gmail_email_sender_test.go +++ b/providers/gmail/gmail_email_sender_test.go @@ -100,12 +100,7 @@ func (m *mockInvalidTokenManager) GetToken() ([]byte, error) { func TestSendEmailWithMockService(t *testing.T) { emailSender := buildMockGmailMessageSenderWrapper(nil) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -113,12 +108,7 @@ func TestSendEmailWithMockService(t *testing.T) { func TestSendEmailWithSendMessageError(t *testing.T) { emailSender := buildMockGmailMessageSenderWrapper(errors.New("send message error")) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -127,12 +117,7 @@ func TestSendEmailWithSendMessageError(t *testing.T) { func TestSendEmailWithMockServiceError(t *testing.T) { emailSender := buildMockGmailMessageSenderWrapper(errors.New("mock service error")) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -141,16 +126,12 @@ func TestSendEmailWithMockServiceError(t *testing.T) { func TestSendEmailWithBCC(t *testing.T) { emailSender := buildMockGmailMessageSenderWrapper(nil) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - BCC: []string{"bcc@example.com"}, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetBCC([]string{"bcc@example.com"}) err := emailSender.SendEmail(message) assert.NoError(t, err) + } func TestNewGmailEmailSenderOauth2(t *testing.T) { @@ -336,12 +317,7 @@ func TestNewGmailEmailSenderJWTAccessInvalidJson(t *testing.T) { func TestSendEmailWithNilGmailService(t *testing.T) { emailSender := &gmailMessageSenderWrapper{} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) diff --git a/providers/helper_functions.go b/providers/helper_functions.go index 42c1b7a..0659ef3 100644 --- a/providers/helper_functions.go +++ b/providers/helper_functions.go @@ -22,19 +22,15 @@ import ( // // Example: // -// message := EmailMessage{ -// From: "sender@example.com", -// To: ["recipient@example.com"], -// Subject: "Test Email", -// Text: "This is a test email.", -// HTML: "

This is a test email.

", -// Attachments: []Attachment{ -// { -// Filename: "test.txt", -// Content: []byte("This is a test attachment."), -// }, -// }, -// } +// message := *gomail.NewEmailMessage( +// "sender@example.com", +// []string["recipient@example.com"], +// "Test Email", +// "This is a test email.",) +// .SetHtml("

This is a test email.

").AddAttachment(Attachment{ +// Filename: "test.txt", +// Content: []byte("This is a test attachment."), +// }) // mimeMessage, err := BuildMimeMessage(message) // if err != nil { // log.Fatalf("Failed to build MIME message: %v", err) diff --git a/providers/helper_functions_test.go b/providers/helper_functions_test.go index 8753b9c..d2ce293 100644 --- a/providers/helper_functions_test.go +++ b/providers/helper_functions_test.go @@ -15,47 +15,27 @@ func TestBuildMimeMessage(t *testing.T) { contains []string }{ { - gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - }, + *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."), []string{"From: sender@example.com", "To: recipient@example.com", "Subject: Test Email", "This is a test email."}, }, { - gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - HTML: "

This is a test email.

", - }, + *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "

This is a test email.

"), []string{"From: sender@example.com", "To: recipient@example.com", "Subject: Test Email", "Content-Type: text/html", "

This is a test email.

"}, }, { - gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - Attachments: []gomail.Attachment{ + *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetAttachments([]gomail.Attachment{ {Filename: "test.txt", Content: []byte("This is a test attachment.")}, - }, - }, + }), []string{"From: sender@example.com", "To: recipient@example.com", "Cc: cc@example.com", "Subject: Test Email", "This is a test email.", "Content-Disposition: attachment; filename=\"test.txt\"", base64.StdEncoding.EncodeToString([]byte("This is a test attachment."))}, }, { - gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - ReplyTo: "reply-to@example.com", - }, + *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("reply-to@example.com"), []string{"From: sender@example.com", "To: recipient@example.com", "Cc: cc@example.com", "Subject: Test Email", "This is a test email.", "Reply-To: reply-to@example.com"}, }, } @@ -82,5 +62,4 @@ func TestStrPtr(t *testing.T) { ptrStr := StrPtr(str) assert.Equal(t, ptrStr, &str) assert.EqualValues(t, ptrStr, &str) - } diff --git a/providers/mailgun/mailgun_email_sender_test.go b/providers/mailgun/mailgun_email_sender_test.go index 3d44a31..6e6d898 100644 --- a/providers/mailgun/mailgun_email_sender_test.go +++ b/providers/mailgun/mailgun_email_sender_test.go @@ -45,22 +45,16 @@ func TestMailgunEmailSender_SendEmail(t *testing.T) { mailgunClient: &mockMailgun{}, } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("replyto@example.com"). + SetHTML("

This is a test email.

"). + SetBCC([]string{"bcc@example.com"}). + AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -71,12 +65,7 @@ func TestMailgunEmailSender_SendEmailWithSendError(t *testing.T) { mailgunClient: &mockMailgunWithError{}, } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -88,12 +77,12 @@ func TestMailgunEmailSender_SendEmailWithEmptyFields(t *testing.T) { mailgunClient: &mockMailgun{}, } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) err := emailSender.SendEmail(message) assert.NoError(t, err) diff --git a/providers/mandrill/mandrill_email_sender_test.go b/providers/mandrill/mandrill_email_sender_test.go index 580d619..140003b 100644 --- a/providers/mandrill/mandrill_email_sender_test.go +++ b/providers/mandrill/mandrill_email_sender_test.go @@ -22,22 +22,16 @@ func TestMandrillEmailSender_SendEmail(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("replyto@example.com"). + SetHTML("

This is a test email.

"). + SetBCC([]string{"bcc@example.com"}). + AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) // Mock server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -56,12 +50,12 @@ func TestMandrillEmailSender_SendEmailWithError(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: string(make([]byte, 1<<20)), // Intentionally large string to cause error - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + string(make([]byte, 1<<20)), // Intentionally large string to cause error + []string{"recipient@example.com"}, + "Test Email", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -71,12 +65,7 @@ func TestMandrillEmailSender_SendEmailNewRequestError(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") emailSender.url = "no a url" emailSender.requestMethod = "no a request method" @@ -90,12 +79,7 @@ func TestMandrillEmailSender_SendEmailWithSendError(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") // Mock server to simulate a server error ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -114,12 +98,7 @@ func TestMandrillEmailSender_SendEmailWithSendTimeOutError(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") // Mock server to simulate a server error ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -139,12 +118,7 @@ func TestMandrillEmailSender_SendEmailWithNon200StatusCode(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") // Mock server to simulate a non-200 status code response ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -163,12 +137,12 @@ func TestMandrillEmailSender_SendEmailWithEmptyFields(t *testing.T) { emailSender, err := NewMandrillEmailSender("test-api-key") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) // Mock server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/providers/microsoft365/microsoft365_email_sender_test.go b/providers/microsoft365/microsoft365_email_sender_test.go index abb07b8..61c246d 100644 --- a/providers/microsoft365/microsoft365_email_sender_test.go +++ b/providers/microsoft365/microsoft365_email_sender_test.go @@ -82,13 +82,12 @@ func TestSendEmail(t *testing.T) { Content: []byte("test_attachment_content"), } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment) reqBuild := mockItemSendMailRequestBuild(nil) sender := &mSGraphEmailSender{ @@ -101,12 +100,12 @@ func TestSendEmail(t *testing.T) { } func TestComposeMessage_PlainTextEmail(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ) msMessage := composeMsMessage(message) @@ -119,12 +118,7 @@ func TestComposeMessage_PlainTextEmail(t *testing.T) { } func TestComposeMessage_HTMLEmail(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - HTML: "

Test Body

", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Subject", "

Test Body

") msMessage := composeMsMessage(message) @@ -135,13 +129,12 @@ func TestComposeMessage_HTMLEmail(t *testing.T) { } func TestComposeMessage_WithCC(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - CC: []string{"cc@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).SetCC([]string{"cc@example.com"}) msMessage := composeMsMessage(message) @@ -150,13 +143,12 @@ func TestComposeMessage_WithCC(t *testing.T) { } func TestComposeMessage_WithBCC(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - BCC: []string{"bcc@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).SetBCC([]string{"bcc@example.com"}) msMessage := composeMsMessage(message) @@ -165,13 +157,12 @@ func TestComposeMessage_WithBCC(t *testing.T) { } func TestComposeMessage_WithReplyTo(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - ReplyTo: "replyto@example.com", - Subject: "Test Subject", - Text: "Test Body", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).SetReplyTo("replyto@example.com") msMessage := composeMsMessage(message) @@ -186,13 +177,12 @@ func TestComposeMessage_WithAttachments(t *testing.T) { Content: []byte("test_attachment_content"), } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment) msMessage := composeMsMessage(message) @@ -204,8 +194,7 @@ func TestComposeMessage_WithAttachments(t *testing.T) { } func TestComposeMessage_EmptyFields(t *testing.T) { - message := gomail.EmailMessage{} - + message := *gomail.NewEmailMessage("", []string{}, "", "") msMessage := composeMsMessage(message) assert.NotNil(t, msMessage) @@ -270,13 +259,12 @@ func TestSendEmail_NoUserRequestBuilder(t *testing.T) { Content: []byte("test_attachment_content"), } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment) sender := &mSGraphEmailSender{ userRequestBuilder: nil, @@ -294,13 +282,12 @@ func TestComposeMessage_LargeAttachment(t *testing.T) { Content: largeContent, } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment) msMessage := composeMsMessage(message) @@ -319,13 +306,12 @@ func TestComposeMessage_MultipleAttachments(t *testing.T) { Content: []byte("test_attachment_content2"), } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment1, attachment2}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment1).AddAttachment(attachment2) msMessage := composeMsMessage(message) @@ -337,10 +323,10 @@ func TestComposeMessage_MultipleAttachments(t *testing.T) { } func TestComposeMessage_MissingFields(t *testing.T) { - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "", "") msMessage := composeMsMessage(message) @@ -361,13 +347,12 @@ func TestSendEmail_FailedSend(t *testing.T) { Content: []byte("test_attachment_content"), } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Subject", - Text: "Test Body", - Attachments: []gomail.Attachment{attachment}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(attachment) clientError := fmt.Errorf("mock error") reqBuild := mockItemSendMailRequestBuild(clientError) diff --git a/providers/postmark/postmark_email_sender_test.go b/providers/postmark/postmark_email_sender_test.go index 24b2dba..1640483 100644 --- a/providers/postmark/postmark_email_sender_test.go +++ b/providers/postmark/postmark_email_sender_test.go @@ -23,22 +23,16 @@ func TestNewPostmarkEmailSender(t *testing.T) { func TestPostmarkEmailSender_SendEmail(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("replyto@example.com"). + SetHTML("

This is a test email.

"). + SetBCC([]string{"bcc@example.com"}). + AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) // Mock server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -56,12 +50,12 @@ func TestPostmarkEmailSender_SendEmail(t *testing.T) { func TestPostmarkEmailSender_SendEmailWithMarshalError(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: string(make([]byte, 1<<20)), // Intentionally large string to cause marshal error - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + string(make([]byte, 1<<20)), // Intentionally large string to cause marshal error + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -71,12 +65,7 @@ func TestPostmarkEmailSender_SendEmailWithRequestCreationError(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") emailSender.url = "no a url" emailSender.requestMethod = "no a request method" @@ -89,12 +78,7 @@ func TestPostmarkEmailSender_SendEmailWithSendError(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") // Mock server to simulate a server error ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -115,12 +99,7 @@ func TestPostmarkEmailSender_SendEmailWithNon200StatusCode(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") // Mock server to simulate a non-200 status code response ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -139,12 +118,12 @@ func TestPostmarkEmailSender_SendEmailWithEmptyFields(t *testing.T) { emailSender, err := NewPostmarkEmailSender("test-server-token") assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) // Mock server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/providers/sendgrid/sendgrid_email_sender_test.go b/providers/sendgrid/sendgrid_email_sender_test.go index 356119d..6621387 100644 --- a/providers/sendgrid/sendgrid_email_sender_test.go +++ b/providers/sendgrid/sendgrid_email_sender_test.go @@ -55,22 +55,16 @@ func TestSendGridEmailSender_SendEmail(t *testing.T) { emailSender := NewMockSendGridEmailSender("test-api-key", ts.URL) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("replyto@example.com"). + SetHTML("

This is a test email.

"). + SetBCC([]string{"bcc@example.com"}). + AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -82,12 +76,7 @@ func TestSendGridEmailSender_SendEmailWithError(t *testing.T) { emailSender := NewMockSendGridEmailSender("test-api-key", ts.URL) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -100,12 +89,7 @@ func TestSendGridEmailSender_SendEmailWithNon200StatusCode(t *testing.T) { emailSender := NewMockSendGridEmailSender("test-api-key", ts.URL) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -118,12 +102,12 @@ func TestSendGridEmailSender_SendEmailWithEmptyFields(t *testing.T) { emailSender := NewMockSendGridEmailSender("test-api-key", ts.URL) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -138,18 +122,15 @@ func TestSendGridEmailSender_SendEmailWithAttachments(t *testing.T) { attachmentContent := "This is a test attachment." attachmentContentBase64 := base64.StdEncoding.EncodeToString([]byte(attachmentContent)) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte(attachmentContent), - }, - }, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte(attachmentContent), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) diff --git a/providers/ses/ses_email_sender_test.go b/providers/ses/ses_email_sender_test.go index 22667fb..959cbbc 100644 --- a/providers/ses/ses_email_sender_test.go +++ b/providers/ses/ses_email_sender_test.go @@ -69,16 +69,16 @@ func TestSESEmailSender_SendEmail(t *testing.T) { sender: "sender@example.com", } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - } + message := *gomail.NewFullEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Email", + []string{"cc@example.com"}, + []string{"bcc@example.com"}, + "replyto@example.com", + "This is a test email.", + "

This is a test email.

", []gomail.Attachment{}, + ) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -92,12 +92,7 @@ func TestSESEmailSender_SendEmailWithError(t *testing.T) { sender: "sender@example.com", } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -112,12 +107,12 @@ func TestSESEmailSender_SendEmailWithEmptyFields(t *testing.T) { sender: "sender@example.com", } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -131,13 +126,15 @@ func TestSESEmailSender_SendEmailWithAttachments(t *testing.T) { sender: "sender@example.com", } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - Attachments: []gomail.Attachment{{Filename: "test.txt", Content: []byte("This is a test attachment.")}}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -151,13 +148,12 @@ func TestSESEmailSender_SendEmailWithReplyTo(t *testing.T) { sender: "sender@example.com", } - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - ReplyTo: "replyto@example.com", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Subject", + "Test Body", + ).SetReplyTo("replyto@example.com") err := emailSender.SendEmail(message) assert.NoError(t, err) diff --git a/providers/smtp/smtp_email_sender_test.go b/providers/smtp/smtp_email_sender_test.go index 7629009..54c504e 100644 --- a/providers/smtp/smtp_email_sender_test.go +++ b/providers/smtp/smtp_email_sender_test.go @@ -181,12 +181,7 @@ func TestSendEmailPlainAuth(t *testing.T) { emailSender, err := NewSmtpEmailSender(host, portInt, "user", "password", AUTH_PLAIN) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.NoError(t, err) @@ -203,12 +198,7 @@ func TestSendEmailCramMD5Auth(t *testing.T) { emailSender, err := NewSmtpEmailSender(host, portInt, "user", "password", AUTH_CRAM_MD5) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.NoError(t, err) @@ -225,12 +215,7 @@ func TestSendEmailError(t *testing.T) { emailSender, err := NewSmtpEmailSender(host, portInt, "user", "wrongpassword", AUTH_PLAIN) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.Error(t, err) @@ -241,12 +226,7 @@ func TestSendEmailInvalidServer(t *testing.T) { emailSender, err := NewSmtpEmailSender("invalid.server.com", 587, "user", "password", AUTH_PLAIN) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.Error(t, err) @@ -256,12 +236,7 @@ func TestSendEmailMissingSettings(t *testing.T) { emailSender, err := NewSmtpEmailSender("", 0, "", "", AUTH_PLAIN) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.Error(t, err) @@ -278,12 +253,7 @@ func TestSendEmailImplicitTLS(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_IMPLICIT) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.NoError(t, err) @@ -303,12 +273,7 @@ func TestSendEmailExplicitTLS(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.NoError(t, err) @@ -325,12 +290,7 @@ func TestSendEmailExplicitErrorTLS(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err = emailSender.SendEmail(message) assert.Error(t, err) @@ -354,12 +314,12 @@ func TestSendTLSEmailConnectionError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -399,12 +359,12 @@ func TestSendTLSEmailEHLOError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "Test Email", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -451,12 +411,12 @@ func TestSendTLSEmailAUTHError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -505,12 +465,12 @@ func TestSendTLSEmailMailError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -560,12 +520,12 @@ func TestSendTLSEmailRcptError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -618,12 +578,12 @@ func TestSendTLSEmailDataError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) @@ -677,12 +637,12 @@ func TestSendTLSEmailDataWriteError(t *testing.T) { emailSender, err := NewSmtpEmailSenderWithConnMethod(host, portInt, "user", "password", AUTH_PLAIN, CONN_TLS) assert.NoError(t, err) - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"nonexistent@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"nonexistent@example.com"}, + "This is a test email.", + "This is a test email.", + ) err = emailSender.SendEmail(message) assert.Error(t, err) diff --git a/providers/sparkpost/sparkpost_email_sender_test.go b/providers/sparkpost/sparkpost_email_sender_test.go index 66f7526..024dad1 100644 --- a/providers/sparkpost/sparkpost_email_sender_test.go +++ b/providers/sparkpost/sparkpost_email_sender_test.go @@ -74,22 +74,16 @@ func TestSparkPostEmailSender_SendEmail(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - HTML: "

This is a test email.

", - CC: []string{"cc@example.com"}, - BCC: []string{"bcc@example.com"}, - ReplyTo: "replyto@example.com", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetCC([]string{"cc@example.com"}). + SetBCC([]string{"bcc@example.com"}). + SetReplyTo("replyto@example.com"). + SetHTML("

This is a test email.

"). + SetBCC([]string{"bcc@example.com"}). + AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -105,12 +99,7 @@ func TestSparkPostEmailSender_SendEmailWithError(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email.") err := emailSender.SendEmail(message) assert.Error(t, err) @@ -133,12 +122,12 @@ func TestSparkPostEmailSender_SendEmailWithEmptyFields(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{}, - Subject: "", - Text: "", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{}, + "", + "", + ) err := emailSender.SendEmail(message) assert.Error(t, err) @@ -160,18 +149,15 @@ func TestSparkPostEmailSender_SendEmailWithAttachments(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - Attachments: []gomail.Attachment{ - { - Filename: "test.txt", - Content: []byte("This is a test attachment."), - }, - }, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Email", + "This is a test email.", + ).AddAttachment(gomail.Attachment{ + Filename: "test.txt", + Content: []byte("This is a test attachment."), + }) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -193,13 +179,12 @@ func TestSparkPostEmailSender_SendEmailWithReplyTo(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - ReplyTo: "replyto@example.com", - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Email", + "This is a test email.", + ).SetReplyTo("replyto@example.com") err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -221,13 +206,12 @@ func TestSparkPostEmailSender_SendEmailWithCC(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - CC: []string{"cc@example.com"}, - } + message := *gomail.NewEmailMessage( + "sender@example.com", + []string{"recipient@example.com"}, + "Test Email", + "This is a test email.", + ).SetCC([]string{"cc@example.com"}) err := emailSender.SendEmail(message) assert.NoError(t, err) @@ -250,13 +234,8 @@ func TestSparkPostEmailSender_SendEmailWithBCC(t *testing.T) { emailSender := &sparkPostEmailSender{client: mockClient.client} - message := gomail.EmailMessage{ - From: "sender@example.com", - To: []string{"recipient@example.com"}, - Subject: "Test Email", - Text: "This is a test email.", - BCC: []string{"bcc@example.com"}, - } + message := *gomail.NewEmailMessage("sender@example.com", []string{"recipient@example.com"}, "Test Email", "This is a test email."). + SetBCC([]string{"bcc@example.com"}) err := emailSender.SendEmail(message) assert.NoError(t, err)