[Python] "Value is not a valid email address" when sending multiple email addresses using...

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using fastapi-mail package, and trying to send multiple files to multiple email addresses. When I send the email to only one email address, the application works as expected. However, when I change to List[EmailStr] for sending to multiple email addresses, I get this error:

    not a valid email address


    Here is my code:

    @app.post("/file")async def send_file(
    background_tasks: BackgroundTasks,
    email:List[EmailStr] = Form(...), #I Change here before EmailStr = Form(...)
    file:Optional[List[UploadFile]] = File(...),) -> JSONResponse:
    print(email)
    print(file)
    message = MessageSchema(
    subject="Fastapi mail module",
    recipients=email,
    body="Simple background task",
    subtype="html",
    attachments=file)

    fm = FastMail(ConnectionConfig(
    MAIL_USERNAME=res("MAIL_USERNAME"),
    MAIL_PASSWORD=res("MAIL_PASSWORD"),
    MAIL_FROM="admin@acsebs.com",
    MAIL_PORT=res("MAIL_PORT"),
    MAIL_SERVER=res("MAIL_SERVER"),
    MAIL_FROM_NAME="send attachment email service",
    MAIL_TLS=res("MAIL_TLS"),
    MAIL_SSL=res("MAIL_SSL"),
    USE_CREDENTIALS=res("USE_CREDENTIALS"),
    VALIDATE_CERTS=res("VALIDATE_CERTS")
    ))

    background_tasks.add_task(fm.send_message, message)

    return JSONResponse(status_code=200, content={"message": "email has been sent"})

    Posting data through Swagger UI:


    [​IMG]

    The error:


    [​IMG]

    Continue reading...

Compartilhe esta Página