Erlang SMTP client WITH attachments

e3bfsja2  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(189)

I'm looking for one of two things:

  1. An Erlang library that supports sending of emails with attachments
  2. An example of using gen_smtp to send an email with an attachment, since I have already successfully sent emails with this library
    It seems there is very little in the way of SMTP client support with attachments out there, although there are plenty of modules that can send plain text emails via SMTP. Anyone have suggestions?
    Also, using Windows - I'm not sure the sendmail route is available?
  • Agus, when I try that exact code, as well as similar code, I keep getting the following error, any ideas?:*
** 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)

The version of gen_smtp I'm using in rebar.config:
{gen_smtp, ".*", {git, "git://github.com/gen-smtp/gen_smtp.git", {branch, master}}}

soat7uwm

soat7uwm1#

简短回答:您可以使用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).

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

相关问题