{"id":99,"date":"2014-11-27T16:53:32","date_gmt":"2014-11-27T08:53:32","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=99"},"modified":"2026-02-18T01:20:37","modified_gmt":"2026-02-17T17:20:37","slug":"email-attachment-with-unicode-filename-in-django","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/email-attachment-with-unicode-filename-in-django.html","title":{"rendered":"Send Email Attachments with Unicode Filenames in Django"},"content":{"rendered":"<p>\u4f7f\u7528 MIMEApplication \u4e26\u8a2d\u7f6e attachment \u7684 header<br \/>\n\u4e0d\u7136\u5982\u679c\u6a94\u540d\u6709\u4e2d\u6587\u7684\u8a71<br \/>\n\u7528 Gmail \u6536\u5230\u7684\u9644\u4ef6\u6a94\u540d\u6703\u662f noname<\/p>\n<pre class=\"line-numbers\"><code class=\"language-py\">def withdraw_send_email(request, pk):\n    withdraw = get_object_or_404(Withdraw, pk=pk)\n\n    email = 'vinta.chen@gmail.com'\n    subject = _(u'Packer \u6578\u4f4d\u767c\u884c\u670d\u52d9')\n    body = render_to_string('dps\/email\/withdraw_notice.html', {})\n\n    message = EmailMessage(\n        subject=subject,\n        body=body,\n        to=[email, ],\n    )\n    message.content_subtype = 'html'\n\n    from email.mime.application import MIMEApplication\n\n    # \u6a94\u540d\u5305\u542b\u4e2d\u6587\u7684\u8a71\uff0c\u8981\u7528\u9019\u7a2e\u65b9\u5f0f Gmail \u6536\u5230\u7684\u9644\u4ef6\u6a94\u540d\u624d\u4e0d\u6703\u662f noname\n    filename = '%s \u7d50\u7b97\u8868.xlsx' % (withdraw.serial_number[:6].encode('utf-8'))\n    file_data = withdraw.file.read()\n    attach_file = MIMEApplication(file_data, _subtype='vnd.ms-excel')\n    attach_file.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % (filename))\n\n    message.attach(attach_file)\n\n    message.send()\n\n    return HttpResponse('OK')<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/email.mime.html\">https:\/\/docs.python.org\/2\/library\/email.mime.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 MIMEApplication \u4e26\u8a2d\u7f6e attachment \u7684 header \u4e0d\u7136\u5982\u679c\u6a94\u540d\u6709\u4e2d\u6587\u7684\u8a71 \u7528 Gmail \u6536\u5230\u7684\u9644\u4ef6\u6a94\u540d\u6703\u662f noname def withdraw_send_email(request, pk): withdraw = get_object_or_404(Withdraw, pk=pk) email = 'vinta.chen@gmail.com' subject = _(u'Packer \u6578\u4f4d\u767c\u884c\u670d\u52d9') body = render_to_string('dps\/email\/withdraw_notice.html', {}) message = EmailMessage( subject=subject, body=body, to=[email, ], ) message.content_subtype = 'html' from email.mime.application import MIMEApplication # \u6a94\u540d\u5305\u542b\u4e2d\u6587\u7684\u8a71\uff0c\u8981\u7528\u9019\u7a2e\u65b9\u5f0f Gmail \u6536\u5230\u7684\u9644\u4ef6\u6a94\u540d\u624d\u4e0d\u6703\u662f noname filename = '%s \u7d50\u7b97\u8868.xlsx' % (withdraw.serial_number[:6].encode('utf-8'))&hellip; <a href=\"https:\/\/vinta.ws\/code\/email-attachment-with-unicode-filename-in-django.html\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":770,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,116],"tags":[13,2],"class_list":["post-99","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","category-about-web-development","tag-django","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}