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

Sendgrid: Allow to specify the content type #871

Merged
merged 4 commits into from
Nov 2, 2024
Merged
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
50 changes: 38 additions & 12 deletions service/sendgrid/sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@

// SendGrid struct holds necessary data to communicate with the SendGrid API.
type SendGrid struct {
usePlainText bool
client *sendgrid.Client
senderAddress string
senderName string
receiverAddresses []string
}

// BodyType is used to specify the format of the body.
type BodyType int

const (
// PlainText is used to specify that the body is plain text.
PlainText BodyType = iota
// HTML is used to specify that the body is HTML.
HTML
)

// New returns a new instance of a SendGrid notification service.
// You will need a SendGrid API key.
// See https://sendgrid.com/docs/for-developers/sending-email/api-getting-started/
Expand All @@ -27,6 +38,7 @@
senderAddress: senderAddress,
senderName: senderName,
receiverAddresses: []string{},
usePlainText: false,

Check warning on line 41 in service/sendgrid/sendgrid.go

View check run for this annotation

Codecov / codecov/patch

service/sendgrid/sendgrid.go#L41

Added line #L41 was not covered by tests
}
}

Expand All @@ -36,11 +48,30 @@
s.receiverAddresses = append(s.receiverAddresses, addresses...)
}

// BodyFormat can be used to specify the format of the body.
// Default BodyType is HTML.
func (s *SendGrid) BodyFormat(format BodyType) {
switch format {
case PlainText:
s.usePlainText = true
case HTML:
s.usePlainText = false
default:
s.usePlainText = false

Check warning on line 60 in service/sendgrid/sendgrid.go

View check run for this annotation

Codecov / codecov/patch

service/sendgrid/sendgrid.go#L53-L60

Added lines #L53 - L60 were not covered by tests
}
}

// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
// html as markup language.
func (s SendGrid) Send(ctx context.Context, subject, message string) error {
from := mail.NewEmail(s.senderName, s.senderAddress)
content := mail.NewContent("text/html", message)
var contentType string
if s.usePlainText {
contentType = "text/plain"
} else {
contentType = "text/html"
}
content := mail.NewContent(contentType, message)

Check warning on line 74 in service/sendgrid/sendgrid.go

View check run for this annotation

Codecov / codecov/patch

service/sendgrid/sendgrid.go#L68-L74

Added lines #L68 - L74 were not covered by tests

// Create a new personalization instance to be able to add multiple receiver addresses.
personalization := mail.NewPersonalization()
Expand All @@ -55,18 +86,13 @@
mailMessage.AddContent(content)
mailMessage.SetFrom(from)

select {
case <-ctx.Done():
return ctx.Err()
default:
resp, err := s.client.Send(mailMessage)
if err != nil {
return fmt.Errorf("send message: %w", err)
}
resp, err := s.client.SendWithContext(ctx, mailMessage)
if err != nil {
return fmt.Errorf("send message: %w", err)
}

Check warning on line 92 in service/sendgrid/sendgrid.go

View check run for this annotation

Codecov / codecov/patch

service/sendgrid/sendgrid.go#L89-L92

Added lines #L89 - L92 were not covered by tests

if resp.StatusCode != http.StatusAccepted {
return errors.New("the SendGrid endpoint did not accept the message")
}
if resp.StatusCode != http.StatusAccepted {
return errors.New("the SendGrid endpoint did not accept the message")

Check warning on line 95 in service/sendgrid/sendgrid.go

View check run for this annotation

Codecov / codecov/patch

service/sendgrid/sendgrid.go#L94-L95

Added lines #L94 - L95 were not covered by tests
}

return nil
Expand Down