Last active
November 6, 2022 02:03
-
-
Save seriyps/6319602 to your computer and use it in GitHub Desktop.
Send emails using Erlang gen_smtp shortcut.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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 <[email protected]>">>, <<"Alice <[email protected]>">>, <<"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(<<"[email protected]">>, <<"[email protected]">>, <<"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 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
does not work . maybe it is working with minimal or no security in-place