简体   繁体   English

如何使用模板在fastapi中发送email

[英]How to send email in fastapi with template

conf = ConnectionConfig(
    USERNAME=config.mail_username,
    PASSWORD=config.mail_pasword,
    FROM=config.mail_from,
    PORT=config.mail_port,
    SERVER=config.mail_server,
    USE_CREDENTIALS=True,
    VALIDATE_CERTS=True,
    TEMPLATE_FOLDER='./templates'
)

async def send_email(email_to: EmailSchema, body:EmailSchema) -> JSONResponse:
    message = MessageSchema(
        subject="fastapi",
        recipients=[email_to],
        body=body,
        subtype="html"
    )

    fm = FastMail(conf)
    
    await fm.send_message(message,template_name='email.html')

data="xyz"
@app.get("/email")
async def endpoint_send_email(
): 
    await send_email(
        email_to=email_to,
        body=data
        )


email.html

<!DOCTYPE html>
<html>
  <head>
  <title>email</title>
  </head>
  <body>
    <h4>Hi Team</h4>
    <p>get the data of date {{date}}</p><br />
    {{body.data}}
    <br /><br />
    <h4>thanks,</h4>
    <h4>Team</h4>
  </body>
</html>

When i try to send email without using template_name its sending with body values xyz (plain)当我尝试在不使用template_name的情况下发送 email 时,它会发送正文值xyz (纯文本)

I need to send in this template format if i use template name i am getting below error.如果我使用模板名称,我需要以这种模板格式发送我收到以下错误。 Help me to find solutions thanks帮助我找到解决方案谢谢

typeerror: 'posixpath' object is not iterable python typeerror: 'posixpath' object 不可迭代 python

well, you are passing your HTML file as text so that's why you won't receive the email as a template.好吧,您将 HTML 文件作为文本传递,这就是为什么您不会收到 email 作为模板的原因。 you can use the jinja2 library to render your template and send it correctly.您可以使用 jinja2 库来呈现您的模板并正确发送。 you create an environment variable你创建一个环境变量

env = Environment(
   loader=PackageLoader('app', 'templates'),#where you are getting the templates from
   autoescape=select_autoescape(['html', 'xml']))
template = env.get_template(template_name)
html = template.render(
    name=email,
    code=code,
    subject=subject
)

then you use MessageSchema and you send it as you did!然后你使用 MessageSchema 并像你一样发送它! hope my answer helps you希望我的回答对你有帮助

env = Environment(
    loader=FileSystemLoader(searchpath="./templates"),
    autoescape=select_autoescape(['html', 'xml'])
)
async def sendMail(url,email_to: EmailSchema):
  conf = ConnectionConfig(
    USERNAME=config.mail_username,
    PASSWORD=config.mail_pasword,
    FROM=config.mail_from,
    PORT=config.mail_port,
    SERVER=config.mail_server,
    USE_CREDENTIALS=True,
    VALIDATE_CERTS=True,
 )
  template = env.get_template('email.html')
   html = template.render(
        url=url,
  )
  message = MessageSchema(
        subject="fastapi",
        recipients=[email_to],
        body=body,
        subtype="html"
   )

  fm = FastMail(conf)
    await fm.send_message(message)

data="xyz"
@app.get("/email")
async def endpoint_send_email(
): 
    await send_email(
        email_to=email_to,
        url=data
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM