@@ -57,7 +57,11 @@ def _splitparam(param):
5757def _formatparam (param , value = None , quote = True ):
5858 """Convenience function to format and return a key=value pair.
5959
60- This will quote the value if needed or if quote is true.
60+ This will quote the value if needed or if quote is true. If value is a
61+ three tuple (charset, language, value), it will be encoded according
62+ to RFC2231 rules. If it contains non-ascii characters it will likewise
63+ be encoded according to RFC2231 rules, using the utf-8 charset and
64+ a null language.
6165 """
6266 if value is not None and len (value ) > 0 :
6367 # A tuple is used for RFC 2231 encoded parameter values where items
@@ -67,6 +71,12 @@ def _formatparam(param, value=None, quote=True):
6771 # Encode as per RFC 2231
6872 param += '*'
6973 value = utils .encode_rfc2231 (value [2 ], value [0 ], value [1 ])
74+ else :
75+ try :
76+ value .encode ('ascii' )
77+ except UnicodeEncodeError :
78+ param += '*'
79+ value = utils .encode_rfc2231 (value , 'utf-8' , '' )
7080 # BAW: Please check this. I think that if quote is set it should
7181 # force quoting even if not necessary.
7282 if quote or tspecials .search (value ):
@@ -438,11 +448,19 @@ def add_header(self, _name, _value, **_params):
438448 name is the header field to add. keyword arguments can be used to set
439449 additional parameters for the header field, with underscores converted
440450 to dashes. Normally the parameter will be added as key="value" unless
441- value is None, in which case only the key will be added.
451+ value is None, in which case only the key will be added. If a
452+ parameter value contains non-ASCII characters it can be specified as a
453+ three-tuple of (charset, language, value), in which case it will be
454+ encoded according to RFC2231 rules. Otherwise it will be encoded using
455+ the utf-8 charset and a language of ''.
442456
443- Example :
457+ Examples :
444458
445459 msg.add_header('content-disposition', 'attachment', filename='bud.gif')
460+ msg.add_header('content-disposition', 'attachment',
461+ filename=('utf-8', '', Fußballer.ppt'))
462+ msg.add_header('content-disposition', 'attachment',
463+ filename='Fußballer.ppt'))
446464 """
447465 parts = []
448466 for k , v in _params .items ():
0 commit comments