hackney icon indicating copy to clipboard operation
hackney copied to clipboard

multipart needs more examples

Open silviucpp opened this issue 9 years ago • 4 comments

Hello,

I'm trying to send an email via mailgun.com using the hackney and I have some issues sending attachments (which requires multipart).

https://documentation.mailgun.com/api-sending.html#sending

Basically my interest fields are:

  • from
  • to
  • subject
  • text
  • attachment File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.

I tried the following:

 PayloadBase =
                [
                    {<<"from">>, From},
                    {<<"to">>, To},
                    {<<"subject">>, Subject},
                    {<<"text">>, TextBody},
                    {<<"html">>, HtmlBody}
            ],

            Payload = case Attachment of
               null ->
                   {form, PayloadBase};
               _->
                   {multipart, PayloadBase ++ [{file, Attachment}]}
            end,

But for some reason the attachment is not sent.. Everything else works as expected. I don't see how I can set the filed name to "attachement" as required by mailgun

Silviu

silviucpp avatar Mar 07 '16 21:03 silviucpp

More or less this seems to be a hackney bug/limitation.

Seems when you send a file the name is hard-coded to "file" and you cannot change it into something else. : like for example attachment as required by mailgun.

I also found this code where hackney is doing this:

Disposition = {<<"form-data">>,
               [{<<"name">>, <<"\"file\"">>},
                {<<"filename">>, <<"\"", FName/binary, "\"">>}]},

Any idea if my theory is true ?

Silviu

silviucpp avatar Mar 12 '16 19:03 silviucpp

file is indeed a specific case if it's your question. It's acting like most server are expecting it.

Nothing prevent you to use your own solution either in stream or in the given body using {Name, Bin, Disposition, ExtraHeaders} .

The code that handle the file is here: https://github.com/benoitc/hackney/blob/master/src/hackney_multipart.erl#L57-L77

and the mp file header: https://github.com/benoitc/hackney/blob/master/src/hackney_multipart.erl#L224-L240

If something is missing let me know.

benoitc avatar Mar 23 '16 09:03 benoitc

Hello,

I think adding the possibility to customize the name and file name on the existing file support [{file, ...}] support will provide developers more freedom.

Silviu

silviucpp avatar Mar 23 '16 10:03 silviucpp

well this is exactly what you can do here:

https://github.com/benoitc/hackney/blob/master/src/hackney_multipart.erl#L232

For example:

FName = hackney_bstr:to_binary(filename:basename(Path)),
MyName = <<"attachments">>
 Disposition = {<<"form-data">>,
                   [{<<"name">>, <<"\"", MyName/binary, "\"">>},
                    {<<"filename">>, <<"\"", FName/binary, "\"">>}]},
ExtraHeaders = [],
{file, Path, Disposition, ExtraHeaders}

I am not sure it needs another case. I already handle too much specific cases already :)

benoitc avatar Mar 23 '16 10:03 benoitc