Erlang hackney:使用附件在 mailgun.com 中发送邮件
Erlang hackney : send a mail in mailgun.com using attachement
我正在尝试使用 hackney 通过 mailgun.com 发送电子邮件,但在发送附件时遇到了一些问题(这需要多部分)。
https://documentation.mailgun.com/api-sending.html#sending
基本上我感兴趣的领域是:
from
to
subject
text
attachment File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.
我尝试了以下方法:
PayloadBase =[
{<<"from">>, From},
{<<"to">>, To},
{<<"subject">>, Subject},
{<<"text">>, TextBody},
{<<"html">>, HtmlBody}
],
Payload = case Attachment of
null ->
{form, PayloadBase};
_->
{multipart, PayloadBase ++ [{file, Attachment}]}
end,
但由于某种原因,附件未发送。其他一切正常。
我看不出如何根据 mailgun 的要求将文件名设置为 "attachment" .. 这就是我怀疑的错误
我没有使用过 mailgun,但我相信您需要将 attachment
作为字段名称。见 examples at the bottom of the page 你 posted:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='foo@example.com' \
-F cc='bar@example.com' \
-F bcc='baz@example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/cartman.jpg \
-F attachment=@files/cartman.png
如果先使用 curl
会更容易,然后再 debug what headers curl sends 到服务器。然后你可以在 Erlang 中模仿它。
此 post 解释了 what multipart/form-data
is and points to the W3 document,提供了数据应如何编码的示例。
以下代码将解决问题:
Payload2 = case Attachment of
null ->
{form, PayloadBase};
_->
FName = hackney_bstr:to_binary(filename:basename(Attachment)),
MyName = <<"attachment">>,
Disposition = {<<"form-data">>, [{<<"name">>, <<"\"", MyName/binary, "\"">>}, {<<"filename">>, <<"\"", FName/binary, "\"">>}]},
ExtraHeaders = [],
{multipart, PayloadBase ++ [{file, Attachment, Disposition, ExtraHeaders}]}
end,
西尔维
我正在尝试使用 hackney 通过 mailgun.com 发送电子邮件,但在发送附件时遇到了一些问题(这需要多部分)。
https://documentation.mailgun.com/api-sending.html#sending
基本上我感兴趣的领域是:
from
to
subject
text
attachment File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.
我尝试了以下方法:
PayloadBase =[
{<<"from">>, From},
{<<"to">>, To},
{<<"subject">>, Subject},
{<<"text">>, TextBody},
{<<"html">>, HtmlBody}
],
Payload = case Attachment of
null ->
{form, PayloadBase};
_->
{multipart, PayloadBase ++ [{file, Attachment}]}
end,
但由于某种原因,附件未发送。其他一切正常。 我看不出如何根据 mailgun 的要求将文件名设置为 "attachment" .. 这就是我怀疑的错误
我没有使用过 mailgun,但我相信您需要将 attachment
作为字段名称。见 examples at the bottom of the page 你 posted:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='foo@example.com' \
-F cc='bar@example.com' \
-F bcc='baz@example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/cartman.jpg \
-F attachment=@files/cartman.png
如果先使用 curl
会更容易,然后再 debug what headers curl sends 到服务器。然后你可以在 Erlang 中模仿它。
此 post 解释了 what multipart/form-data
is and points to the W3 document,提供了数据应如何编码的示例。
以下代码将解决问题:
Payload2 = case Attachment of
null ->
{form, PayloadBase};
_->
FName = hackney_bstr:to_binary(filename:basename(Attachment)),
MyName = <<"attachment">>,
Disposition = {<<"form-data">>, [{<<"name">>, <<"\"", MyName/binary, "\"">>}, {<<"filename">>, <<"\"", FName/binary, "\"">>}]},
ExtraHeaders = [],
{multipart, PayloadBase ++ [{file, Attachment, Disposition, ExtraHeaders}]}
end,
西尔维