import boto3 from fastapi import Form, FastAPI from fastapi.middleware.cors import CORSMiddleware from botocore.exceptions import ClientError from pydantic import BaseModel, EmailStr from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from datetime import date app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class User(BaseModel): name: str email: EmailStr phone: str sex: str class Html(User): area: str type: str mode: str budget: str pin: float room: str hall: str restroom: str style: str time: date class User2(BaseModel): name: str email: EmailStr phone: str class Html2(User2): loc: str h_class: str size: str @app.post('/hhh/mail/deco/v1') async def hhh_send_mail(html: Html): def render_template(template, kwargs): import jinja2 templateLoader = jinja2.FileSystemLoader(searchpath="./templates") templateEnv = jinja2.Environment(loader=templateLoader) templ = templateEnv.get_template(template) return templ.render(kwargs) SENDER = "Gorgeous Space " RECIPIENT = html.email AWS_REGION = "us-east-1" CHARSET = "UTF-8" client = boto3.client('ses',region_name=AWS_REGION) try: context = {} context["name"] = html.name context["phone"] = html.phone context["mail"] = html.email context["sex"] = html.sex context["area"] = html.area context["type"] = html.type context["mode"] = html.mode context["budget"] = html.budget context["pin"] = html.pin context["room"] = html.room context["hall"] = html.hall context["restroom"] = html.restroom context["style"] = html.style context["time"] = str(html.time) msg = MIMEMultipart() msg["Subject"] = "富邦裝修需求貸款通知信" msg["From"] = "noreply@hhh.com.tw" msg["To"] = RECIPIENT body = MIMEText(render_template("email.html",{"context":context}), "html") msg.attach(body) response = client.send_raw_email( Source=msg["From"], Destinations=[msg["To"]], RawMessage={"Data": msg.as_string()} ) print(response) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['MessageId']) @app.post('/hhh/mail/deco/v2') async def hhh_send_mail(name: str = Form(...), phone: str = Form(...), email: EmailStr = Form(...), loc: str = Form(...),h_class: str = Form(...),size: float = Form(...)): def render_template(template, kwargs): import jinja2 templateLoader = jinja2.FileSystemLoader(searchpath="./templates") templateEnv = jinja2.Environment(loader=templateLoader) templ = templateEnv.get_template(template) return templ.render(kwargs) SENDER = "Gorgeous Space " RECIPIENT = email AWS_REGION = "us-east-1" CHARSET = "UTF-8" client = boto3.client('ses',region_name=AWS_REGION) try: context = {} context["name"] = name context["phone"] = phone context["mail"] = email context["loc"] = loc context["h_class"] = h_class context["size"] = size msg = MIMEMultipart() msg["Subject"] = "富邦裝修需求貸款通知信" msg["From"] = "noreply@hhh.com.tw" msg["To"] = RECIPIENT body = MIMEText(render_template("email_v2.html",{"context":context}), "html") msg.attach(body) response = client.send_raw_email( Source=msg["From"], Destinations=[msg["To"]], RawMessage={"Data": msg.as_string()} ) print(response) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['MessageId'])