--- zabbix-2.0.1.orig/src/libs/zbxmedia/email.c	2012-06-28 06:02:01.000000000 +0900
+++ zabbix-2.0.1/src/libs/zbxmedia/email.c	2012-08-24 17:58:55.525095438 +0900
@@ -29,6 +29,115 @@
 
 /******************************************************************************
  *                                                                            *
+ * Comments: Returns the bit for the next UTF-8 encoded character             *
+ *                                                                            *
+ ******************************************************************************/
+int get_nextutf8_byte(const char *buf, size_t pos) {
+
+	int	nextutf = pos;
+	int	buflen = strlen(buf);
+
+	if	(pos < buflen) {
+		if ((buf[pos] & 0x80) == 0x00) {
+			nextutf = pos + 1;
+		} else if ((buf[pos] & 0xE0) == 0xC0) {
+			nextutf = pos + 2;
+		} else if ((buf[pos] & 0xF0) == 0xE0) {
+			nextutf = pos + 3;
+		} else if ((buf[pos] & 0xF8) == 0xF0) {
+			nextutf = pos + 4;
+		} else {
+			nextutf = -1;
+		}
+	}
+
+	return nextutf;
+}
+
+/******************************************************************************
+ *                                                                            *
+ * Comments: Returns a base64 encoded Subject originally in UTF-8 that is     *
+ *           RFC-2047 compliant                                               *
+ *                                                                            *
+ ******************************************************************************/
+void str_base64_encode_rfc2047(const char *src, char **p_base64, int lastbyte) {
+
+	// Base64 subject to comply with RFC2047 using UTF-8 encoding
+	// The encoding header length for UTF-8 is 13
+	char *CRLF = "\r\n ";
+
+	// Position markers for the cycles
+	int startbyte = 0;
+	int priorposbyte = 0;
+	int oldposbyte = -1;
+	int nextposbyte = 0;
+
+	// Probable size of the new RFC2047 compliant subject
+	int enclen = lastbyte*4;
+
+	// This will hold the final encoded and RFC2047 compliant subject
+	char subject[enclen];
+
+	// This will hold a ZBX_EMAIL_B64_MAXLINE subject section in each cycle
+	char tmpsubject[ZBX_EMAIL_B64_MAXLINE];
+
+	assert(src);
+	assert(!*p_base64);
+
+	// Adds each line of UTF-8 encoded line to the final subject
+	while (nextposbyte != -1 && priorposbyte < lastbyte) {
+
+		// Temporary string holder
+		char *inctmp = NULL;
+
+		// Current size of the base64 encoded string
+		int currlen = 0;
+
+		// Iterates through the string until it hits the ZBX_EMAIL_B64_MAXLINE limit
+		// The limit of 46 comes from standard base64 encoding assumptions
+		while (((nextposbyte - startbyte) < 46) && (nextposbyte <= lastbyte) && (oldposbyte != nextposbyte)) {
+
+			nextposbyte = get_nextutf8_byte(src, priorposbyte);
+
+			if (nextposbyte == -1) {
+				break;
+			}
+
+			// Update markers
+			oldposbyte = priorposbyte;
+			priorposbyte = nextposbyte;
+		}
+
+		// Test the size of what we chosen.
+		str_base64_encode_dyn((src + startbyte), &inctmp, (oldposbyte - startbyte));
+
+		currlen = strlen(inctmp);
+
+		// Format the string for email subject format
+		zbx_snprintf(tmpsubject, ZBX_EMAIL_B64_MAXLINE, "=?UTF-8?B?%s?=", inctmp);
+
+		// Before finishing concatenate the result we have. If it's the first cycle,
+		// copy the string, if not, append.
+		if (startbyte == 0) {
+			zbx_strlcpy(subject, tmpsubject, enclen);
+		} else {
+			zbx_strlcat(subject, tmpsubject, enclen);
+		}
+
+		if (nextposbyte != -1 && oldposbyte < lastbyte) {
+			zbx_strlcat(subject, CRLF, enclen);
+		}
+
+		startbyte = oldposbyte;
+	}
+
+	*p_base64 = zbx_malloc(NULL, strlen(subject) + 1);
+	zbx_strlcpy(*p_base64, subject, strlen(subject));
+
+}
+
+/******************************************************************************
+ *                                                                            *
  * Comments: reads until '\n'                                                 *
  *                                                                            *
  ******************************************************************************/
@@ -196,15 +305,44 @@ int	send_email(const char *smtp_server, 
 	if (FAIL == is_ascii_string(localsubject))
 	{
 		int	len;
+		base64 = NULL;
+
+		int origlen = strlen(localsubject);
 
+		// Encode to Base64
 		str_base64_encode_dyn(localsubject, &base64, strlen(localsubject));
-		zbx_free(localsubject);
 
-		len = strlen(base64) + 13;
-		localsubject = zbx_malloc(NULL, len);
-		zbx_snprintf(localsubject, len, "=?UTF-8?B?%s?=", base64);
-		zbx_free(base64);
-		base64 = NULL;
+		// Find the length of the entire encoded string
+		len = strlen(base64);
+
+		int finallen = 0;
+
+		if (len + 14 > ZBX_EMAIL_B64_MAXLINE) {
+
+			zbx_free(base64);
+
+			str_base64_encode_rfc2047(localsubject, &base64, origlen);
+
+			finallen = strlen(base64) + 1;
+			zbx_free(localsubject);
+			localsubject = zbx_malloc(NULL, finallen);
+			zbx_strlcpy(localsubject, base64, finallen);
+
+			zbx_free(base64);
+
+		} else {
+
+			// Finally free the localsubject variable
+			zbx_free(localsubject);
+
+			// Get the final length of string required
+			finallen = len + 14;
+
+			// Continue as before, filling the localsubject variable with the desired data
+			localsubject = zbx_malloc(NULL, finallen);
+			zbx_snprintf(localsubject, finallen, "=?UTF-8?B?%s?=", base64);
+			zbx_free(base64);
+		}
 	}
 
 	/* prepare body */
