发送带有附件的邮件,但不发送附件,仅发送正文

时间:2019-07-06 03:38:28

标签: python django django-email

我正在发送一封带有pdf附件的邮件,但在接收方只能接收正文而不是附件

这是我在运行脚本时收到的错误,但是邮件已发送

Traceback (most recent call last):
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 799, in write
    self._sock.sendall(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Result Creater\virtual\lib\site-packages\django\core\servers\basehttp.py", line 116, in handle_error
    super().handle_error()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Result Creater\virtual\lib\site-packages\django\core\servers\basehttp.py", line 171, in handle
    self.handle_one_request()
  File "C:\Result Creater\virtual\lib\site-packages\django\core\servers\basehttp.py", line 194, in handle_one_request
    handler.run(self.server.get_app())
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Result Creater\virtual\lib\site-packages\django\core\servers\basehttp.py", line 111, in close
    super().close()
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python37\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'

我的发送电子邮件功能-

def send_email(request):
    subject='Mark Sheet'
    message='You requested for your Marksheet'
    email_from=settings.EMAIL_HOST_USER
    recipient_list=[request.user.email]
    email=EmailMessage(subject,message,email_from,recipient_list,
        headers={'Reply- To':settings.EMAIL_HOST_USER})
    file=str(request.user)+'.pdf'
    email.attach_file=(f"/store/{file}")
    email.send()
    return redirect('home')

我的用于创建pdf的Pdf类

class Pdf(View):

def get(self,request):
    marks=Marks.objects.filter(user_name=request.user).first()
    today=timezone.now()
    params={
        'today':today,
        'marks':marks,
        'request':request
    }
    file=Render.render_to_file('user/pdf.html',params)
    return redirect('send_email')

1 个答案:

答案 0 :(得分:0)

您的语法错误:

email.attach_file=(f"/store/{file}")

attach_file()是一种方法,并且您要用=为其分配值,这意味着该方法将在对象上被替换而不是被调用。更改为:

email.attach_file(f"/store/{file}")