is_close_changed_notice.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from enum import Enum
  2. import time
  3. from datetime import datetime
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from fastapi import FastAPI, Query, UploadFile, File
  7. from fastapi import FastAPI
  8. from fastapi.middleware.cors import CORSMiddleware
  9. import json
  10. import urllib.request
  11. import dataset,json
  12. import dataset
  13. from datetime import datetime, timedelta
  14. from email.mime.text import MIMEText
  15. from email.mime.image import MIMEImage
  16. from email.mime.multipart import MIMEMultipart
  17. from email.mime.base import MIMEBase
  18. from email.mime.application import MIMEApplication
  19. import os
  20. import boto3
  21. from botocore.exceptions import ClientError
  22. app = FastAPI()
  23. origins = [
  24. "http://139.162.121.30",
  25. "http://139.162.121.30:8002",
  26. ]
  27. app.add_middleware(
  28. CORSMiddleware,
  29. # allow_origins=origins,
  30. allow_origins=["*"],
  31. allow_credentials=True,
  32. allow_methods=["*"],
  33. allow_headers=["*"],
  34. )
  35. # 寄送逾期執行表單Email通知
  36. def hhh_send_mail(email, subject, html):
  37. SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
  38. RECIPIENT = email
  39. AWS_REGION = "us-east-1"
  40. CHARSET = "UTF-8"
  41. client = boto3.client('ses',region_name=AWS_REGION)
  42. try:
  43. msg = MIMEMultipart()
  44. # msg["Subject"] = "逾期執行表單通知_" + str(datetime.date.today())
  45. msg["Subject"] = subject
  46. msg["From"] = "noreply@hhh.com.tw"
  47. msg["To"] = email
  48. # Set message body
  49. body = MIMEText(html, "html")
  50. msg.attach(body)
  51. response = client.send_raw_email(
  52. Source=msg["From"],
  53. Destinations=[msg["To"]],
  54. RawMessage={"Data": msg.as_string()}
  55. )
  56. print(response)
  57. # Display an error if something goes wrong.
  58. except ClientError as e:
  59. print(e.response['Error']['Message'])
  60. else:
  61. print("Email sent! Message ID:"),
  62. print(response['MessageId'])
  63. # Choozmo DB
  64. # db = dataset.connect("mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4")
  65. # 測試機 DB
  66. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4')
  67. # 正式機 DB
  68. # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  69. @app.get("/is_close_changed_notice")
  70. async def is_close_changed_notice(exf_id: str = '', is_close: str = ''):
  71. subject = ""
  72. # 取得合約資料
  73. q = "SELECT * \
  74. FROM execute_form \
  75. WHERE exf_id = " + exf_id
  76. q_result_count = len(list(db.query(q)))
  77. if q_result_count == 0: # 無合約資料
  78. return
  79. for r in db.query(q):
  80. subject = '[合約狀態修改]:' + r['company'] + ' (合約編號:' + r['num'] + ')_' + datetime.now().strftime("%Y-%m-%d %H:%M")
  81. html = """
  82. <!DOCTYPE html>
  83. <head>
  84. <meta charset="UTF-8">
  85. </head>
  86. <body>
  87. """
  88. if is_close == 'D':
  89. html += '<div>本合約狀態已修改為 <span style="color:red;">續約</span></div>';
  90. elif is_close == 'E':
  91. html += '<div>本合約狀態已修改為 <span style="color:red;">不續約延期</span></div>';
  92. elif is_close == 'F':
  93. html += '<div>本合約狀態已修改為 <span style="color:red;">不續約需下線</span></div>';
  94. elif is_close == 'G':
  95. html += '<div>本合約狀態已修改為 <span style="color:red;">已到期未續約</span></div>';
  96. html += "<div><br>幸福空間經營團隊敬上<br><br>※此信為系統自動寄送,請勿直接回信。謝謝!</div>"
  97. html += """
  98. </body>
  99. </html>
  100. """
  101. # Email清單
  102. email_list = ['mike@choozmo.com']
  103. # 寄送逾期執行表單Email通知
  104. for email in email_list:
  105. hhh_send_mail(email, subject, html)