为了账号安全,请及时绑定邮箱和手机立即绑定

如何将已保存的 blob 作为附件通过电子邮件发送?

如何将已保存的 blob 作为附件通过电子邮件发送?

梦里花落0921 2021-08-17 17:09:33
我通过javascript将pdf作为blob发送到django,然后尝试将其作为电子邮件附件发送。我的代码是:def SendPrescriptionbyMail(request, cliniclabel, patient_id):    from django.core.files.storage import default_storage    print(request.FILES)    print(request.FILES['file'])    myform = forms.Form(request.POST, request.FILES)    file = myform.files['file']    print(file)    file_name = default_storage.save(file.name, file)    file = default_storage.open(file_name)    print(f'file_name is {file_name}')    file_url = default_storage.url(file_name)    print(f'Or maybe {file_url}')    recipient = 'joel@domain'    import smtplib    from email.mime.text import MIMEText    from email.mime.multipart import MIMEMultipart    msg = MIMEMultipart()    msg['Subject'] = "Your prescription"    msg['From']    = "admin@me"    msg['To']      = recipient    msg.preamble = 'Your prescription is attached'    msg.attach(MIMEText(file(file_name).read()))    s = smtplib.SMTP('smtp.mailgun.org', 587)    s.login('myid@somewhere', 'apikey')    s.sendmail(msg['From'], msg['To'], msg.as_string())    s.quit()    return HttpResponse('Successfully sent email')输出:<MultiValueDict: {'file': [<TemporaryUploadedFile: blob (application/pdf)>]}>blobblobfile_name is blob_4VZxpHYOr maybe /data/blob_4VZxpHY2018-11-14 19:02:36,554 django.request ERROR    Internal Server Error: /clinic/madhav/prescription/sendemail/patient/18Traceback (most recent call last):File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in innerresponse = get_response(request)File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_responseresponse = self.process_exception_by_middleware(e, request)File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号