Skip to content

Commit

Permalink
Use Base64.strict_encode64 instead of Base64.encode64
Browse files Browse the repository at this point in the history
Attachments must be base64 encoded, using `Base64.strict_encode64` where no
line feeds are added.
  • Loading branch information
yujideveloper committed Aug 2, 2023
1 parent e17be75 commit 7892bc6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/sendgrid/helpers/mail/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def encode(io)
# in binary mode, but at least we can faithfully convey the
# bytes.
str = str.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
Base64.encode64 str
Base64.strict_encode64 str
end
end
end
2 changes: 1 addition & 1 deletion mail_helper_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ plain_text_content = 'and easy to do anywhere, even with Ruby'
html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
bytes = File.read('/path/to/the/attachment.pdf')
encoded = Base64.encode64(bytes)
encoded = Base64.strict_encode64(bytes)
msg.add_attachment('balance_001.pdf',
encoded,
'application/pdf',
Expand Down
4 changes: 2 additions & 2 deletions test/sendgrid/helpers/mail/test_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def test_io_enocding
attachment.content = StringIO.new(SAMPLE_INPUT)

expected = {
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLD\nvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCdu\nIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4g\nYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBC\nbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8\nbiB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo=\n"
"content" => "RXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwpFcyBibMO8aHQgc28gZ3LDvG4gd2llIEJsw7x0ZW4gYmzDvGgnbiBpbSBGcsO8aGxpbmcKRXMgYmzDvGh0IHNvIGdyw7xuIHdpZSBCbMO8dGVuIGJsw7xoJ24gaW0gRnLDvGhsaW5nCkVzIGJsw7xodCBzbyBncsO8biB3aWUgQmzDvHRlbiBibMO8aCduIGltIEZyw7xobGluZwo="
}

json = attachment.to_json

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

assert_equal(decoded, SAMPLE_INPUT)

Expand Down

0 comments on commit 7892bc6

Please sign in to comment.