Skip to content

Commit

Permalink
Configurable sender
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabil BENDAFI authored and nabilbendafi committed Jul 3, 2016
1 parent a27176d commit 97ce373
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions modules/alert/mail/doc/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Parameters

SMTP account password, if authentication is used.

.. describe:: sender

Sender of alert email.

.. describe:: recipients

Recipients of alert email. [Mandatory]
Expand Down
19 changes: 17 additions & 2 deletions modules/alert/mail/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <unistd.h>

#define FROM "<haka@alert.com>"
#define MAXHOSTNAMELEN 1024
#define BODY_SIZE 2048

static REGISTER_LOG_SECTION(mail);
Expand All @@ -33,6 +34,7 @@ struct mail_alerter {
char *username;
char *password;
AUTH auth;
char *sender;
struct curl_slist *recipients;
};

Expand Down Expand Up @@ -114,7 +116,7 @@ static bool send_mail(struct mail_alerter *state, uint64 id, const struct time *
}

/* Mail sender */
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, state->sender);

/* Mail recipients */
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, state->recipients);
Expand Down Expand Up @@ -172,6 +174,7 @@ struct alerter_module *init_alerter(struct parameters *args)
const char *username = parameters_get_string(args, "username", NULL);
const char *password = parameters_get_string(args, "password", NULL);
const char *auth = parameters_get_string(args, "authentication", "NULL");
const char *sender = parameters_get_string(args, "sender", NULL);
const char *recipients = parameters_get_string(args, "recipients", NULL);

if (!server || !recipients) {
Expand All @@ -197,6 +200,18 @@ struct alerter_module *init_alerter(struct parameters *args)
}
}

if (!sender) {
char hostname[MAXHOSTNAMELEN+1];

if (gethostname(hostname, MAXHOSTNAMELEN)) {
LOG_ERROR(mail, "Couldn't get hostname\n");
strcpy(hostname, "localhost@localdomain.com");
}
mail_alerter->sender = strdup(hostname);
} else {
mail_alerter->sender = strdup(sender);
}

char *recipient;
while ((recipient = strtok((char *)recipients, ",")) != NULL)
mail_alerter->recipients = curl_slist_append(mail_alerter->recipients,
Expand Down

0 comments on commit 97ce373

Please sign in to comment.