Skip to content

Commit

Permalink
HELP-14590: replacing LF with CRLF before sending emails (#6539)
Browse files Browse the repository at this point in the history
Per SMTP spec bare line feed (LF) are forbidden and
CRLF should be used instead. Some SMTP relays and server in both ends
are nice enough to correct bad line ending, but some don't.

Like in one case relay server is Office 365 and receiving end is AOL.com.
Office 365 stopped correcting line endings and AOL is not fixing it either.
This cause the email to bounce back with Non-Delivery Report (NDR) message.

From [here](https://www.drupal.org/forum/support/post-installation/2017-06-05/office-365-blocks-emails-with-bare-line-feeds-how-to-fix):
When bare line feed characters are included in a message, the SMTP protocol
chunking feature is required to transmit the message between email servers.
Chunking uses the SMTP protocol BDAT command, but the recipient's email
server (in our case AOL) doesn't support the BDAT command.
  • Loading branch information
icehess authored May 26, 2020
1 parent cc98a42 commit e8d2077
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion applications/teletype/src/teletype_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ add_rendered_templates_to_email([{ContentType, Content}|Rs], Acc) ->
,{<<"Content-Transfer-Encoding">>, CTEncoding}
])
,[]
,iolist_to_binary(Content)
,sanitize_content(iolist_to_binary(Content))
},
lager:debug("adding template ~s (encoding ~s)", [ContentType, CTEncoding]),
add_rendered_templates_to_email(Rs, [Template | Acc]).
Expand All @@ -362,6 +362,32 @@ add_rendered_templates_to_email([{ContentType, Content}|Rs], Acc) ->
default_content_transfer_encoding(<<"text/html">>) -> <<"base64">>;
default_content_transfer_encoding(_) -> <<"7BIT">>.

%% @doc Sanitizing Email Content.
%% This function is soppouse to sanitize the email content. For now
%% it just fixing Bare Line Feed.
%%
%% Per SMTP spec bare line feed (LF) are forbidden and
%% CRLF should be used instead. Some SMTP relays and server in both ends
%% are nice enough to correct bad line ending, but some don't.
%%
%% Like in one case relay server is Office 365 and receiving end is AOL.com.
%% Office 365 stopped correcting line endings and AOL is not fixing it either.
%% This cause the email to bounce back with Non-Delivery Report (NDR) message.
%%
%% When bare line feed characters are included in a message, the SMTP protocol
%% chunking feature is required to transmit the message between email servers.
%% Chunking uses the SMTP protocol BDAT command, but the recipient's email
%% server (in our case AOL) doesn't support the BDAT command.
-spec sanitize_content(binary()) -> binary().
sanitize_content(Content) ->
%% We first replace `\r\n' to `\n', so when converting `\n' back to `\r\n' we don't
%% double converting `\n', just in case the template lines are edited to end with `\r\n'.
binary:replace(binary:replace(Content, <<$\r, $\n>>, <<$\n>>, ['global'])
,<<$\n>>
,<<$\r, $\n>>
,['global']
).

-spec system_params() -> kz_term:proplist().
system_params() ->
[{<<"hostname">>, kz_term:to_binary(net_adm:localhost())}
Expand Down

0 comments on commit e8d2077

Please sign in to comment.