소스 검색

v1 by json b2 by form formate

conradlan 3 년 전
커밋
16ff2aee4f
10개의 변경된 파일309개의 추가작업 그리고 0개의 파일을 삭제
  1. 0 0
      .env
  2. 30 0
      .vscode/launch.json
  3. BIN
      __pycache__/main.cpython-310.pyc
  4. BIN
      __pycache__/main.cpython-38.pyc
  5. 146 0
      main.py
  6. 1 0
      pa.txt
  7. 39 0
      requirement.txt
  8. 14 0
      service-account-credentials.json
  9. 52 0
      templates/email.html
  10. 27 0
      templates/email_v2.html

+ 0 - 0
.env


+ 30 - 0
.vscode/launch.json

@@ -0,0 +1,30 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Python: Current File",
+            "type": "python",
+            "request": "launch",
+            "program": "${file}",
+            "console": "integratedTerminal"
+        },
+        {
+            "name": "Python: FastAPI",
+            "type": "python",
+            "request": "launch",
+            "module": "uvicorn",
+            "args": [
+                "main:app",
+                "--host",
+                "0.0.0.0",
+                "--port",
+                "9998",
+                "--reload"
+            ],
+            "jinja": true
+        }
+    ]
+}

BIN
__pycache__/main.cpython-310.pyc


BIN
__pycache__/main.cpython-38.pyc


+ 146 - 0
main.py

@@ -0,0 +1,146 @@
+
+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'])

+ 1 - 0
pa.txt

