main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import boto3
  2. from fastapi import Form, FastAPI
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from botocore.exceptions import ClientError
  5. from pydantic import BaseModel, EmailStr
  6. from email.mime.multipart import MIMEMultipart
  7. from email.mime.text import MIMEText
  8. from datetime import date
  9. app = FastAPI()
  10. app.add_middleware(
  11. CORSMiddleware,
  12. allow_origins=["*"],
  13. allow_credentials=True,
  14. allow_methods=["*"],
  15. allow_headers=["*"],
  16. )
  17. class User(BaseModel):
  18. name: str
  19. email: EmailStr
  20. phone: str
  21. sex: str
  22. class Html(User):
  23. area: str
  24. type: str
  25. mode: str
  26. budget: str
  27. pin: float
  28. room: str
  29. hall: str
  30. restroom: str
  31. style: str
  32. time: date
  33. class User2(BaseModel):
  34. name: str
  35. email: EmailStr
  36. phone: str
  37. class Html2(User2):
  38. loc: str
  39. h_class: str
  40. size: str
  41. @app.post('/hhh/mail/deco/v1')
  42. async def hhh_send_mail(html: Html):
  43. def render_template(template, kwargs):
  44. import jinja2
  45. templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
  46. templateEnv = jinja2.Environment(loader=templateLoader)
  47. templ = templateEnv.get_template(template)
  48. return templ.render(kwargs)
  49. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  50. RECIPIENT = html.email
  51. AWS_REGION = "us-east-1"
  52. CHARSET = "UTF-8"
  53. client = boto3.client('ses',region_name=AWS_REGION)
  54. try:
  55. context = {}
  56. context["name"] = html.name
  57. context["phone"] = html.phone
  58. context["mail"] = html.email
  59. context["sex"] = html.sex
  60. context["area"] = html.area
  61. context["type"] = html.type
  62. context["mode"] = html.mode
  63. context["budget"] = html.budget
  64. context["pin"] = html.pin
  65. context["room"] = html.room
  66. context["hall"] = html.hall
  67. context["restroom"] = html.restroom
  68. context["style"] = html.style
  69. context["time"] = str(html.time)
  70. msg = MIMEMultipart()
  71. msg["Subject"] = "富邦裝修需求貸款通知信"
  72. msg["From"] = "noreply@hhh.com.tw"
  73. msg["To"] = RECIPIENT
  74. body = MIMEText(render_template("email.html",{"context":context}), "html")
  75. msg.attach(body)
  76. response = client.send_raw_email(
  77. Source=msg["From"],
  78. Destinations=[msg["To"]],
  79. RawMessage={"Data": msg.as_string()}
  80. )
  81. print(response)
  82. # Display an error if something goes wrong.
  83. except ClientError as e:
  84. print(e.response['Error']['Message'])
  85. else:
  86. print("Email sent! Message ID:"),
  87. print(response['MessageId'])
  88. @app.post('/hhh/mail/deco/v2')
  89. async def hhh_send_mail(name: str = Form(...), phone: str = Form(...), email: EmailStr = Form(...), loc: str = Form(...),h_class: str = Form(...),size: float = Form(...)):
  90. def render_template(template, kwargs):
  91. import jinja2
  92. templateLoader = jinja2.FileSystemLoader(searchpath="./templates")
  93. templateEnv = jinja2.Environment(loader=templateLoader)
  94. templ = templateEnv.get_template(template)
  95. return templ.render(kwargs)
  96. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  97. RECIPIENT = email
  98. AWS_REGION = "us-east-1"
  99. CHARSET = "UTF-8"
  100. client = boto3.client('ses',region_name=AWS_REGION)
  101. try:
  102. context = {}
  103. context["name"] = name
  104. context["phone"] = phone
  105. context["mail"] = email
  106. context["loc"] = loc
  107. context["h_class"] = h_class
  108. context["size"] = size
  109. msg = MIMEMultipart()
  110. msg["Subject"] = "富邦裝修需求貸款通知信"
  111. msg["From"] = "noreply@hhh.com.tw"
  112. msg["To"] = RECIPIENT
  113. body = MIMEText(render_template("email_v2.html",{"context":context}), "html")
  114. msg.attach(body)
  115. response = client.send_raw_email(
  116. Source=msg["From"],
  117. Destinations=[msg["To"]],
  118. RawMessage={"Data": msg.as_string()}
  119. )
  120. print(response)
  121. # Display an error if something goes wrong.
  122. except ClientError as e:
  123. print(e.response['Error']['Message'])
  124. else:
  125. print("Email sent! Message ID:"),
  126. print(response['MessageId'])