我正在嘗試使用 發送電子郵件FastAPI-mail,即使發送成功,當我在 Gmail 或 Outlook 中打開電子郵件時,內容(訊息)也會出現兩次。
我正在查看代碼,但我認為我沒有將訊息附加兩次(另請注意,頂部訊息始終顯示標簽,而第二條則不顯示(見下圖)。
任何幫助將不勝感激!

main.py
from fastapi import FastAPI
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from starlette.requests import Request
from starlette.responses import JSONResponse
from pydantic import EmailStr, BaseModel
from typing import List
app = FastAPI()
class EmailSchema(BaseModel):
email: List[EmailStr]
conf = ConnectionConfig(
MAIL_USERNAME='myGmailAddress',
MAIL_PASSWORD="myPassword",
MAIL_FROM='myGmailAddress',
MAIL_PORT=587,
MAIL_SERVER="smtp.gmail.com",
MAIL_TLS=True,
MAIL_SSL=False
)
@app.post("/send_mail")
async def send_mail(email: EmailSchema):
template = """
<html>
<body>
<p>Hi !!!
<br>Thanks for using <b>fastapi mail</b>!!!</p>
</body>
</html>
"""
message = MessageSchema(
subject="Fastapi-Mail module",
recipients=email.dict().get("email"), # List of recipients, as many as you can pass
body=template,
subtype="html"
)
template = """
<p>Hi !!!
<br>Thanks for using <b>fastapi mail</b>!!!
</p>"""
'''
template = """
<p>Hi !!!
<br>Thanks for using <b>fastapi mail</b>!!!
</p>"""
'''
fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})
uj5u.com熱心網友回復:
而不是body,使用該html屬性。
message = MessageSchema(
subject="Fastapi-Mail module",
recipients=email.dict().get("email"), # List of recipients, as many as you can pass
html=template, # <<<<<<<<< here
subtype="html"
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516115.html
上一篇:Python電子郵件發送
