dkim, cc and bcc

51 views
Skip to first unread message

can2nac

unread,
Jun 29, 2014, 8:07:19 AM6/29/14
to chica...@googlegroups.com
Hi all,

i had a task to implement dkim, cc and bcc with boss_mail. Below is a solution.

1. Update gen_smtp to the latest version. You will need mimemail:encode\2;
2. Run Mimeail with your app or each time with boss_mail. I added code:ensure_loaded(mimemail), in init\0 in main_init;
3. Change boss_mail and recompile CB

%depricated by can2nac Jun 24 2014
%send_message(App, FromAddress, ToAddress, Action, HeaderFields, Variables, Options, Callback) ->
%    BodyFun = fun() -> build_message(App, Action, HeaderFields, Variables, Options) end,
%    gen_server:call(boss_mail, {deliver, FromAddress, ToAddress, BodyFun, Callback}).

%proper cc and bcc processing, added by can2nac Jun 24 2014
send_message(App,FromAddress,ToAddress,Action,HeaderFields,Variables,Options,Callback) ->    
    Recipients=[ToAddress]++lists:flatmap(fun(T)->string:tokens(proplists:get_value(T,HeaderFields,[]),";")end,["Cc","Bcc"]),
    _HeaderFields=lists:keydelete("Bcc",1,HeaderFields),
    BodyFun=fun()->build_message(App,Action,_HeaderFields,Variables,Options)end,
    lists:foreach(fun(_ToAddress)->gen_server:call(boss_mail,{deliver,FromAddress,_ToAddress,BodyFun,Callback})end,Recipients).

build_message(App, Action, HeaderFields, Variables, Options) ->
    ContentLanguage = proplists:get_value("Content-Language", HeaderFields),
    EffectiveAction = proplists:get_value(template, Options, Action),
    Attachments = proplists:get_value(attachments, Options, []),
    {MimeType, MessageBody} = build_message_body_attachments(App, EffectiveAction, Variables, Attachments, ContentLanguage),
    MessageHeader = build_message_header(HeaderFields, MimeType),
    %added by can2nac Jun 24 2014 START
    DKIMOptions=
      case file:read_file(boss_env:get_env(dkim_certificate_file,undefined)) of
    {ok,PemBin}->[{dkim,boss_env:get_env(dkim_options,[])++[{private_key,{pem_plain,PemBin}}]}];
        {error,_}->[]
      end,
    %added by can2nac Jun 24 2014 END
    %[MessageHeader, "\r\n", convert_unix_newlines_to_dos(MessageBody)]. %removed by can2nac Jun 24 2014
    mimemail:encode({list_to_binary(MimeType),<<>>,MessageHeader,[]
,list_to_binary(convert_unix_newlines_to_dos(MessageBody))},DKIMOptions).%added by can2nac Jun 24 2014

build_message_header(HeaderFields, DefaultMimeType) ->
    MessageID = case proplists:get_value("Message-ID", HeaderFields) of
        undefined -> smtp_util:generate_message_id();
        Other -> Other
    end,
    ContentType = proplists:get_value("Content-Type", HeaderFields, DefaultMimeType),
    Date = proplists:get_value("Date", HeaderFields, erlydtl_dateformat:format("r")),
    AllHeaders = [{"Date", Date}, {"Content-Type", ContentType},
        {"MIME-Version", "1.0"}, {"Message-ID", MessageID} | HeaderFields],
    [{list_to_binary(K),list_to_binary(V)}||{K,V}<-AllHeaders].%added by can2nac Jun 24 2014
    %add_fields(AllHeaders, [], []).%removed by can2nac Jun 24 2014

%depricated by can2nac Jun 24 2014 END
%add_fields([], _, Acc) ->
%    lists:reverse(Acc);
%add_fields([{Key, Value}|Rest], Seen, Acc) ->
%    case proplists:get_value(Key, Seen) of
%        undefined ->
%            add_fields(Rest, [Key|Seen], [[Key, ": ", Value, "\r\n"] | Acc]);
%        _ ->
%            add_fields(Rest, Seen, Acc)
%    end.

4. Add in boss_config following lines
{dkim_certificate_file,"priv/dkim.pem"},
    {dkim_options,[{s,<<"some_txt_record_prefix">>},{d,<<"domain.com.">>}]}, %TXT some_txt_record_refix._dkim.domain.com

Only open certificates were implemented, password protected were not implemented.

Jesse Gumm

unread,
Jun 29, 2014, 10:10:37 AM6/29/14
to chica...@googlegroups.com

This looks useful.

Could you out this together in a pull request on github?

--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/b88fa990-46a7-4c91-bfc4-5b8f75c2795c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

can2nac

unread,
Jun 30, 2014, 9:55:04 AM6/30/14
to chica...@googlegroups.com
ok, will do little later as i'm still on 0.8.7 build, not time for update to latest CB version right now, but i'll try until the end of the week.

Jesse Gumm

unread,
Jun 30, 2014, 10:27:36 AM6/30/14
to chica...@googlegroups.com

Thanks!

--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

Reply all
Reply to author
Forward
0 new messages