我正在使用 python 请求来发布请求。 当附件参数有一些非ascii字符时会引发异常,在其他只有ascii数据存在的情况下,一切都很好。
you can see the exception here
response = requests.post(url="https://api.mailgun.net/v2/%s/messages" % utils.config.mailDomain,
auth=("api", utils.config.mailApiKey),
data={
"from" : me,
"to" : recepients,
"subject" : subject,
"html" if html else "text" : message
},
files= [('attachment', codecs.open(f.decode('utf8'))) for f in attachments] if attachments and len(attachments) else []
)
编辑: 使用 utf8 解码文件名后,我没有得到异常,但文件未附加。 我通过附加名称中只有 ascii 字符的文件来调试请求,并且请求 header 请求构建是:
{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': u'form-data; name="attachment"; filename="Hello.docx"'}
这成功了,我收到了带有附件的邮件。
但是,当使用带有希伯来字符的文件时,请求的 header 是:
{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': 'form-data; name="attachment"; filename*=utf-8\'\'%D7%91%D7%93%D7%99%D7%A7%D7%94.doc'}
我收到了邮件,但没有附加文件。有什么想法吗?
最佳答案
当文件名包含非 ascii 时,请求库按照标准 RFC 2231 对其进行编码.格式如你所见:filename*=utf-8''......
。似乎 MailGun 不支持这个标准,因此,非 ascii 文件名丢失了。您可以联系 MailGun 以确认他们对 unicode 文件名的期望格式。
作为一种不完美的解决方法,您可以将非 ascii 字符替换为:
def replace_non_ascii(x): return ''.join(i if ord(i) < 128 else '_' for i in x)
并在调用请求时明确指定文件名(假设 attachments
是基于 unicode 的文件名列表):
files= [('attachment', (replace_non_ascii(f), codecs.open(f))) for f in attachments] ...
编辑
如果您想自定义 header 格式,我们假设(而不是标准的 RFC 2231)MailGun 可以接受这种格式:
filename="%D7%91%D7%93%D7%99%D7%A7%D7%94.doc"
然后您可以将文件名自定义为:
import urllib
def custom_filename(x): return urllib.quote(x.encode('utf8'))
files= [('attachment', (custom_filename(f), codecs.open(f))) for f in attachments] ...
根据 MailGun 的响应,您可能需要调整 requests
代码或改用低级库 (urllib2)。希望他们能支持 RFC 2231
关于python 请求非 ascii 文件名的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397418/