send alert only when server is down
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");