Getting SSL error when sending mail via Django

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)

This is what I see when I send mail via Django.

This is my email configuration in settings.py

# Email Settings
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "xxxxxxxx@gmail.com"
EMAIL_HOST_PASSWORD = "xxxxxxxxxxxxx"
EMAIL_USE_TLS = True

this is how I send mail

    send_mail(
        "Subject",
        "Test message",
        settings.EMAIL_HOST_USER,
        ["xxxxxxxxx@gmail.com"],
        fail_silently=False,
    )

This error message is indicating that there is a problem with the SSL certificate being used to send the email. One possible cause of this error is that the certificate being used is not from a trusted issuer, or that it is out of date. To fix this issue, you may need to update your certificate or configure your application to use a different certificate. Additionally, make sure that the sending email address is allowed to access less secure apps.

also you can send email by doing this

    from django.core.mail import EmailMultiAlternatives
    subject, from_email, to = emailSubject, '<your_gmail@gmail.com>', ["where you 
     want to send"]
    msg = EmailMultiAlternatives(subject, from_email, to)
    msg.send()

I was using Django 4.2 and Python 3.10, running into the same issue. Downgrading Django to 4.1.7 worked for me. Hope this helps

Same here. Downgrading and it worked.

The default configuration has changed in Django 4.2, see here for details and workaround.

If you are encountering compatibility issues or errors when using Django 4.2 with Python 3.10, you may consider downgrading Django to a compatible version. Here are more detailed steps to downgrade Django:

  1. Identify the compatible Django version: Determine the Django version that is compatible with Python 3.10. You can refer to the Django project's documentation, release notes, or compatibility matrix to find the recommended Django version for Python 3.10.

  2. Update the Django version in requirements.txt: Open the requirements.txt file in your Django project's root directory. Locate the line that specifies the Django version, which may look like Django==4.2. Replace the version number with the compatible Django version. For example, if Django 4.1.7 is compatible, the line should be changed to Django==4.1.7.

  3. Save the changes: Save the updated requirements.txt file.

  4. Install the compatible Django version: Open a terminal or command prompt and navigate to your Django project's directory. Run the following command to install the compatible Django version:

    pip install -r requirements.txt --upgrade
    

    This command will install the specified Django version and its dependencies.

  5. Verify the Django version: After the installation completes, you can verify that Django has been downgraded to the compatible version by running the following command:

    python -m django --version

    This command will display the currently installed Django version, which should match the downgraded version you specified in requirements.txt.

By following these steps, you can successfully downgrade Django to a compatible version and resolve any compatibility issues with Python 3.10. It's important to note that downgrading Django may impact certain features or functionalities available in newer versions, so be sure to test your application thoroughly after the downgrade.

Installing collected packages: Django Attempting uninstall: Django Found existing installation: Django 4.2.2 Uninstalling Django-4.2.2: Successfully uninstalled Django-4.2.2 Successfully installed Django-4.1.7

python3 -m django --version

4.1.7

pip install -r requirements.txt --upgrade

Back to Top