Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for defining the name of the email recipient #99

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions mailing/sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"net/http"
"strings"
)

// sendgridEmailService implements the Sender interface using Sendgrid.
Expand Down Expand Up @@ -69,11 +70,26 @@ func newSendgridEmailSender(client sendgridSender, injector contentInjector) Sen
}
}

// parseSendgridEmails converts the given slice of emails to sendgrid emails.
func parseSendgridEmails(emails []string) []*mail.Email {
out := make([]*mail.Email, len(emails))
for i := 0; i < len(emails); i++ {
out[i] = mail.NewEmail("", emails[i])
// parseSendgridEmails converts the given slice of recipients to sendgrid emails.
// Each recipient must be in the following format: "FirstName LastName:email@example.org".
func parseSendgridEmails(recipients []string) []*mail.Email {
out := make([]*mail.Email, len(recipients))
for i := 0; i < len(recipients); i++ {
name, addr := parseRecipient(recipients[i])
if len(name) == 0 || len(addr) == 0 {
continue
}
out[i] = mail.NewEmail(name, addr)
}
return out
}

func parseRecipient(recipient string) (name string, address string) {
params := strings.Split(recipient, ":")
if len(params) != 2 {
return "", ""
}
name = params[0]
address = params[1]
return
}
2 changes: 1 addition & 1 deletion mailing/sendgrid_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type sendgridEmailBuilder struct {

// Sender sets the email address where the resulting email comes from.
func (b sendgridEmailBuilder) Sender(from string) sendgridEmailBuilder {
b.personalization.AddFrom(mail.NewEmail("", from))
b.personalization.AddFrom(mail.NewEmail(parseRecipient(from)))
return b
}

Expand Down
26 changes: 14 additions & 12 deletions mailing/sendgrid_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mailing

import (
"fmt"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/stretchr/testify/suite"
"testing"
Expand All @@ -23,32 +24,33 @@ func (suite *SendgridEmailBuilderTestSuite) SetupTest() {
}

func (suite *SendgridEmailBuilderTestSuite) TestSender() {
const sender = "noreply@gazebosim.org"
result := suite.builder.Sender(sender).personalization.From.Address
suite.Assert().Equal(sender, result)
const sender = "Gazebo Web:noreply@gazebosim.org"
from := suite.builder.Sender(sender).personalization.From

suite.Assert().Equal(sender, fmt.Sprintf("%s:%s", from.Name, from.Address))
}

func (suite *SendgridEmailBuilderTestSuite) TestRecipients() {
recipients := []string{"test1@gazebosim.org", "test2@gazebosim.org"}
recipients := []string{"Test 1:test1@gazebosim.org", "Test 2:test2@gazebosim.org"}
result := suite.builder.Recipients(recipients).personalization.To
for _, to := range result {
suite.Assert().Contains(recipients, to.Address)
suite.Assert().Contains(recipients, fmt.Sprintf("%s:%s", to.Name, to.Address))
}
}

func (suite *SendgridEmailBuilderTestSuite) TestCCs() {
recipients := []string{"test1@gazebosim.org", "test2@gazebosim.org"}
recipients := []string{"Test 1:test1@gazebosim.org", "Test 2:test2@gazebosim.org"}
result := suite.builder.CC(recipients).personalization.CC
for _, cc := range result {
suite.Assert().Contains(recipients, cc.Address)
suite.Assert().Contains(recipients, fmt.Sprintf("%s:%s", cc.Name, cc.Address))
}
}

func (suite *SendgridEmailBuilderTestSuite) TestBBCs() {
recipients := []string{"test1@gazebosim.org", "test2@gazebosim.org"}
recipients := []string{"Test 1:test1@gazebosim.org", "Test 2:test2@gazebosim.org"}
result := suite.builder.BCC(recipients).personalization.BCC
for _, bcc := range result {
suite.Assert().Contains(recipients, bcc.Address)
suite.Assert().Contains(recipients, fmt.Sprintf("%s:%s", bcc.Name, bcc.Address))
}
}

Expand Down Expand Up @@ -96,9 +98,9 @@ func (suite *SendgridEmailBuilderTestSuite) TestBuild() {

m := suite.builder.
Sender("noreply@gazebosim.org").
Recipients([]string{"test@gazebosim.org"}).
CC([]string{"cc@gazebosim.org"}).
BCC([]string{"bcc@gazebosim.org"}).
Recipients([]string{"Test Example:test@gazebosim.org"}).
CC([]string{"Test CC:cc@gazebosim.org"}).
BCC([]string{"Test BCC:bcc@gazebosim.org"}).
Subject("Test email subject").
Template(key, data).
Build()
Expand Down
6 changes: 3 additions & 3 deletions mailing/sendgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ func (suite *SendgridTestSuite) TestSendEmail_WithDynamicTemplates() {
}

func (suite *SendgridTestSuite) TestParseEmail() {
recipients := []string{"test2@gazebosim.org", "test3@gazebosim.org"}
recipients := []string{"Test 2:test2@gazebosim.org", "Test 3:test3@gazebosim.org"}

expected := []*mail.Email{
mail.NewEmail("", recipients[0]),
mail.NewEmail("", recipients[1]),
mail.NewEmail("Test 2", "test2@gazebosim.org"),
mail.NewEmail("Test 3", "test3@gazebosim.org"),
}

result := parseSendgridEmails(recipients)
Expand Down
Loading