Skip to content

Instantly share code, notes, and snippets.

@wdshin
Created December 5, 2014 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdshin/d2b9ac864addaa62de9b to your computer and use it in GitHub Desktop.
Save wdshin/d2b9ac864addaa62de9b to your computer and use it in GitHub Desktop.
test_sendgrid.erl
url(v1,send_mail,json) ->
<<"https://sendgrid.com/api/mail.send.json">>.
url_encode(Data) ->
url_encode(Data,"").
url_encode([],Acc) ->
Acc;
url_encode([{Key,Value}|R],"") ->
url_encode(R, edoc_lib:escape_uri(Key) ++ "=" ++ encode_uri_rfc3986:encode(Value));
url_encode([{Key,Value}|R],Acc) ->
url_encode(R, Acc ++ "&" ++ edoc_lib:escape_uri(Key) ++ "=" ++ encode_uri_rfc3986:encode(Value)).
send_simple_mail({APIUser,APIKey},{From,FromName,ReplyTo},{To,ToName},{Subject,HTML,Text}) ->
Array = [ { "api_user" , binary_to_list(APIUser) } , { "api_key" , binary_to_list(APIKey) } ,
{ "to" , binary_to_list(To) },
{ "toname" , binary_to_list(ToName) } , { "subject" , binary_to_list(Subject) } ,
{ "text", binary_to_list(Text) } , { "html" , binary_to_list(HTML) } ,
{ "from" , binary_to_list(From) } , { "fromname" , binary_to_list(FromName) },
{ "replyto" , binary_to_list(ReplyTo) } ],
Body = list_to_binary(url_encode(Array)),
Headers = [ {<<"Content-Type">>, <<"application/x-www-form-urlencoded">>},
{<<"Accept">>, <<"application/json">>} ],
{ok, A, B, ConnRef} =hackney:request(post, url(v1,send_mail,json) , Headers , Body, [ { insecure, true } ]),
{ok, Body1} = hackney:body(ConnRef),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment