Guest User

Untitled

a guest
Oct 13th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. def mail(mail_adress, docx):
  2.  
  3. msg = MIMEMultipart()
  4. msg["Subject"] = "TEST"
  5. msg["From"] = settings.EMAIL_HOST_USER
  6. msg["To"] = email
  7.  
  8. message = "TEST"
  9. body = MIMEText(message)
  10. msg.attach(body)
  11.  
  12. mime = {'type': 'text', 'subtype': 'docx'}
  13. attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
  14.  
  15. attachment = MIMEBase(mime['type'], mime['subtype'])
  16. file = open(attach_file['path'], 'rb')
  17. attachment.set_payload(file.read())
  18. file.close()
  19. encoders.encode_base64(attachment)
  20. msg.attach(attachment)
  21. attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])
  22.  
  23. return msg
  24.  
  25. def send(from_addr, to_addrs, msg):
  26. smtp = smtplib.SMTP()
  27. smtp.connect()
  28.  
  29. from_addr = 'xxxx@gmail.com'
  30. from_addr_name = "test"
  31. to_addr= "yyyyy@gmail.com"
  32. subject = u"test"
  33. message = "test"
  34. body = MIMEText(message)
  35. mime = {'type': 'text', 'subtype': 'docx'}
  36. attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
  37.  
  38. msg = mail(from_addr, from_addr_name, to_addr, subject, body, mime, attach_file)
  39. smtp.sendmail(from_addr, to_addrs, msg)
  40. smtp.close()
  41.  
  42. send()
  43.  
  44. EMAIL_HOST = 'smtp.gmail.com'
  45. EMAIL_PORT = 587
  46. EMAIL_HOST_USER = 'xxxx@gmail.com'
  47. EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
  48. EMAIL_USE_TLS = True
  49.  
  50. Traceback (most recent call last):
  51. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
  52. response = get_response(request)
  53. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
  54. response = self.process_exception_by_middleware(e, request)
  55. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
  56. response = wrapped_callback(request, *callback_args, **callback_kwargs)
  57. File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 400, in make_document
  58. send()
  59. File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 454, in send
  60. smtp.connect()
  61. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 336, in connect
  62. self.sock = self._get_socket(host, port, self.timeout)
  63. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 307, in _get_socket
  64. self.source_address)
  65. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 724, in create_connection
  66. raise err
  67. File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 713, in create_connection
  68. sock.connect(sa)
  69. ConnectionRefusedError: [Errno 61] Connection refused
Add Comment
Please, Sign In to add comment