Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multipart needs more examples #292

Open
silviucpp opened this issue Mar 7, 2016 · 4 comments
Open

multipart needs more examples #292

silviucpp opened this issue Mar 7, 2016 · 4 comments

Comments

@silviucpp
Copy link

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
Copy link
Author

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

@benoitc
Copy link
Owner

benoitc commented Mar 23, 2016

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.

@silviucpp
Copy link
Author

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

@benoitc
Copy link
Owner

benoitc commented Mar 23, 2016

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants