SMTP Error (gmail sending) Using Django with Railway in Deployment

I am getting the following error in Railway Deploy logs when it is trying to send Gmail.

Internal Server Error: /register/

Traceback (most recent call last):

  File "/opt/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner

    response = get_response(request)

               ^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper

    return view_func(request, *args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view

    return self.dispatch(request, *args, **kwargs)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch

    response = self.handle_exception(exc)

               ^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception

    self.raise_uncaught_exception(exc)

  File "/opt/venv/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception

    raise exc

  File "/opt/venv/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch

    response = handler(request, *args, **kwargs)

               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/app/hostel_app/views.py", line 43, in post

    CustomUserSerializer().send_verification_email(user, token)

  File "/app/hostel_app/serializers.py", line 56, in send_verification_email

    send_mail(subject, message, email_from, recipient_list)

  File "/opt/venv/lib/python3.11/site-packages/django/core/mail/__init__.py", line 88, in send_mail

    return mail.send()

           ^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/core/mail/message.py", line 301, in send

    return self.get_connection(fail_silently).send_messages([self])

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/core/mail/backends/smtp.py", line 128, in send_messages

    new_conn_created = self.open()

                       ^^^^^^^^^^^

  File "/opt/venv/lib/python3.11/site-packages/django/core/mail/backends/smtp.py", line 95, in open

    self.connection.login(self.username, self.password)

  File "/root/.nix-profile/lib/python3.11/smtplib.py", line 750, in login

    raise last_exception

  File "/root/.nix-profile/lib/python3.11/smtplib.py", line 739, in login

    (code, resp) = self.auth(

                   ^^^^^^^^^^

  File "/root/.nix-profile/lib/python3.11/smtplib.py", line 662, in auth

    raise SMTPAuthenticationError(code, resp)

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8  https://support.google.com/mail/?p=BadCredentials d2e1a72fcca58-72dab81530asm3463874b3a.52 - gsmtp')

The same code I am using on local and EC2 instance so there it is working fine. The code I am using in settings file is as:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True  # or False depending on your setup
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')

The password is app password which I generated from gmail and gmail is my personal gmail. Anyone have the solution for this issue?

Вернуться на верх