From b99845b938eaa591930b3f670bee174c3dfcb8f7 Mon Sep 17 00:00:00 2001 From: Christian Mehlmauer <105281+firefart@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:56:43 +0200 Subject: [PATCH 1/2] allow to specify the content type --- service/sendgrid/sendgrid.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go index a23bd5cb..7851af7b 100644 --- a/service/sendgrid/sendgrid.go +++ b/service/sendgrid/sendgrid.go @@ -12,12 +12,23 @@ import ( // 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/ @@ -27,6 +38,7 @@ func New(apiKey, senderAddress, senderName string) *SendGrid { senderAddress: senderAddress, senderName: senderName, receiverAddresses: []string{}, + usePlainText: false, } } @@ -36,11 +48,30 @@ func (s *SendGrid) AddReceivers(addresses ...string) { 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 + } +} + // 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) // Create a new personalization instance to be able to add multiple receiver addresses. personalization := mail.NewPersonalization() From 1bae4d61c8c18623069ad73fc987851c401665a3 Mon Sep 17 00:00:00 2001 From: Christian Mehlmauer <105281+firefart@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:57:28 +0200 Subject: [PATCH 2/2] context can now be passed in --- service/sendgrid/sendgrid.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/service/sendgrid/sendgrid.go b/service/sendgrid/sendgrid.go index 7851af7b..741db35e 100644 --- a/service/sendgrid/sendgrid.go +++ b/service/sendgrid/sendgrid.go @@ -86,18 +86,13 @@ func (s SendGrid) Send(ctx context.Context, subject, message string) error { 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) + } - 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") } return nil