Skip to content

Commit

Permalink
core: in emails, only embed images < 1MB. (#3082)
Browse files Browse the repository at this point in the history
Also limit the number of outgoing emails to 30 in parallel.
Also add a config site.email_images_noembed to disable embedding images in emails.
  • Loading branch information
mworrell committed Aug 19, 2022
1 parent c052357 commit 4f8ecff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/smtp/z_email_embed.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%% @author Arjan Scherpenisse <arjan@scherpenisse.net>
%% @copyright 2010-2014 Arjan Scherpenisse
%% @copyright 2010-2022 Arjan Scherpenisse
%% @doc Email image embedding

%% Copyright 2010-2014 Arjan Scherpenisse <arjan@scherpenisse.net>
%% Copyright 2010-2022 Arjan Scherpenisse <arjan@scherpenisse.net>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -27,10 +27,17 @@
-include_lib("zotonic.hrl").
-include_lib("zotonic_file.hrl").

% Do not embed images > 1MB
-define(MAX_EMBED_SIZE, 1024*1024).

%% @doc Embed images mentioned in the HTML parts.
embed_images(Parts, Context) ->
[ embed_images_part(P, Context) || P <- Parts ].

case z_convert:to_bool(m_config:get_value(site, email_images_noembed, Context)) of
true ->
[ embed_images_part(P, Context) || P <- Parts ];
false ->
Parts
end.

embed_images_part({<<"text">>, <<"html">>, Hs, Ps, Html} = HtmlPart, Context) ->
case embed_images_html(Html, Context) of
Expand Down Expand Up @@ -68,7 +75,7 @@ find_images(Html) ->

embed_image([Path, LibImg, SubPath], {Parts, Html, Context} = Acc) ->
case lookup(LibImg, SubPath, Context) of
{ok, #z_file_info{mime=Mime} = Info} ->
{ok, #z_file_info{ mime=Mime, size=Size } = Info} when is_integer(Size), Size < ?MAX_EMBED_SIZE ->
try
Data = z_file_request:content_data(Info, identity),
{Part, Cid} = create_attachment(Data, Mime, filename:basename(SubPath)),
Expand All @@ -81,6 +88,8 @@ embed_image([Path, LibImg, SubPath], {Parts, Html, Context} = Acc) ->
lager:error("email embed_image error ~p on embedding ~p~n~p", [Error, Info, Stacktrace]),
Acc
end;
{ok, _} ->
Acc;
{error, _} ->
Acc
end.
Expand Down
2 changes: 1 addition & 1 deletion src/smtp/z_email_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
-define(MAX_RETRY, 10).

% Max number of e-mails being sent at the same time
-define(EMAIL_MAX_SENDING, 100).
-define(EMAIL_MAX_SENDING, 30).

% Max number of connections per (relay) domain.
-define(EMAIL_MAX_DOMAIN, 5).
Expand Down

0 comments on commit 4f8ecff

Please sign in to comment.