Skip to content

Commit b4f86e3

Browse files
authored
feat: gzip mail body when content-encoding is set to gzip (#468)
[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.
1 parent bdbdc21 commit b4f86e3

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

base_interface.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sendgrid
22

33
import (
4+
"bytes"
5+
"compress/gzip"
46
"context"
57
"errors"
68
"net/http"
@@ -61,6 +63,24 @@ func (cl *Client) Send(email *mail.SGMailV3) (*rest.Response, error) {
6163
// SendWithContext sends an email through Twilio SendGrid with context.Context.
6264
func (cl *Client) SendWithContext(ctx context.Context, email *mail.SGMailV3) (*rest.Response, error) {
6365
cl.Body = mail.GetRequestBody(email)
66+
// when Content-Encoding header is set to "gzip"
67+
// mail body is compressed using gzip according to
68+
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#mail-body-compression
69+
if cl.Headers["Content-Encoding"] == "gzip" {
70+
var gzipped bytes.Buffer
71+
gz := gzip.NewWriter(&gzipped)
72+
if _, err := gz.Write(cl.Body); err != nil {
73+
return nil, err
74+
}
75+
if err := gz.Flush(); err != nil {
76+
return nil, err
77+
}
78+
if err := gz.Close(); err != nil {
79+
return nil, err
80+
}
81+
82+
cl.Body = gzipped.Bytes()
83+
}
6484
return MakeRequestWithContext(ctx, cl.Request)
6585
}
6686

sendgrid_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,156 @@ func Test_test_mail_batch__batch_id__get(t *testing.T) {
16021602
assert.Equal(t, 200, response.StatusCode, "Wrong status code returned")
16031603
}
16041604

1605+
func Test_test_send_client_with_mail_body_compression_enabled(t *testing.T) {
1606+
apiKey := "SENDGRID_API_KEY"
1607+
client := NewSendClient(apiKey)
1608+
client.Headers["Content-Encoding"] = "gzip"
1609+
1610+
emailBytes := []byte(` {
1611+
"asm": {
1612+
"group_id": 1,
1613+
"groups_to_display": [
1614+
1,
1615+
2,
1616+
3
1617+
]
1618+
},
1619+
"attachments": [
1620+
{
1621+
"content": "[BASE64 encoded content block here]",
1622+
"content_id": "ii_139db99fdb5c3704",
1623+
"disposition": "inline",
1624+
"filename": "file1.jpg",
1625+
"name": "file1",
1626+
"type": "jpg"
1627+
}
1628+
],
1629+
"batch_id": "[YOUR BATCH ID GOES HERE]",
1630+
"categories": [
1631+
"category1",
1632+
"category2"
1633+
],
1634+
"content": [
1635+
{
1636+
"type": "text/html",
1637+
"value": "<html><p>Hello, world!</p><img src=[CID GOES HERE]></img></html>"
1638+
}
1639+
],
1640+
"custom_args": {
1641+
"New Argument 1": "New Value 1",
1642+
"activationAttempt": "1",
1643+
"customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]"
1644+
},
1645+
"from": {
1646+
"email": "sam.smith@example.com",
1647+
"name": "Sam Smith"
1648+
},
1649+
"headers": {},
1650+
"ip_pool_name": "[YOUR POOL NAME GOES HERE]",
1651+
"mail_settings": {
1652+
"bcc": {
1653+
"email": "ben.doe@example.com",
1654+
"enable": true
1655+
},
1656+
"bypass_list_management": {
1657+
"enable": true
1658+
},
1659+
"footer": {
1660+
"enable": true,
1661+
"html": "<p>Thanks</br>The Twilio SendGrid Team</p>",
1662+
"text": "Thanks,/n The Twilio SendGrid Team"
1663+
},
1664+
"sandbox_mode": {
1665+
"enable": false
1666+
},
1667+
"spam_check": {
1668+
"enable": true,
1669+
"post_to_url": "http://example.com/compliance",
1670+
"threshold": 3
1671+
}
1672+
},
1673+
"personalizations": [
1674+
{
1675+
"bcc": [
1676+
{
1677+
"email": "sam.doe@example.com",
1678+
"name": "Sam Doe"
1679+
}
1680+
],
1681+
"cc": [
1682+
{
1683+
"email": "jane.doe@example.com",
1684+
"name": "Jane Doe"
1685+
}
1686+
],
1687+
"custom_args": {
1688+
"New Argument 1": "New Value 1",
1689+
"activationAttempt": "1",
1690+
"customerAccountNumber": "[CUSTOMER ACCOUNT NUMBER GOES HERE]"
1691+
},
1692+
"headers": {
1693+
"X-Accept-Language": "en",
1694+
"X-Mailer": "MyApp"
1695+
},
1696+
"send_at": 1409348513,
1697+
"subject": "Hello, World!",
1698+
"substitutions": {
1699+
"id": "substitutions",
1700+
"type": "object"
1701+
},
1702+
"to": [
1703+
{
1704+
"email": "john.doe@example.com",
1705+
"name": "John Doe"
1706+
}
1707+
]
1708+
}
1709+
],
1710+
"reply_to": {
1711+
"email": "sam.smith@example.com",
1712+
"name": "Sam Smith"
1713+
},
1714+
"send_at": 1409348513,
1715+
"subject": "Hello, World!",
1716+
"template_id": "[YOUR TEMPLATE ID GOES HERE]",
1717+
"tracking_settings": {
1718+
"click_tracking": {
1719+
"enable": true,
1720+
"enable_text": true
1721+
},
1722+
"ganalytics": {
1723+
"enable": true,
1724+
"utm_campaign": "[NAME OF YOUR REFERRER SOURCE]",
1725+
"utm_content": "[USE THIS SPACE TO DIFFERENTIATE YOUR EMAIL FROM ADS]",
1726+
"utm_medium": "[NAME OF YOUR MARKETING MEDIUM e.g. email]",
1727+
"utm_name": "[NAME OF YOUR CAMPAIGN]",
1728+
"utm_term": "[IDENTIFY PAID KEYWORDS HERE]"
1729+
},
1730+
"open_tracking": {
1731+
"enable": true,
1732+
"substitution_tag": "%opentrack"
1733+
},
1734+
"subscription_tracking": {
1735+
"enable": true,
1736+
"html": "If you would like to unsubscribe and stop receiving these emails <% clickhere %>.",
1737+
"substitution_tag": "<%click here%>",
1738+
"text": "If you would like to unsubscribe and stop receiving these emails <% click here %>."
1739+
}
1740+
}
1741+
}`)
1742+
email := &mail.SGMailV3{}
1743+
err := json.Unmarshal(emailBytes, email)
1744+
assert.Nil(t, err, fmt.Sprintf("Unmarshal error: %v", err))
1745+
client.Request.Headers["X-Mock"] = "202"
1746+
response, err := client.Send(email)
1747+
if err != nil {
1748+
t.Log(err)
1749+
}
1750+
t.Log(response)
1751+
assert.Equal(t, 202, response.StatusCode, "Wrong status code returned")
1752+
1753+
}
1754+
16051755
func Test_test_send_client(t *testing.T) {
16061756
apiKey := "SENDGRID_APIKEY"
16071757
client := NewSendClient(apiKey)

0 commit comments

Comments
 (0)