nylas-ruby icon indicating copy to clipboard operation
nylas-ruby copied to clipboard

Set multipart/form-data header while sending attachments > 3mb

Open byteg opened this issue 1 year ago • 3 comments

Describe the bug "Content-Type": "multipart/form-data" is not set when I try to send attachments > 3mb.

To Reproduce Some steps involved to reproduce the bug and any code samples you can share.

request_body = {
  subject: "send large attachments",
  body: "large attachments",
  attachments: [
    {  content: "<large content here>", content_type: "", size: SIZE, filename: "filename"}
  ],
  to: [{email: "[email protected]"}]
}
response, _request_id = nylas.messages.send(identifier:, request_body:)

I get an error: Nylas::NylasApiError (request must be of content-type multipart/form-data or application/json): Expected behavior Large attachments are sent correctly

SDK Version: nylas (6.1.1)

Additional context Update: IT DOES NOT HELP Possible fix: https://github.com/nylas/nylas-ruby/blob/main/lib/nylas/handler/http_client.rb#L101 Add line: resulting_headers["Content-type"] = "multipart/form-data"

byteg avatar Aug 28 '24 10:08 byteg

Hey @byteg! Thanks for opening this issue. Can I ask what the type of object is getting sent?

mrashed-dev avatar Sep 24 '24 16:09 mrashed-dev

I think the solution here is to send the attachment as a Nylas file object if it is >3mb. I have the following code that is working for me.

if attachment.blob.byte_size > 3.megabytes
  Nylas::FileUtils.attach_file_request_builder(file_path, name)
else
  {
    content: base64_content,
    content_type: attachment.content_type,
    filename: name,
  }
end

-- UPDATE -- Looking deeper into it, I think you can just use Nylas::FileUtils.attach_file_request_builder for all requests and it should handle them (as of 6.1.1). The one bug here is you lose filename if the attachment is >3MB, but that's probably a separate bug.

maxmetcalf12 avatar Nov 13 '24 18:11 maxmetcalf12

@maxmetcalf12 Thanks for chiming in, the attach_file_request_builder should be able to handle the attachments above 3MB, and should send them as multipart/form-data.

You can also change the filename + send them as inline for example:

file = Nylas::FileUtils.attach_file_request_builder("/file_path.jpg", "test_name.jpg", "custom_content_id")
request_body = {
  'subject': 'test',
  'to': [{email: '[email protected]'}],
  "body": "<html><body> <h1>Hello My Friend!</h1><p>This is an HTML email sent using the Nylas API.</p><img src='cid:custom_content_id' alt='Attached Image' /></body></html>",
  'attachments': [file]
}

cc @byteg: https://github.com/nylas/nylas-ruby/pull/506

SubashPradhan avatar Nov 29 '24 14:11 SubashPradhan

I believe this should be fixed in https://github.com/nylas/nylas-ruby/pull/524

AaronDDM avatar Jun 11 '25 14:06 AaronDDM