Skip to content

Commit fd2b502

Browse files
committed
fix: use Base64.strict_encode64 instead of Base64.encode64
Attachments must be base64 encoded, using `Base64.strict_encode64` where no line feeds are added.
1 parent 02b6a2a commit fd2b502

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

lib/sendgrid/helpers/mail/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def encode(io)
4949
# in binary mode, but at least we can faithfully convey the
5050
# bytes.
5151
str = str.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
52-
Base64.encode64 str
52+
Base64.strict_encode64 str
5353
end
5454
end
5555
end

mail_helper_v3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ plain_text_content = 'and easy to do anywhere, even with Ruby'
307307
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
308308
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
309309
bytes = File.read('/path/to/the/attachment.pdf')
310-
encoded = Base64.encode64(bytes)
310+
encoded = Base64.strict_encode64(bytes)
311311
msg.add_attachment('balance_001.pdf',
312312
encoded,
313313
'application/pdf',

test/sendgrid/helpers/mail/test_attachment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ def test_io_enocding
1818
attachment.content = StringIO.new(SAMPLE_INPUT)
1919

2020
expected = {
21-
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLD\nvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCdu\nIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4g\nYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBC\nbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8\nbiB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo=\n"
21+
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4gYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo="
2222
}
2323

2424
json = attachment.to_json
2525

2626
# Double check that the decoded json matches original input.
27-
decoded = Base64.decode64(json["content"]).force_encoding('UTF-8').encode
27+
decoded = Base64.strict_decode64(json["content"]).force_encoding('UTF-8').encode
2828

2929
assert_equal(decoded, SAMPLE_INPUT)
3030

0 commit comments

Comments
 (0)