Use group receivers when sending email, custom subject
This commit is contained in:
parent
ab5d442d9f
commit
b11ab480b9
|
@ -6,6 +6,7 @@
|
||||||
-export([start_link/0, init/1, terminate/2]).
|
-export([start_link/0, init/1, terminate/2]).
|
||||||
-export([handle_info/2, handle_cast/2, handle_call/3]).
|
-export([handle_info/2, handle_cast/2, handle_call/3]).
|
||||||
-export([code_change/3]).
|
-export([code_change/3]).
|
||||||
|
-export([email_subject/2]).
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link(?MODULE, [], []).
|
gen_server:start_link(?MODULE, [], []).
|
||||||
|
@ -75,13 +76,24 @@ send_email(File, Text) ->
|
||||||
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
|
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
|
||||||
Sender = proplists:get_value(sender, EmailConfig),
|
Sender = proplists:get_value(sender, EmailConfig),
|
||||||
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
|
DefaultReceiver = proplists:get_value(default_receiver, EmailConfig),
|
||||||
|
RawSubject = proplists:get_value(subject, EmailConfig),
|
||||||
Connection = proplists:get_value(connection, EmailConfig),
|
Connection = proplists:get_value(connection, EmailConfig),
|
||||||
error_logger:info_msg("Sending email for file ~s with text: ~n~s~n", [File, Text]),
|
try receivers(File) of
|
||||||
|
GroupReceivers ->
|
||||||
|
Receivers = if GroupReceivers == []
|
||||||
|
-> [DefaultReceiver];
|
||||||
|
true -> GroupReceivers
|
||||||
|
end,
|
||||||
|
Subject = email_subject(RawSubject, File),
|
||||||
gen_smtp_client:send_blocking(
|
gen_smtp_client:send_blocking(
|
||||||
{Sender, [DefaultReceiver],
|
{Sender, Receivers,
|
||||||
io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])},
|
io_lib:format("Subject: ~s\r\nFrom: ~s\r\n\r\nLogfile: ~s~nError: ~n~s~n", [Subject, Sender, File, Text])},
|
||||||
Connection
|
Connection
|
||||||
).
|
)
|
||||||
|
catch
|
||||||
|
_Throw ->
|
||||||
|
ok
|
||||||
|
end.
|
||||||
|
|
||||||
remove_email(Id) ->
|
remove_email(Id) ->
|
||||||
mnesia:activity(
|
mnesia:activity(
|
||||||
|
@ -90,3 +102,25 @@ remove_email(Id) ->
|
||||||
mnesia:delete({log_monitor_error, Id})
|
mnesia:delete({log_monitor_error, Id})
|
||||||
end),
|
end),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
receivers(File) ->
|
||||||
|
mnesia:activity(
|
||||||
|
transaction,
|
||||||
|
fun() ->
|
||||||
|
case mnesia:read(log_monitor_file, File) of
|
||||||
|
[{log_monitor_file, File, _, _, Group}] ->
|
||||||
|
case mnesia:read(log_monitor_group, Group) of
|
||||||
|
[{log_monitor_group, Group, EmailReceivers}] ->
|
||||||
|
EmailReceivers
|
||||||
|
end;
|
||||||
|
_ -> throw(file_not_found)
|
||||||
|
end
|
||||||
|
end).
|
||||||
|
|
||||||
|
email_subject(Text, File) ->
|
||||||
|
Text1 = re:replace(Text, "%f", File, [global, {return, list}]),
|
||||||
|
re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]).
|
||||||
|
|
||||||
|
file_name_from_path(File) ->
|
||||||
|
{match, [_, Basename]} = re:run(File, "^.*\/(.*)\\..*$", [{capture, all, list}]),
|
||||||
|
Basename.
|
||||||
|
|
|
@ -5,6 +5,11 @@
|
||||||
[
|
[
|
||||||
{sender, "log@monitor.com"},
|
{sender, "log@monitor.com"},
|
||||||
{default_receiver, "salvini.fabio001@gmail.com"},
|
{default_receiver, "salvini.fabio001@gmail.com"},
|
||||||
|
%% Subject of the email.
|
||||||
|
%% Placeholders:
|
||||||
|
%% %f: the full path of the log file (ex. /var/log/myapp.log).
|
||||||
|
%% %F: the basename of the log file (ex. myapp).
|
||||||
|
{subject, "[%F] Error notification"},
|
||||||
{connection, [
|
{connection, [
|
||||||
{relay, "smtp.fabiosalvinii.com"},
|
{relay, "smtp.fabiosalvinii.com"},
|
||||||
{username, "salvini.fabio001@gmail.com"},
|
{username, "salvini.fabio001@gmail.com"},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user