Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def mail(mail_adress, docx):
- msg = MIMEMultipart()
- msg["Subject"] = "TEST"
- msg["From"] = settings.EMAIL_HOST_USER
- msg["To"] = email
- message = "TEST"
- body = MIMEText(message)
- msg.attach(body)
- mime = {'type': 'text', 'subtype': 'docx'}
- attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
- attachment = MIMEBase(mime['type'], mime['subtype'])
- 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'])
- return msg
- def send(from_addr, to_addrs, msg):
- smtp = smtplib.SMTP()
- smtp.connect()
- from_addr = 'xxxx@gmail.com'
- from_addr_name = "test"
- to_addr= "yyyyy@gmail.com"
- subject = u"test"
- message = "test"
- body = MIMEText(message)
- mime = {'type': 'text', 'subtype': 'docx'}
- attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
- msg = mail(from_addr, from_addr_name, to_addr, subject, body, mime, attach_file)
- smtp.sendmail(from_addr, to_addrs, msg)
- smtp.close()
- send()
- EMAIL_HOST = 'smtp.gmail.com'
- EMAIL_PORT = 587
- EMAIL_HOST_USER = 'xxxx@gmail.com'
- EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
- EMAIL_USE_TLS = True
- 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 400, in make_document
- send()
- File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 454, in send
- smtp.connect()
- File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 336, in connect
- self.sock = self._get_socket(host, port, self.timeout)
- File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 307, in _get_socket
- self.source_address)
- File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 724, in create_connection
- raise err
- File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 713, in create_connection
- sock.connect(sa)
- ConnectionRefusedError: [Errno 61] Connection refused
Add Comment
Please, Sign In to add comment