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

make relay work again in the smtp_server_example #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ gen_smtp_server:start(
```

This configures the session to fix bare newlines (other options are `strip`, `ignore` and `false`: `false` rejects emails with bare newlines, `ignore` passes them through unmodified and `strip` removes them) and tells the callback module to run the MIME decoder on the email once its been received. The example callback module also supports the following options: `relay` - whether to relay email on, `auth` - whether to do SMTP authentication and `parse` - whether to invoke the MIME parser. The example callback module is included mainly as an example and are not intended for serious usage. You could easily create your own callback options.

If you want to pass through options to socket in relay mode in the `smtp_server_example`, for example when you need to bind the socket to different IP when your server has more than one IP, you can use `relayopts` option in the `callbackoption` list, such as `{callbackoptions, [{relayopts, [{sockopts, [{ip, {22,22,22,22}}]}]}]}`


In general, following options can be specified `gen_smtp_server:options()`:

* `{domain, string()}` - is used as server hostname (it's placed to SMTP server banner and HELO/EHLO response), default - guess from machine hostname
Expand Down Expand Up @@ -231,6 +235,8 @@ gen_smtp_client:send(

If you want to listen on IPv6, you can use the `{family, inet6}` and `{address, {0, 0, 0, 0, 0, 0, 0, 0}}` options to enable listening on IPv6.



Please notice that when using the LMTP protocol, the `handle_EHLO` callback will be used
to handle the `LHLO` command as defined in [RFC2033](https://tools.ietf.org/html/rfc2033),
due to their similarities. Although not used, the implementation of `handle_HELO` is still
Expand Down
17 changes: 11 additions & 6 deletions src/smtp_server_example.erl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ handle_DATA(From, To, Data, State) ->
% if RELAY is true, then relay email to email address, else send email data to console
case proplists:get_value(relay, State#state.options, false) of
true ->
relay(From, To, Data);
RelayConnectOpts = proplists:get_value(relayopts, State#state.options, []),
ok = relay(From, To, Data, RelayConnectOpts),
{ok, ["sent"], State};
false ->
% some kind of unique id
Reference = lists:flatten([
Expand Down Expand Up @@ -311,14 +313,17 @@ terminate(Reason, State) ->
unique_id() ->
erlang:unique_integer().

-spec relay(binary(), [binary()], binary()) -> ok.
relay(_, [], _) ->
-spec relay(binary(), [binary()], binary(), list()) -> ok.
relay(_, [], _, _) ->
ok;
relay(From, [To | Rest], Data) ->
relay(From, [To | Rest], Data, RelayConnectOpts) ->
% relay message to email address
[_User, Host] = string:tokens(binary_to_list(To), "@"),
gen_smtp_client:send({From, [To], erlang:binary_to_list(Data)}, [{relay, Host}]),
relay(From, Rest, Data).
gen_smtp_client:send(
{From, [To], erlang:binary_to_list(Data)},
lists:keystore(relay, 1, RelayConnectOpts, {relay, Host})
),
relay(From, Rest, Data, RelayConnectOpts).

%% @doc Helps `handle_DATA' to deal with the received email.
%% This function is not directly required by the behaviour.
Expand Down
Loading