Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

On nonblock send with callback, use middleman to relay exit status ba…

…ck to the caller
  • Loading branch information...
commit d4f967270f71c72488929c061d3ebc2b8c9e2bdc 1 parent 23b3893
@Vagabond authored
Showing with 19 additions and 1 deletion.
  1. +19 −1 src/gen_smtp_client.erl
View
20 src/gen_smtp_client.erl
@@ -56,11 +56,29 @@ send(Email, Options) ->
-spec send(Email :: {string() | binary(), [string() | binary(), ...], string() | binary() | function()}, Options :: list(), Callback :: function() | 'undefined') -> {'ok', pid()} | {'error', any()}.
%% @doc Send an email nonblocking and invoke a callback with the result of the send.
-%% The callback will receive either `ok' or an `{error, Type, Message}' as the single argument.
+%% The callback will receive either `ok', `{error, Type, Message}' or `{exit, ExitReason}'
+%% as the single argument.
send(Email, Options, Callback) ->
NewOptions = lists:ukeymerge(1, lists:sort(Options),
lists:sort(?DEFAULT_OPTIONS)),
case check_options(NewOptions) of
+ ok when is_function(Callback) ->
+ spawn(fun() ->
+ process_flag(trap_exit, true),
+ Pid = spawn_link(fun() ->
+ send_it_nonblock(Email, NewOptions, Callback)
+ end
+ ),
+ receive
+ {'EXIT', Pid, Reason} ->
+ case Reason of
+ X when X == normal; X == shutdown ->
+ ok;
+ Error ->
+ Callback({exit, Error})
+ end
+ end
+ end);
ok ->
Pid = spawn_link(fun () ->
send_it_nonblock(Email, NewOptions, Callback)

2 comments on commit d4f9672

@evanmiller

Nice!

@Vagabond
Owner

Thanks for the suggestion :)

Please sign in to comment.
Something went wrong with that request. Please try again.