docxファイルを添付したメールを送りたいです。views.pyに
# -*- coding: utf-8 -*- import email,email.encoders,email.mime.text,email.mime.base from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email import encoders from email.mime.base import MIMEBase def mail(mail, docx): msg = MIMEMultipart() msg["Subject"] = "SEND MAIL" msg["From"] = settings.EMAIL_HOST_USER msg["To"] = email message = "SEND" body = MIMEText(message) msg.attach(body) mime = {'type': 'text', 'subtype': 'docx'} attach_file = {'name': 'test.docx', 'path': '/Users/hasuikeyuri/Downloads/legal_doc_mail/test.docx'} attachment = MIMEBase(mime['type'], mime['subtype']) file = open(attach_file['path']) attachment.set_payload(file.read()) file.close() encoders.encode_base64(attachment) msg.attach(attachment) attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name']) msg.send()
とコードを書きました。
Traceback に
Traceback (most recent call last): File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 407, in make_document mail(mail, file) File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 442, in mail attachment.set_payload(file.read()) File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 11: invalid start byte
と出ました。
なぜこのようなことが起こるのか理解できません。書類はutf-8で保存されているはずです。
何が問題なのでしょうか?どのように直せばいいのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/13 14:17
2018/10/13 15:04
2018/10/14 00:11