send alert only when server is down
diff --git a/include/types/global.h b/include/types/global.h
index 6082211..42b4d80 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -97,7 +97,7 @@
 	struct freq_ctr comp_bps_in;	/* bytes per second, before http compression */
 	struct freq_ctr comp_bps_out;	/* bytes per second, after http compression */
 	int email_alert;
-	char email_to[50];
+	char email_to[3][50];
 	char email_from[50];	
 	int cps_lim, cps_max;
 	int sps_lim, sps_max;
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 1398e4c..260804c 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -539,6 +539,7 @@
  */
 int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
 {
+	int i = 0;
 	int err_code = 0;
 	char *errmsg = NULL;
 
@@ -592,13 +593,12 @@
       err_code |= ERR_ALERT;
       goto out;
     }
-    if (*(args[2]) != 0) {
-      strncpy(global.email_from,args[2],50);
-    }
-    else {
-      strncpy(global.email_from,"na",50);
+    strncpy(global.email_from,args[1],50);
+    for (i=0; i<3; i++) {
+      if (*(args[i+2]) != 0)
+      	strncpy(global.email_to[i],args[i+2],50);
     }
-    strncpy(global.email_to,args[1],50);
+    
   } 
 	else if (!strcmp(args[0], "daemon")) {
 		global.mode |= MODE_DAEMON;
diff --git a/src/checks.c b/src/checks.c
index 1cda9d4..91ec4c9 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -295,7 +295,7 @@
 			     (check->health >= check->rise) ? (s->uweight ? "UP" : "DRAIN") : "DOWN");
 
 		Warning("%s.\n", trash.str);
-		Emaila("%s \n", trash.str);
+		
 		send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
 	}
 }
diff --git a/src/log.c b/src/log.c
index 10eb68d..8fbb558 100644
--- a/src/log.c
+++ b/src/log.c
@@ -604,22 +604,40 @@
 void Emaila(const char *fmt, ...)
 {
   va_list argp;
-  char tmp[128];
-  char buf[256];
-  char alertcmd[500];
+  char tmp[256];
+  char buf[500];
+  char alertcmd[1024];
+  int i;
   int sysreturn;  
+  size_t len = 0, len1 = 0, len2 = 0, len3 = 0;
 
   if (global.email_alert) {
     va_start(argp, fmt);
     vsnprintf(tmp,128,fmt, argp);
     make_literal(tmp, buf);
-    /*if no from address is set use default */  
-    if (!strcmp(global.email_from,"na")) {
-      snprintf(alertcmd,500,"echo \" %s\" | mail -s \"HAProxy alert\" %s &",buf,global.email_to);
-    }
-    else {
-      snprintf(alertcmd,500,"echo \" %s\" | mail -s \"$(echo -e \"HAProxy alert\\nFrom: HAProxy <%s>\\n\")\" %s &",buf,global.email_from,global.email_to);
+    
+    len1 = strlen(global.email_to[0]);
+    len2 = strlen(global.email_to[1]);
+    len3 = strlen(global.email_to[2]);
+    memcpy(tmp, global.email_to[0], len1);
+    tmp[len1] = ' ';    
+    len = len1 + 1;
+    if (len2 > 0) {
+      memcpy(tmp + len, global.email_to[1], len2); // includes terminating null    	
+      len += len2;
+      tmp[len] = ' ';
+      len += 1;
+      if (len3 > 0) {
+      	memcpy(tmp + len, global.email_to[2], len3); // includes terminating null    	
+        len += len3;
+        tmp[len] = ' ';
+        len += 1;
+      }
     }
+    tmp[len] = '\0';
+    snprintf(alertcmd, 1024, "echo \"%s\" | mail -s \"$(echo -e \"HAProxy alert\\nFrom: HAProxy <%s>\\n\")\" %s&",
+    	buf, global.email_from, tmp);
+    	  
     sysreturn = system(alertcmd);
     if (sysreturn == -1) {
       Warning("There was an error sending the email alert");
diff --git a/src/server.c b/src/server.c
index bc74944..067909c 100644
--- a/src/server.c
+++ b/src/server.c
@@ -255,7 +255,8 @@
 
 	srv_append_status(&trash, s, reason, xferred, 0);
 	Warning("%s.\n", trash.str);
-
+	Emaila("%s \n", trash.str);
+  
 	/* we don't send an alert if the server was previously paused */
 	send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", trash.str);