Index: apps/app_voicemail.c
===================================================================
--- apps/app_voicemail.c	(revision 404951)
+++ apps/app_voicemail.c	(working copy)
@@ -643,7 +643,7 @@
 	char mailbox[AST_MAX_EXTENSION]; /*!< Mailbox id, unique within vm context */
 	char password[80];               /*!< Secret pin code, numbers only */
 	char fullname[80];               /*!< Full name, for directory app */
-	char email[80];                  /*!< E-mail address */
+	char *email;                     /*!< E-mail address */
 	char *emailsubject;              /*!< E-mail subject */
 	char *emailbody;                 /*!< E-mail body */
 	char pager[80];                  /*!< E-mail address to pager (no attachment) */
@@ -1043,6 +1043,8 @@
 		vmu->maxdeletedmsg = maxdeletedmsg;
 	}
 	vmu->volgain = volgain;
+	ast_free(vmu->email);
+	vmu->email = NULL;
 	ast_free(vmu->emailsubject);
 	vmu->emailsubject = NULL;
 	ast_free(vmu->emailbody);
@@ -1339,7 +1341,8 @@
 		} else if (!strcasecmp(var->name, "pager")) {
 			ast_copy_string(retval->pager, var->value, sizeof(retval->pager));
 		} else if (!strcasecmp(var->name, "email")) {
-			ast_copy_string(retval->email, var->value, sizeof(retval->email));
+			ast_free(retval->email);
+			retval->email = ast_strdup(var->value);
 		} else if (!strcasecmp(var->name, "fullname")) {
 			ast_copy_string(retval->fullname, var->value, sizeof(retval->fullname));
 		} else if (!strcasecmp(var->name, "context")) {
@@ -1466,6 +1469,7 @@
 		if ((vmu = (ivm ? ivm : ast_malloc(sizeof(*vmu))))) {
 			*vmu = *cur;
 			if (!ivm) {
+				vmu->email = ast_strdup(cur->email);
 				vmu->emailbody = ast_strdup(cur->emailbody);
 				vmu->emailsubject = ast_strdup(cur->emailsubject);
 			}
@@ -1761,6 +1765,9 @@
 {
 	if (ast_test_flag(vmu, VM_ALLOCED)) {
 
+		ast_free(vmu->email);
+		vmu->email = NULL;
+
 		ast_free(vmu->emailbody);
 		vmu->emailbody = NULL;
 
@@ -2297,7 +2304,7 @@
 		 * of this function, we will revert back to an empty string if tempcopy
 		 * is 1.
 		 */
-		ast_copy_string(vmu->email, vmu->imapuser, sizeof(vmu->email));
+		vmu->email = ast_strdup(vmu->imapuser);
 		tempcopy = 1;
 	}
 
@@ -2310,7 +2317,7 @@
 	if (!(p = vm_mkftemp(tmp))) {
 		ast_log(AST_LOG_WARNING, "Unable to store '%s' (can't create temporary file)\n", fn);
 		if (tempcopy)
-			*(vmu->email) = '\0';
+			ast_free(vmu->email);
 		return -1;
 	}
 
@@ -4563,6 +4570,9 @@
 	struct ast_str *str1 = ast_str_create(16), *str2 = ast_str_create(16);
 	char *greeting_attachment; 
 	char filename[256];
+	int first_line;
+	char *tmp;
+	char *email;
 
 	if (!str1 || !str2) {
 		ast_free(str1);
@@ -4604,7 +4614,7 @@
 			ast_str_substitute_variables(&str1, 0, ast, fromstring);
 
 			if (check_mime(ast_str_buffer(str1))) {
-				int first_line = 1;
+				first_line = 1;
 				ast_str_encode_mime(&str2, 0, ast_str_buffer(str1), strlen("From: "), strlen(who) + 3);
 				while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
 					*ptr = '\0';
@@ -4625,20 +4635,25 @@
 		fprintf(p, "From: Asterisk PBX <%s>" ENDL, who);
 	}
 
-	if (check_mime(vmu->fullname)) {
-		int first_line = 1;
-		char *ptr;
-		ast_str_encode_mime(&str2, 0, vmu->fullname, strlen("To: "), strlen(vmu->email) + 3);
-		while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
-			*ptr = '\0';
-			fprintf(p, "%s %s" ENDL, first_line ? "To:" : "", ast_str_buffer(str2));
-			first_line = 0;
-			/* Substring is smaller, so this will never grow */
-			ast_str_set(&str2, 0, "%s", ptr + 1);
+	tmp = ast_strdupa(vmu->email);
+	fprintf(p, "To:");
+	first_line = 1;
+	while ((email = strsep(&tmp, "|"))) {
+		char *next = strchr(S_OR(tmp, ""), '|');
+		if (check_mime(vmu->fullname)) {
+			char *ptr;
+			ast_str_encode_mime(&str2, 0, vmu->fullname, first_line ? strlen("To: ") : 0, strlen(email) + 3 + (next ? strlen(",") : 0));
+			while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
+				*ptr = '\0';
+				fprintf(p, " %s" ENDL, ast_str_buffer(str2));
+				/* Substring is smaller, so this will never grow */
+				ast_str_set(&str2, 0, "%s", ptr + 1);
+			}
+			fprintf(p, " %s <%s>%s" ENDL, ast_str_buffer(str2), email, next ? "," : "");
+		} else {
+			fprintf(p, " %s <%s>%s" ENDL, ast_str_quote(&str2, 0, vmu->fullname), email, next ? "," : "");
 		}
-		fprintf(p, "%s %s <%s>" ENDL, first_line ? "To:" : "", ast_str_buffer(str2), vmu->email);
-	} else {
-		fprintf(p, "To: %s <%s>" ENDL, ast_str_quote(&str2, 0, vmu->fullname), vmu->email);
+		first_line = 0;
 	}
 
 	if (!ast_strlen_zero(emailsubject) || !ast_strlen_zero(vmu->emailsubject)) {
@@ -4648,7 +4663,7 @@
 			prep_email_sub_vars(ast, vmu, msgnum + 1, context, mailbox, fromfolder, cidnum, cidname, dur, date, category, flag);
 			ast_str_substitute_variables(&str1, 0, ast, e_subj);
 			if (check_mime(ast_str_buffer(str1))) {
-				int first_line = 1;
+				first_line = 1;
 				char *ptr;
 				ast_str_encode_mime(&str2, 0, ast_str_buffer(str1), strlen("Subject: "), 0);
 				while ((ptr = strchr(ast_str_buffer(str2), ' '))) {
@@ -10825,7 +10840,7 @@
 		ast_copy_string(vmu->fullname, s, sizeof(vmu->fullname));
 	}
 	if (stringp && (s = strsep(&stringp, ","))) {
-		ast_copy_string(vmu->email, s, sizeof(vmu->email));
+		vmu->email = ast_strdup(s);
 	}
 	if (stringp && (s = strsep(&stringp, ","))) {
 		ast_copy_string(vmu->pager, s, sizeof(vmu->pager));
@@ -12985,7 +13000,7 @@
 	}
 
 	populate_defaults(vmu);
-	ast_copy_string(vmu->email, "test2@example.net", sizeof(vmu->email));
+	vmu->email = ast_strdup("test2@example.net");
 #ifdef IMAP_STORAGE
 	/* TODO When we set up the IMAP server test, we'll need to have credentials for the VMU structure added here */
 #endif
