From b11ab480b9bdd43f3d9ee32dfe50b8f77ebec540 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Mon, 26 Jun 2017 16:27:53 +0200 Subject: [PATCH] Use group receivers when sending email, custom subject --- apps/log_monitor/src/mailer.erl | 46 ++++++++++++++++++++++++++++----- config/log_monitor.config | 5 ++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/apps/log_monitor/src/mailer.erl b/apps/log_monitor/src/mailer.erl index 8e0e71f..796b0ad 100644 --- a/apps/log_monitor/src/mailer.erl +++ b/apps/log_monitor/src/mailer.erl @@ -6,6 +6,7 @@ -export([start_link/0, init/1, terminate/2]). -export([handle_info/2, handle_cast/2, handle_call/3]). -export([code_change/3]). +-export([email_subject/2]). start_link() -> gen_server:start_link(?MODULE, [], []). @@ -75,13 +76,24 @@ send_email(File, Text) -> {ok, EmailConfig} = application:get_env(log_monitor, email_config), Sender = proplists:get_value(sender, EmailConfig), DefaultReceiver = proplists:get_value(default_receiver, EmailConfig), + RawSubject = proplists:get_value(subject, EmailConfig), Connection = proplists:get_value(connection, EmailConfig), - error_logger:info_msg("Sending email for file ~s with text: ~n~s~n", [File, Text]), - gen_smtp_client:send_blocking( - {Sender, [DefaultReceiver], - io_lib:format("Subject: Error notification\r\nFrom: Fabio\r\n\r\nLogfile: ~s~nError: ~n~s~n", [File, Text])}, - Connection - ). + try receivers(File) of + GroupReceivers -> + Receivers = if GroupReceivers == [] + -> [DefaultReceiver]; + true -> GroupReceivers + end, + Subject = email_subject(RawSubject, File), + gen_smtp_client:send_blocking( + {Sender, Receivers, + io_lib:format("Subject: ~s\r\nFrom: ~s\r\n\r\nLogfile: ~s~nError: ~n~s~n", [Subject, Sender, File, Text])}, + Connection + ) + catch + _Throw -> + ok + end. remove_email(Id) -> mnesia:activity( @@ -90,3 +102,25 @@ remove_email(Id) -> mnesia:delete({log_monitor_error, Id}) end), 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. diff --git a/config/log_monitor.config b/config/log_monitor.config index 0d117c7..ec7653b 100644 --- a/config/log_monitor.config +++ b/config/log_monitor.config @@ -5,6 +5,11 @@ [ {sender, "log@monitor.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, [ {relay, "smtp.fabiosalvinii.com"}, {username, "salvini.fabio001@gmail.com"},