Skip to content

Instantly share code, notes, and snippets.

@seriyps
Last active November 6, 2022 02:03
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save seriyps/6319602 to your computer and use it in GitHub Desktop.
Save seriyps/6319602 to your computer and use it in GitHub Desktop.
Send emails using Erlang gen_smtp shortcut.
% Send plaintext email using gen_smtp https://github.com/Vagabond/gen_smtp
% This function sends email directly to receiver's SMTP server and don't use MTA relays.
%
% Example plaintext email:
% Mail = mail_plain(<<"Bob <sender@example.com>">>, <<"Alice <receiver@example.com>">>, <<"The mail subject">>, <<"The mail body">>),
% send_email(Mail).
%
% Example email with image attachment:
% ImgName = "image.jpg",
% {ok, ImgBin} = file:read_file(ImgName),
% Mail = mail_with_attachments(<<"sender@example.com">>, <<"receiver@example.com">>, <<"Photos">>,
% <<"See photo in attachment">>,
% [{ImgName, "image/jpeg", ImgBin}]),
% send_email(Mail).
-spec send_email(email()) -> binary() | {error, atom(), any()} | {error, any()}.
send_email(Email) ->
{From, [To], _Body} = Email,
[_, FromDomain] = split_addr(From),
[_, ToDomain] = split_addr(To),
Options = [{relay, binary_to_list(ToDomain)},
{tls, if_availablex},
{hostname, FromDomain}],
gen_smtp_client:send_blocking(Email, Options).
-spec mail_plain(binary(), binary(), binary(), binary(), Opts) -> email() when
Opts :: [{headers, [{binary(), binary()}]}].
mail_plain(From, To, Subject, Body, Opts) ->
AddHeaders = proplists:get_value(headers, Opts, []),
Mimemail =
{<<"text">>, <<"plain">>,
[
{<<"From">>, From},
{<<"To">>, To},
{<<"Subject">>, Subject},
{<<"Content-Type">>, <<"text/plain; charset=utf-8">>}
| AddHeaders],
[{<<"transfer-encoding">>, <<"base64">>}],
Body},
FromAddr = extract_addr_rfc822(From),
{FromAddr, [extract_addr_rfc822(To)], mimemail:encode(Mimemail)}.
-spec mail_with_attachments(
binary(), binary(), binary(), binary(),
[{Name :: binary(), MimeType :: binary(), Body :: binary()}]) -> email().
mail_with_attachments(From, To, Subject, Body, Attachments) ->
MimeBody = {<<"text">>, <<"plain">>,
[{<<"Content-Type">>, <<"text/plain;charset=utf-8">>},
{<<"Content-Transfer-Encoding">>, <<"quoted-printable">>},
{<<"Content-Disposition">>, <<"inline">>}],
[],
Body},
MimeAttachments = [begin
[Ct1, Ct2] = binary:split(MimeType, <<"/">>),
{Ct1, Ct2,
[{<<"Content-Transfer-Encoding">>, <<"base64">>}],
[{<<"disposition">>, <<"attachment">>},
{<<"disposition-params">>,
[{<<"filename">>, Name}]}],
AtBody}
end
|| {Name, MimeType, AtBody} <- Attachments],
Mimemail = {<<"multipart">>,
<<"mixed">>,
[{<<"From">>, From},
{<<"To">>, To},
{<<"Subject">>, Subject}],
[],
[MimeBody | MimeAttachments]},
FromAddr = extract_addr_rfc822(From),
{FromAddr, [extract_addr_rfc822(To)], mimemail:encode(Mimemail)}.
extract_addr_rfc822(Rfc822) ->
{ok, [{_, Addr}]} = smtp_util:parse_rfc822_addresses(Rfc822),
list_to_binary(Addr).
split_addr(MailAddr) ->
binary:split(MailAddr, <<"@">>).
@izinin
Copy link

izinin commented Apr 14, 2020

does not work . maybe it is working with minimal or no security in-place

(mongooseim@localhost)9> rp(gen_smtp_client:send_blocking(Email, Options)).
** exception throw: {temporary_failure,<<"421-4.7.0 [3.122.131.30      15] Our system has detected that this message is\r\n421-4.7.0 suspicious due to t"...>>}
     in function  gen_smtp_client:try_DATA/4 (/home/ubuntu/web_portal_feature_dev/server/_build/default/lib/gen_smtp/src/gen_smtp_client.erl, line 404)
     in call from gen_smtp_client:send_it/2 (/home/ubuntu/web_portal_feature_dev/server/_build/default/lib/gen_smtp/src/gen_smtp_client.erl, line 262)
(mongooseim@localhost)10> 

@seriyps
Copy link
Author

seriyps commented Apr 15, 2020

@izinin Well, if you are trying to send an email from your workstation - it could easily fail because of anti-spam filters (many providers block emails from residential IPs).
You may also want to configure SPF, DKIM and DMARC policies and signatures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment