メールに書類を添付して送りたいです。
def mail(mail_adress, docx): smtp_host = 'smtp.gmail.com' smtp_port = 587 username = 'zzz' password = 'zzzzz' msg = message.EmailMessage() msg.set_content('test mail') msg['Subject'] = 'test mail(sub)' msg['From'] = 'xxxxxx@gmail.com' msg['To'] = 'xxx@gmail.com' mine = {'type': 'text', 'subtype': 'docx'} attachment = MIMEBase(mine['type'], mine['subtype']) attach_file = {'name': 'test.docx', 'path': '/Users//test.docx'} file = open(attach_file['path'], 'rb') attachment.set_payload(file.read()) file.close() encoders.encode_base64(attachment) msg.attach(attachment) attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name']) server = smtplib.SMTP(smtp_host, smtp_port) server.ehlo() server.starttls() server.ehlo() server.login(username, password) server.send_message(msg) server.quit()
とコードを書きました。このコードを実行すると、
Attach is not valid on a message with a non-multipart payload とエラーが出ました。Tracebackでは、
Traceback (most recent call last): File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/Users/xxx/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/xxx/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/xxx/Downloads/legal_doc_mail/common/views.py", line 401, in make_document mail(mail_adress, file) File "/Users/xxx/Downloads/legal_doc_mail/common/views.py", line 472, in mail msg.attach(attachment) File "/Users/xxx/anaconda3/envs/py36/lib/python3.6/email/message.py", line 210, in attach raise TypeError("Attach is not valid on a message with a" TypeError: Attach is not valid on a message with a non-multipart payload
とエラーが出ました。
どのように修正すればいいでしょうか?何が原因でこのエラーが出るのでしょうか?
あなたの回答
tips
プレビュー