@@ -0,0 +1 @@
+,W!jy&R`A3,s4Z77

+ 39 - 0
requirement.txt

@@ -0,0 +1,39 @@
+aioredis==2.0.0
+aiosmtplib==1.1.6
+anyio==3.3.4
+asgiref==3.4.1
+async-timeout==3.0.1
+blinker==1.4
+boto3==1.19.3
+botocore==1.22.3
+certifi==2021.10.8
+charset-normalizer==2.0.7
+click==8.0.3
+dnspython==2.1.0
+email-validator==1.1.3
+fakeredis==1.6.1
+fastapi==0.70.0
+fastapi-mail==0.4.1
+h11==0.12.0
+httpcore==0.13.7
+httpx==0.20.0
+idna==3.3
+Jinja2==3.0.2
+jmespath==0.10.0
+MarkupSafe==2.0.1
+packaging==21.0
+pydantic==1.8.2
+pyparsing==3.0.1
+python-dateutil==2.8.2
+python-dotenv==0.19.1
+python-multipart==0.0.5
+redis==3.5.3
+rfc3986==1.5.0
+s3transfer==0.5.0
+six==1.16.0
+sniffio==1.2.0
+sortedcontainers==2.4.0
+starlette==0.16.0
+typing-extensions==3.10.0.2
+urllib3==1.26.7
+uvicorn==0.15.0

+ 14 - 0
service-account-credentials.json

@@ -0,0 +1,14 @@
+{
+    "type": "service_account",
+    "project_id": "gorgeousspace",
+    "private_key_id": "e3fc9e50c31baa2defc75e9be443d99afdfeb5dc",
+    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQDBJuKui02AiQ56\nm4ts4tL1V6OoF/wjwqBmhKernSTDvHvISSRAN0CNp+ygsbawFTo6XBVWNWGsQ5HL\njg0+mXTDLTT8SpkOFcN7wFlfWR9myDdnN1nOY9JMLsRKksERbtmA8wL3gRdIaDTH\nfyNonVnaa8u/4LFPx6lGfr9vN3af7Aa3JieHMVcy/auIYQCH3AM4ANV9urGtH955\nuXQQ4Sh3YLGSCSphwY6lacphhvdvwLdSUHPhLJWWuROZ2MPsd3pmwDxRKYwNCcTV\nV6El3Nu7lJFq6c6UneKUpQiNp0L9IQh//Dyx95UH6fwbO5ezLd6Wt5cY2/0s777E\nGpv84+yNAgMBAAECgf8henYFmO937/+NOM8MpXJSRqm8yNlIny/bfQ0gIR48DVXL\nGrXxD4xlnKcUF4gedzkVYbGvcnnF08joOeRGO2ukI7Ol6X03gzBsvImxs2e9BoxR\nMk1Sk4E1URwdpuxiJc4G2wPEsnJXl3C309QO3zSvKQJ1PW+fmSL/sgX3Hh6EV+Vm\nN2Ybu7W6dVzMdRg/Lf3RFSCyHY8InNfVfZ3HzqoMJoEsRIMt7yaoe1rHP0XrUfah\nsqkaVoDh4Qtu62EERFEG/b2A33sTrRO0I1DW7AUt14xT9lmfflFwd2I97fNvsv1d\nyFHggBzC0v0EhieCh0zdd9JDpMm8RfgfzUry7esCgYEA+j/7dIIstkoFt7cVgILN\nhY1EQTP8mgZmlcV5vXANEPvf9mVKEflJIEPxknEZu1U3ga08C7oLbfLlp9P/Lnw+\njvKQNcpEZFQVOs8qWn019Vpo8T4WevPrk2QBF8ERTH+TyE1RkG5sRtRFn2h63Vc0\nq58/z1Rcg4T/uRkwuOcptR8CgYEAxZcKrfn2IL0mtZM7gJKN0JfJAGQL4USZ3gym\n5YT27CWGNQuABdISqoKIp11m5cjxDdtquKfGP1EK3BJ2ftVvq1tuqvUTildlYnir\n+orrjRguFQriwKDXdi1YSMzn0Wb4gEp152aiXkIsrBL3m6tbISc1tJWHohQW5m/W\nFTGD3NMCgYAnrm2ZE+szHJm7f/SQ5CziuHvSOwQbCFjL9vxO5a2su8PPtlAAeZ72\n1s8/gV+rLOAYV/flhCK71IBGFH+qR1lEIYJshR5DKnITWTZGEwSzWxq45jd6V6NL\nQ9hIg/zSPIiagmgklt3kfVRs6oxQ/LsFW8MqhR4GXNiP6Uaoiz850QKBgFoq8imw\nftwz1T1ZIfcraeH90jEGdtFm79x/442r3s6m1RbR16tQUUpUZS4TFojX6QEM1yfL\nEFIGlrEVD9QTbHFDOT50tmUUOuTz8m9UA+gQV78sh4umGo0IZlhagZNyrQZGdIWT\ntZbUFfS0dyAn019OuFhfQFT7W/Pup0BmpykXAoGAA9rzaOBHO0IGNzZ8NMAcjBn6\nkpRd2BmSuRaR/NK2LBwnkX1GDNd2KwzyvfwUWmW9QU9Sb1WtTxi8O5bl4mdw80s5\nYWq/Vrm1lFfEFrbkEuQaAZc6Jb/VhkeRLG4U4HYDhiLt2OODND05eOfpu2lEOnIw\nHLFVHn8+3YPUeyDTPJA=\n-----END PRIVATE KEY-----\n",
+    "client_email": "hhh-for-ga@gorgeousspace.iam.gserviceaccount.com",
+    "client_id": "117634037950333429541",
+    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+    "token_uri": "https://oauth2.googleapis.com/token",
+    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
+    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/hhh-for-ga%40gorgeousspace.iam.gserviceaccount.com"
+  }
+  
+  

+ 52 - 0
templates/email.html

@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<html>
+    <head>
+		<title>index</title>
+    </head>
+    <body>
+      <!-- {% for Key, Value in context.items() %}
+        <h1>Key: {{key}}</h1>
+        <h2>Value: {{value}}</h2>
+      {% endfor %} -->
+      <h1>客戶資料</h1>
+      <div>姓名:{{context.name}}</div>
+      <div>電話:{{context.phone}}</div>
+      <div>郵件:{{context.mail}}</div>
+      <div>性別:{{context.sex}}</div>
+      <h1>裝修資料</h1>
+      <table>
+        <tr>
+          <th>地區</th>
+          <th>{{context.area}}</th>
+        </tr>
+        <tr>
+          <th>房屋類型</th>
+          <th>{{context.type}}</th>
+        </tr>
+        <tr>
+          <th>房屋型態</th>
+          <th>{{context.mode}}</th>
+        </tr>
+        <tr>
+          <th>裝修預算</th>
+          <th>{{context.budget}}</th>
+        </tr>
+        <tr>
+          <th>坪數</th>
+          <th>{{context.pin}}</th>
+        </tr>
+        <tr>
+          <th>房、廳、衛</th>
+          <th>{{context.room}}房{{context.hall}}廳{{context.restroom}}衛</th>
+        </tr>
+        <tr>
+          <th>風格類型</th>
+          <th>{{context.style}}</th>
+        </tr>
+        <tr>
+          <th>預計裝修日期</th>
+          <th>{{context.time}}</th>
+        </tr>
+      </table>
+    </body>
+</html>

+ 27 - 0
templates/email_v2.html

@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+    <head>
+		<title>index</title>
+    </head>
+    <body>
+      <h1>客戶資料</h1>
+      <div>姓名:{{context.name}}</div>
+      <div>電話:{{context.phone}}</div>
+      <div>郵件:{{context.mail}}</div>
+      <h1>裝修資料</h1>
+      <table>
+        <tr>
+          <th>地區</th>
+          <th>{{context.loc}}</th>
+        </tr>
+        <tr>
+          <th>房屋類型</th>
+          <th>{{context.h_class}}</th>
+        </tr>
+        <tr>
+          <th>坪數</th>
+          <th>{{context.size}}</th>
+        </tr>
+      </table>
+    </body>
+</html>