Skip to content

Encoding issues encountered while trying to send multi-part emails #528

@duncanmorrissey

Description

@duncanmorrissey

Describe the bug
When the SDK builds a multipart/form-data request to POST /v3/grants/:id/messages/send, the "message" part (JSON) is left in UTF-8 encoding.
HTTParty (used under-the-hood) expects every multipart field to be ASCII-8BIT/BINARY; with a UTF-8 part it raises
ArgumentError: invalid byte sequence in UTF-8 during HTTParty::Request::Body#generate_multipart.

To Reproduce

```ruby
require "nylas"

nylas = Nylas::Client.new(api_key: ENV["NYLAS_API_KEY"])

payload = {
  to:   [{ email: "someone@example.com", name: "Someone" }],
  subject: "Multipart test",
  body: "hello",
  attachments: [
    # a pdf attachment ≥ 3 MB to force multipart
    Nylas::FileUtils.attach_file_request_builder("/path/to/big.pdf")
  ]
}

nylas.messages.send(identifier: ENV["GRANT_ID"], request_body: payload)

Stack trace (shortened):

…/gems/httparty-0.23.1/lib/httparty/request/body.rb:54:in `block in generate_multipart'
…/nylas-6.5.0/lib/nylas/handler/http_client.rb:157:in `httparty_execute'

Expected behavior
Nylas::Messages#send should succeed without raising, regardless of attachment size or type

SDK Version:

  • nylas-ruby 6.5.0
  • Ruby 3.2.5
  • httparty 0.23.1

Additional context

A local monkey-patch in my rails app fixed the issue by forcing the "message" part to be binary

module Nylas::FileUtils
  class << self
    alias_method :_orig_build_form_request, :build_form_request
    def build_form_request(request_body)
      form_data, files = _orig_build_form_request(request_body)
      form_data["message"] = form_data["message"].dup.force_encoding(Encoding::BINARY)
      [form_data, files]
    end
  end
end

A similar change inside FileUtils.build_form_request (or at the call-site before passing to HTTParty) would permanently resolve the error.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions