diff --git a/README.md b/README.md index c746a7c..fb05b25 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/smtp_server_example.erl b/src/smtp_server_example.erl index 88a5c10..91f706d 100644 --- a/src/smtp_server_example.erl +++ b/src/smtp_server_example.erl @@ -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([ @@ -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.