Your Name há 3 anos atrás
pai
commit
43bbabda2b
1 ficheiros alterados com 43 adições e 2 exclusões
  1. 43 2
      step_question/apis/main.py

+ 43 - 2
step_question/apis/main.py

@@ -11,6 +11,12 @@ from starlette.responses import RedirectResponse
 import codecs
 import random
 from starlette.status import HTTP_302_FOUND,HTTP_303_SEE_OTHER
+import smtplib
+import traceback
+import os
+from email.mime.text import MIMEText
+from email.mime.image import MIMEImage
+from email.mime.multipart import MIMEMultipart
 
 app = FastAPI()
 app.add_middleware(
@@ -30,14 +36,48 @@ def get_db():
 
 
 
+def mail_to_user(umail):
+    gmail_user = 'edm@choozmo.com'
+    gmail_password='wqdsyqwvppmubitv'
+    sent_from = gmail_user
+    to = [umail]
+    img_data=None
+    with open('/tmp/coupon.png', 'rb') as f:
+        img_data = f.read()
+
+    msg = MIMEMultipart()
+    msg['Subject'] = '幸福空間五萬裝修折價券'
+    msg['From'] = 'edm@choozmo.com'
+    msg['To'] = 'jeweiliang@gmail.com'
+
+    text = MIMEText("感謝您填寫問卷,敬送您五萬元裝修折價券。")
+    msg.attach(text)
+    image = MIMEImage(img_data, name=os.path.basename("c:/tmp/coupon.png"))
+    msg.attach(image)
+
+    try:
+        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
+        server.ehlo()
+        server.login(gmail_user, gmail_password)
+        server.sendmail(sent_from, to,  msg.as_string())
+        server.close()
+
+        print ('Email sent!')
+    except:
+        traceback.print_exc()
+        print ('Something went wrong...')
+
+
+
 @app.post("/step_questions/submit", response_class=RedirectResponse)
 async def submit(request: Request):
     form_data = await request.form()
 #    form_data = request.form()
+    email=form_data.get('q3')
     result = {
         'sex': form_data.get('q1'),
         'phone': form_data.get('q2'),
-        'email': form_data.get('q3'),
+        'email': email,
         'building_case_name': form_data.get('q4'),
         'building_case_type': form_data.get('q5'),
         'decoration_style': ','.join(form_data.getlist('q6')),
@@ -54,7 +94,8 @@ async def submit(request: Request):
 #    </body></html>"""
 #    return RedirectResponse(url='/a1/index_complete.html')
 #    return HTMLResponse(content=lines.format(str(random.randint(100000,999999))), status_code=200)
-    return RedirectResponse(url="/a1/index_complete.html", status_code=HTTP_302_FOUND)
+    mail_to_user(email)
+    return RedirectResponse(url="/a1/index_complete_msg.html", status_code=HTTP_302_FOUND)
 #    return result