Set multipart/form-data header while sending attachments > 3mb
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"
Hey @byteg! Thanks for opening this issue. Can I ask what the type of object is getting sent?
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 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
I believe this should be fixed in https://github.com/nylas/nylas-ruby/pull/524