带有附件的 Erlang SMTP 客户端

Erlang SMTP client WITH attachments

我正在寻找以下两件事之一:

  1. 支持发送带附件的电子邮件的 Erlang 库
  2. 一个使用gen_smtp发送带附件的邮件的例子,因为我已经用这个库成功发送过邮件

虽然有很多模块可以通过 SMTP 发送纯文本电子邮件,但 SMTP 客户端支持附件的方式似乎很少。有人有什么建议吗?

此外,使用 Windows - 我不确定 sendmail 路由是否可用?



阿格斯,当我尝试那个确切的代码以及类似的代码时,我不断收到以下错误,有什么想法吗?:

** exception error: {badmap,[]}
     in function  maps:get/3
        called as maps:get(content_type_params,[],[])
     in call from mimemail:ensure_content_headers/7 (c:/temp/erlang/myapp/_build/default/lib/gen_smtp/src/mimemail.erl, line 667)
     in call from mimemail:encode/2 (c:/temp/erlang/myapp/_build/default/lib/gen_smtp/src/mimemail.erl, line 161)
     in call from email_test:send_email_with_attachment/0 (c:/temp/erlang/myapp/src/email_test.erl, line 14)

我在 rebar.config 中使用的 gen_smtp 版本:

{gen_smtp, ".*", {git, "git://github.com/gen-smtp/gen_smtp.git", {branch, master}}}

简答:您可以使用gen_smtp发送带附件的电子邮件。

如果你使用过gen_smtp_client:send(Email, Options)gen_smtp_client:send_blocking(Email, Options),那么你实际上可以使用mimemail:encode/2.

生成Body's Email变量
%% @doc Encode a MIME tuple to a binary.
encode({Type, Subtype, Headers, ContentTypeParams, Parts}, Options) ->
...

下面的代码显示了如何发送带有电子邮件内联正文和 2 个附件的邮件(分别为 test1.txterlang.png)。这里的关键是使用 multipart/mixed MIME 类型并相应地构造电子邮件正文。

send_email_with_attachment() ->
    From = "noreply@mydomain.com", 
    ToList = ["target_email@mydomain.com"],
    
    Part2Filename = "/tmp/test1.txt",
    {ok, Part2Binary} = file:read_file(Part2Filename),
    
    Part3Filename = "/tmp/erlang.png",
    {ok, Part3Binary} = file:read_file(Part3Filename),
        
    Email = mimemail:encode(
                            {
                             <<"multipart">>, %%Type, 
                             <<"mixed">>, %%Subtype,
                             %%Headers,  
                             [
                              {<<"From">>, <<"No-Reply <noreply@mydomain.com>">>},
                              {<<"To">>, <<"target_email@mydomain.com">>},
                              {<<"Subject">>, <<"Mail Subject">>}
                             ], 
                             #{}, %%[], %%ContentTypeParams, 
                             %%(Multi)Parts
                             [
                                %%Part 1: this is the inline mail body, note the {<<"disposition">>, <<"inline">>} tag
                                {
                                     <<"text">>, %%Type, 
                                     <<"plain">>, %%Subtype,
                                     %%Headers
                                     [],
                                     %%ContentTypeParams
                                     #{
                                       disposition => <<"inline">>
                                       },
                                     %%Part
                                     <<"Email body (inline) is here blah blah..">>
                                },
                                
                                %%Part 2: this is the text file as attachment, note the {<<"disposition">>, <<"attachment">>} tag
                                {
                                     <<"text">>, %%Type, 
                                     <<"plain">>, %%Subtype,
                                     %%Headers
                                     [],
                                     %%ContentTypeParams
                                     #{
                                       disposition => <<"attachment">>,
                                       disposition_params => [{<<"filename">>, <<"test1.txt">>}]
                                      },
                                     %%Part
                                     Part2Binary
                                },
                                
                                %%Part 3: this is the PNG file as attachment, note the {<<"disposition">>, <<"attachment">>} tag
                                {
                                     <<"image">>, %%Type, 
                                     <<"png">>, %%Subtype,
                                     %%Headers
                                     [],
                                     %%ContentTypeParams 
                                     #{
                                       disposition => <<"attachment">>,
                                       disposition_params => [{<<"filename">>, <<"erlang.png">>}] 
                                      },
                                     %%Part
                                     Part3Binary
                                }
                              ]
                            },
                            [] %%Options
                           ),
    Opts =  [{relay, "smtp.mydomain.com"},
              {tls, never}
             ], 
    gen_smtp_client:send({From, ToList, Email}, Opts).

这就是您将在邮箱中看到的内容: