Skip to content

Commit

Permalink
core: ensure To email is properly formatted. (#2968)
Browse files Browse the repository at this point in the history
* core: ensure To email is properly formatted.

Also add name of signup user to To for emails.

* z_email_server: Fix Message-Id (not -ID), ensure list/binary To addresses
  • Loading branch information
mworrell committed May 13, 2022
1 parent 094c15a commit 269ba47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions modules/mod_signup/mod_signup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
-export([
signup/4,
signup_existing/5,
request_verification/2
request_verification/2,
send_verify_email/3
]).

-include("zotonic.hrl").
Expand Down Expand Up @@ -266,7 +267,9 @@ send_verify_email(UserId, Ident, Context) ->
{email, Email},
{verify_key, Key}
],
z_email:send_render(Email, "email_verify.tpl", Vars, z_acl:sudo(Context)),
{Name, _NameCtx} = z_template:render_to_iolist("_name.tpl", [{id, UserId}], z_acl:sudo(Context)),
To = z_email:combine_name_email(z_string:trim(iolist_to_binary(Name)), Email),
z_email:send_render(To, "email_verify.tpl", Vars, z_acl:sudo(Context)),
ok.


Expand Down
27 changes: 18 additions & 9 deletions src/smtp/z_email_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -771,20 +771,18 @@ encode_email(Id, #email{body=undefined} = Email, MessageId, From, Context) ->
Sub
end,
Headers = [{"From", From},
{"To", z_convert:to_list(Email#email.to)},
{"To", z_convert:to_list(ensure_brackets(Email#email.to))},
{"Subject", z_convert:to_flatlist(Subject)},
{"Date", date(Context)},
{"MIME-Version", "1.0"},
{"Message-ID", MessageId},
{"X-Mailer", "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}
{"Message-Id", MessageId}
| Email#email.headers ],
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, MessageId, From, Context) when is_tuple(Body) ->
Headers = [{<<"From">>, From},
{<<"To">>, Email#email.to},
{<<"Message-ID">>, MessageId},
{<<"X-Mailer">>, "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}
{<<"To">>, ensure_brackets(Email#email.to)},
{<<"Message-Id">>, MessageId}
| Email#email.headers ],
Headers2 = add_reply_to(Id, Email, add_cc(Email, Headers), Context),
{BodyType, BodySubtype, BodyHeaders, BodyParams, BodyParts} = Body,
Expand All @@ -794,9 +792,8 @@ encode_email(Id, #email{body=Body} = Email, MessageId, From, Context) when is_tu
mimemail:encode({BodyType, BodySubtype, MailHeaders, BodyParams, BodyParts}, opt_dkim(Context));
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", MessageId},
{"X-Mailer", "Zotonic " ++ ?ZOTONIC_VERSION ++ " (http://zotonic.com)"}
{"To", z_convert:to_list(ensure_brackets(Email#email.to))},
{"Message-Id", MessageId}
| Email#email.headers ],
Headers2 = add_reply_to(Id, Email, add_cc(Email, Headers), Context),
iolist_to_binary([ encode_headers(Headers2), "\r\n\r\n", Body ]).
Expand Down Expand Up @@ -824,6 +821,18 @@ encode_email(Id, #email{body=Body} = Email, MessageId, From, Context) when is_li
[{"Reply-To", ReplyTo1} | Headers].


ensure_brackets(Email) when is_binary(Email) ->
case binary:match(Email, <<"<">>) of
{_,_} ->
Email;
nomatch ->
[ Name | _ ] = binary:split(Email, <<"@">>),
<<Name/binary, " <", Email/binary, $>>>
end;
ensure_brackets(Email) ->
ensure_brackets(z_convert:to_binary(Email)).


build_and_encode_mail(Headers, Text, Html, Attachment, Context) ->
Headers1 = [
{z_convert:to_binary(H), z_convert:to_binary(V)} || {H,V} <- Headers
Expand Down

0 comments on commit 269ba47

Please sign in to comment.