123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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 <noreply@hhh.com.tw>"
- 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 <noreply@hhh.com.tw>"
- 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'])
|