I’m getting this error [SSL: CERTIFICATE_VERIFY_FAILED} (_ssl.c:1002) when im using fastapi_mail library when using my windows pc

I have a fastapi application that uses fastapi_mail library to send verification email to the user when the user register, also the app is begin containerized using docker, moreover the same image and the same app with it’s all it’s configuration is being used on my windows PC and on my Mac labtop, the main issue that i’m faceing is this error message only will occurs on my windows device but on my Mac is working fine and the email is begin send, but on the other hand on my windows i’m getting this error every single time why? also I have tried using different email and changed the app password from my Gmail multiple times but still wont work on my windows pc.

The ERROR:

fastapi_mail.errors.ConnectionErrors: Exception raised [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1002), check your credentials or email service configuration

this is the configuration for the email

conf = ConnectionConfig(
    MAIL_USERNAME = email_for_msg,
    MAIL_PASSWORD = email_pass,
    MAIL_FROM = email_for_msg,
    MAIL_PORT = 587,
    MAIL_SERVER = "smtp.gmail.com",
    MAIL_STARTTLS = True,
    MAIL_SSL_TLS = False,
    USE_CREDENTIALS = True,
    VALIDATE_CERTS = True
)

async def send_email(
        user_email:list[EmailStr],    
        token:str    
):
    
    template=f"""
    <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        </head>
        <body>
        <a href="http://localhost:9999/verify-email/{token}">
            please verify your account
        </a>
        </body>
        </html>
    """
    message = MessageSchema(
       subject="Fastapi-Mail module",
       recipients=user_email,  # List of recipients, as many as you can pass  
       body=template,
       subtype=MessageType.html
       )
    
    await fm.send_message(message)

Here on the email conf i have tried setting VALIDATE_CERTS = True to VALIDATE_CERTS = False it worked but this will have a security issues

and this is the dockerfile im using if it may help

FROM python:3.11.4-bullseye

RUN apt-get update && apt-get -y install libpq-dev gcc && pip install psycopg


WORKDIR /app

COPY requirements.txt /app

RUN pip install --no-cache-dir -r requirements.txt

COPY . /app

COPY templates app/templates
COPY static app/static


CMD ["uvicorn","main:app","--host","0.0.0.0","--port","8080"]

Leave a Comment