Index: camel/camel-mime-utils.c
===================================================================
--- camel/camel-mime-utils.c	(revision 9742)
+++ camel/camel-mime-utils.c	(working copy)
@@ -3281,7 +3281,33 @@ camel_header_param_list_decode(const cha
 }
 
 static char *
-header_encode_param (const unsigned char *in, gboolean *encoded)
+encode_filename (const char *inptr)
+{
+	char *to_free = NULL, *str;
+
+	if (!g_utf8_validate (inptr, -1, NULL)) {
+		GString *buff = g_string_new ("");
+
+		for (; inptr && *inptr; inptr++) {
+			if (*inptr < 32)
+				g_string_append_printf (buff, "=%02X", (*inptr) & 0xFF);
+			else
+				g_string_append_c (buff, *inptr);
+		}
+
+		to_free = g_string_free (buff, FALSE);
+		inptr = to_free;
+	}
+
+	str = camel_header_encode_string (inptr);
+
+	g_free (to_free);
+
+	return str;
+}
+
+static char *
+header_encode_param (const unsigned char *in, gboolean *encoded, gboolean is_filename)
 {
 	const unsigned char *inptr = in;
 	unsigned char *outbuf = NULL;
@@ -3297,16 +3323,30 @@ header_encode_param (const unsigned char
 	/* if we have really broken utf8 passed in, we just treat it as binary data */
 
 	charset = camel_charset_best((char *) in, strlen((char *) in));
-	if (charset == NULL)
-		return g_strdup((char *) in);
+	if (charset == NULL) {
+		if (is_filename)
+			return encode_filename (in);
+		else
+			return g_strdup((char *) in);
+	}
 
 	if (g_ascii_strcasecmp(charset, "UTF-8") != 0) {
 		if ((outbuf = (unsigned char *) header_convert(charset, "UTF-8", (const char *) in, strlen((char *) in))))
 			inptr = outbuf;
+		else if (is_filename)
+			return encode_filename (in);
 		else
 			return g_strdup((char *) in);
 	}
 
+	if (is_filename) {
+		/* do not set encoded flag for file names */
+		str = encode_filename (inptr);
+		g_free (outbuf);
+
+		return str;
+	}
+
 	/* FIXME: set the 'language' as well, assuming we can get that info...? */
 	out = g_string_new (charset);
 	g_string_append(out, "''");
@@ -3322,7 +3362,7 @@ header_encode_param (const unsigned char
 	str = out->str;
 	g_string_free (out, FALSE);
 	*encoded = TRUE;
-	
+
 	return str;
 }
 
@@ -3332,6 +3372,7 @@ camel_header_param_list_format_append (G
 	int used = out->len;
 
 	while (p) {
+		gboolean is_filename = g_ascii_strcasecmp (p->name, "filename") == 0 || g_ascii_strcasecmp (p->name, "name") == 0;
 		gboolean encoded = FALSE;
 		gboolean quote = FALSE;
 		int here = out->len;
@@ -3343,7 +3384,7 @@ camel_header_param_list_format_append (G
 			continue;
 		}
 
-		value = header_encode_param ((unsigned char *) p->value, &encoded);
+		value = header_encode_param ((unsigned char *) p->value, &encoded, is_filename);
 		if (!value) {
 			w(g_warning ("appending parameter %s=%s violates rfc2184", p->name, p->value));
 			value = g_strdup (p->value);
@@ -3360,17 +3401,19 @@ camel_header_param_list_format_append (G
 			quote = ch && *ch;
 		}
 
+		quote = quote || is_filename;
 		nlen = strlen (p->name);
 		vlen = strlen (value);
 
-		if (used + nlen + vlen > CAMEL_FOLD_SIZE - 8) {
+		/* do not fold file names */
+		if (!is_filename && used + nlen + vlen > CAMEL_FOLD_SIZE - 8) {
 			out = g_string_append (out, ";\n\t");
 			here = out->len;
 			used = 0;
 		} else
 			out = g_string_append (out, "; ");
 
-		if (nlen + vlen > CAMEL_FOLD_SIZE - 8) {
+		if (!is_filename && nlen + vlen > CAMEL_FOLD_SIZE - 8) {
 			/* we need to do special rfc2184 parameter wrapping */
 			int maxlen = CAMEL_FOLD_SIZE - (nlen + 8);
 			char *inptr, *inend;
Index: camel/ChangeLog
===================================================================
--- camel/ChangeLog	(revision 9742)
+++ camel/ChangeLog	(working copy)
@@ -1,3 +1,13 @@
+2008-11-07  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #524377
+
+	* camel-mime-utils.c: (encode_filename), (header_encode_param),
+	(camel_header_param_list_format_append): Encode 'filename' and 'name'
+	parameters based on RFC 2047, instead of the correct RFC 2231, because
+	other clients/servers (like Outlook and Gmail) do not understand
+	the standard encoding.
+
 2008-11-07  Srinivasa Ragavan  <sragavan@novell.com>
 
 	* camel-vee-summary.c: Make unread vfolders to work for match threads.
