Skip to content

Commit

Permalink
Merge pull request #79 from Textalk/master
Browse files Browse the repository at this point in the history
Fixes crash when username is a binary.
  • Loading branch information
mworrell committed Feb 12, 2015
2 parents 87e15c8 + e8a9d2d commit fb667c6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/gen_smtp_client.erl
Expand Up @@ -48,7 +48,7 @@
-export([send/2, send/3, send_blocking/2]).
-endif.

-type email() :: {string() | binary(), [string() | binary(), ...], string() | binary() | function()}.
-type email() :: {string() | binary(), [string() | binary(), ...], string() | binary() | function()}.

-spec send(Email :: {string() | binary(), [string() | binary(), ...], string() | binary() | function()}, Options :: list()) -> {'ok', pid()} | {'error', any()}.
%% @doc Send an email in a non-blocking fashion via a spawned_linked process.
Expand Down Expand Up @@ -315,8 +315,9 @@ try_AUTH(Socket, Options, AuthTypes) ->
false
end;
true ->
Username = proplists:get_value(username, Options),
Password = proplists:get_value(password, Options),

Username = to_string(proplists:get_value(username, Options)),
Password = to_string(proplists:get_value(password, Options)),
%io:format("Auth types: ~p~n", [AuthTypes]),
Types = re:split(AuthTypes, " ", [{return, list}, trim]),
case do_AUTH(Socket, Username, Password, Types) of
Expand All @@ -333,6 +334,9 @@ try_AUTH(Socket, Options, AuthTypes) ->
end
end.

to_string(String) when is_list(String) -> String;
to_string(Binary) when is_binary(Binary) -> binary_to_list(Binary).

-spec do_AUTH(Socket :: socket:socket(), Username :: string(), Password :: string(), Types :: [string()]) -> boolean().
do_AUTH(Socket, Username, Password, Types) ->
FixedTypes = [string:to_upper(X) || X <- Types],
Expand Down Expand Up @@ -718,7 +722,7 @@ session_start_test_() ->
?assertMatch({ok, "EHLO testing\r\n"}, socket:recv(X, 0, 1000)),
socket:send(X, "250-server.example.com EHLO\r\n250-AUTH LOGIN PLAIN\r\n421 too busy\r\n"),
?assertMatch({ok, "QUIT\r\n"}, socket:recv(X, 0, 1000)),

{ok, Y} = socket:accept(ListenSock, 1000),
socket:send(Y, "220 Some banner\r\n"),
?assertMatch({ok, "EHLO testing\r\n"}, socket:recv(Y, 0, 1000)),
Expand Down

0 comments on commit fb667c6

Please sign in to comment.