Skip to content

Commit

Permalink
feat!: gzip mail body when content-encoding is set to gzip
Browse files Browse the repository at this point in the history
[Mail body
compression](https://docs.sendgrid.com/api-reference/mail-send/mail-send#mail-body-compression)
is supported on V3 api. This patch added the support that when a
client sets the content-encoding header to gzip, send compresses the
mail body with gzip.
  • Loading branch information
Bankq committed Aug 6, 2023
1 parent bdbdc21 commit 2235736
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions base_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sendgrid

import (
"bytes"
"compress/gzip"
"context"
"errors"
"net/http"
Expand Down Expand Up @@ -61,6 +63,15 @@ func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
// SendWithContext sends an email through Twilio SendGrid with context.Context.
func (cl *Client) SendWithContext(ctx context.Context, email *mail.SGMailV3) (*rest.Response, error) {
cl.Body = mail.GetRequestBody(email)
if cl.Headers["Content-Encoding"] == "gzip" {
var gzipped bytes.Buffer
gz := gzip.NewWriter(&gzipped)
defer gz.Close()
if _, err := gz.Write(cl.Body); err != nil {
return nil, err
}
cl.Body = gzipped.Bytes()
}
return MakeRequestWithContext(ctx, cl.Request)
}

Expand Down

0 comments on commit 2235736

Please sign in to comment.