Skip to content

Commit

Permalink
Added option email_bounce_override to override bounce email address.
Browse files Browse the repository at this point in the history
Fixes #211. Thanks to Michael Schreckenbauer.
(cherry picked from commit a1f4521)
  • Loading branch information
Arjan Scherpenisse committed Oct 3, 2011
1 parent 6c92b41 commit 399e762
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -26,6 +26,7 @@ Marco Wessel <marco@cyberhq.nl>
Michael Connors <michael@bring42.net>
Mikael Magnusson <mikma264@gmail.com>
Richard Fergie <richard.fergie@gmail.com>
Michael Schreckenbauer <grimlog@gmx.de>
Steve Strong <srstrong@gmail.com>
Tim Benniks <tim@timbenniks.nl>
Watt Poosanguansit <wpoosanguansit@yahoo.com>
31 changes: 19 additions & 12 deletions src/smtp/z_email_server.erl
Expand Up @@ -266,10 +266,12 @@ update_config(State) ->
smtp_spamd_port=SmtpSpamdPort}.


% E-mail domain, depends on the smtp domain of the sending site

%% @doc Get the bounce email address. Can be overridden per site in config setting site.bounce_email_override.
bounce_email(MessageId, Context) ->
"noreply+"++z_convert:to_list(MessageId)++[$@ | bounce_domain(Context)].
case m_config:get_value(site, bounce_email_override, Context) of
undefined -> "noreply+"++MessageId;
VERP -> z_convert:to_list(VERP)
end.

reply_email(MessageId, Context) ->
"reply+"++z_convert:to_list(MessageId)++[$@ | email_domain(Context)].
Expand Down Expand Up @@ -325,6 +327,10 @@ get_email_from(Context) ->
EmailFrom -> z_convert:to_list(EmailFrom)
end.

% Unique message-id, depends on bounce domain
message_id(MessageId, Context) ->
z_convert:to_list(MessageId)++[$@ | bounce_domain(Context)].

%% @doc Remove a worker Pid from the server state.
remove_worker(Pid, State) ->
Filtered = lists:filter(fun({_,P}) -> P =/= Pid end, State#state.sending),
Expand All @@ -350,15 +356,16 @@ send_email(Id, Recipient, Email, Context, State) ->

spawn_send(Id, Recipient, Email, Context, State) ->
F = fun() ->
VERP = "<"++bounce_email(Id, Context)++">",
MessageId = message_id(Id, Context),
VERP = "<"++bounce_email(MessageId, Context)++">",

From = get_email_from(Email#email.from, VERP, State, Context),
Recipient1 = check_override(Recipient, m_config:get_value(site, email_override, Context), State),
Recipient2 = string:strip(z_string:line(binary_to_list(z_convert:to_binary(Recipient1)))),
{_RcptName, RecipientEmail} = z_email:split_name_email(Recipient2),
[_RcptLocalName, RecipientDomain] = string:tokens(RecipientEmail, "@"),

EncodedMail = encode_email(Id, Email, VERP, From, Context),
EncodedMail = encode_email(Id, Email, "<"++MessageId++">", From, Context),

SmtpOpts =
case State#state.smtp_relay of
Expand Down Expand Up @@ -447,12 +454,12 @@ spawn_send(Id, Recipient, Email, Context, State) ->
State#state{sending=[{Id,SenderPid}|State#state.sending]}.


encode_email(_Id, #email{raw=Raw}, _VERP, _From, _Context) when is_list(Raw); is_binary(Raw) ->
encode_email(_Id, #email{raw=Raw}, _MessageId, _From, _Context) when is_list(Raw); is_binary(Raw) ->
z_convert:to_binary([
"X-Mailer: Zotonic ", ?ZOTONIC_VERSION, " (http://zotonic.com)\r\n",
Raw
]);
encode_email(Id, #email{body=undefined} = Email, VERP, From, Context) ->
encode_email(Id, #email{body=undefined} = Email, MessageId, From, Context) ->
%% Optionally render the text and html body
Vars = [{email_to, Email#email.to}, {email_from, From} | Email#email.vars],
Text = optional_render(Email#email.text, Email#email.text_tpl, Vars, Context),
Expand All @@ -473,14 +480,14 @@ encode_email(Id, #email{body=undefined} = Email, VERP, From, Context) ->
{"Subject", z_convert:to_flatlist(Subject)},
{"Date", date(Context)},
{"MIME-Version", "1.0"},
{"Message-ID", VERP},
{"Message-ID", MessageId},
{"X-Mailer", "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}],
Headers2 = add_reply_to(Id, Email, add_cc(Email, Headers), Context),
build_and_encode_mail(Headers2, Text, Html, Email#email.attachments, Context);
encode_email(Id, #email{body=Body} = Email, VERP, From, Context) when is_tuple(Body) ->
encode_email(Id, #email{body=Body} = Email, MessageId, From, Context) when is_tuple(Body) ->
Headers = [{<<"From">>, From},
{<<"To">>, Email#email.to},
{<<"Message-ID">>, VERP},
{<<"Message-ID">>, MessageId},
{<<"X-Mailer">>, "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}
| Email#email.headers ],
Headers2 = add_reply_to(Id, Email, add_cc(Email, Headers), Context),
Expand All @@ -489,10 +496,10 @@ encode_email(Id, #email{body=Body} = Email, VERP, From, Context) when is_tuple(B
{z_convert:to_binary(H), z_convert:to_binary(V)} || {H,V} <- (Headers2 ++ BodyHeaders)
],
mimemail:encode({BodyType, BodySubtype, MailHeaders, BodyParams, BodyParts});
encode_email(Id, #email{body=Body} = Email, VERP, From, Context) when is_list(Body); is_binary(Body) ->
encode_email(Id, #email{body=Body} = Email, MessageId, From, Context) when is_list(Body); is_binary(Body) ->
Headers = [{"From", From},
{"To", z_convert:to_list(Email#email.to)},
{"Message-ID", VERP},
{"Message-ID", MessageId},
{"X-Mailer", "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}
| Email#email.headers ],
Headers2 = add_reply_to(Id, Email, add_cc(Email, Headers), Context),
Expand Down

0 comments on commit 399e762

Please sign in to comment.