sesmail.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import boto3
  2. from botocore.exceptions import ClientError
  3. import dataset
  4. db = dataset.connect(
  5. 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  6. def GetList():
  7. #本週/粉絲/編輯推薦
  8. records = db.query("""SELECT hs.start_time,hs.end_time, ifnull(ca.hdesigner_id,co.hdesigner_ids) hid, ca.hdesigner_id,co.hdesigner_ids, oss.title sec
  9. ,dd.title, dd.name, dd.mail, ifnull(ca.cover,co.clogo) img
  10. FROM homepage_set hs
  11. LEFT JOIN outer_site_set oss ON oss.oss_id = hs.outer_set
  12. left join _hcase ca ON ca.hcase_id = hs.mapping_id AND hs.theme_type='case'
  13. LEFT JOIN _hcolumn co ON hs.mapping_id = co.hcolumn_id AND hs.theme_type='column'
  14. LEFT JOIN _hdesigner dd ON dd.hdesigner_id = ifnull(ca.hdesigner_id,co.hdesigner_ids)
  15. WHERE hs.theme_type IN ('case','column')
  16. and start_time BETWEEN DATE_ADD(CURDATE(), INTERVAL-1 DAY) AND DATE_ADD(NOW(), INTERVAL-1 DAY)
  17. ORDER BY hs.ps_id
  18. """)
  19. mList = []
  20. for row in records:
  21. if str(row['hid']) != "":
  22. sMail = row['mail']
  23. sName = row['name']
  24. sCom = row['title']
  25. sSec = row['sec']
  26. sImg = row['img']
  27. sDuration = row['start_time'].strftime("%Y/%m/%d") + '-' + row['end_time'].strftime("%Y/%m/%d")
  28. mList.append([sMail,sCom,sName,sDuration,sSec,sImg])
  29. #首八大
  30. records = db.query("""SELECT hh.start_time,hh.end_time, hh.hdesigner_id hid, '首頁大' sec
  31. ,dd.title, dd.name, dd.mail, adlogo img
  32. FROM _had hh
  33. LEFT JOIN _hdesigner dd ON dd.hdesigner_id = hh.hdesigner_id
  34. WHERE adtype LIKE '首八大%'
  35. AND hh.onoff='1' AND dd.onoff = '1'
  36. and start_time BETWEEN DATE_ADD(CURDATE(), INTERVAL-1 DAY) AND DATE_ADD(NOW(), INTERVAL-1 DAY)
  37. -- AND(NOW() BETWEEN start_time AND end_time OR(start_time='0000-00-00 00:00:00' and end_time='0000-00-00 00:00:00') or (start_time is null and end_time is NULL))
  38. ORDER BY cast(SUBSTR(adtype,4) AS DECIMAL)
  39. """)
  40. for row in records:
  41. if str(row['hid']) != "":
  42. sMail = row['mail']
  43. sName = row['name']
  44. sCom = row['title']
  45. sSec = row['sec']
  46. sImg = row['img']
  47. sDuration = row['start_time'].strftime("%Y/%m/%d") + '-' + row['end_time'].strftime("%Y/%m/%d")
  48. mList.append([sMail,sCom,sName,sDuration,sSec,sImg])
  49. return mList
  50. #print(sMail,sCom,sName,sDuration,sSec)
  51. def SendMail(iRecipient:str, iCompany:str, iName:str, iDuration:str, iSection:str, iImg:str):
  52. # Replace sender@example.com with your "From" address.
  53. # This address must be verified with Amazon SES.
  54. SENDER = "Gorgeous Space - Mailer <noreply@hhh.com.tw>"
  55. # Replace recipient@example.com with a "To" address. If your account
  56. # is still in the sandbox, this address must be verified.
  57. #RECIPIENT = iRecipient
  58. RECIPIENTS = iRecipient.split(',')
  59. CCS = ["yukyo@choozmo.com","mollie@hhh.com.tw","hhh_backup@hhh.com.tw"]
  60. BCCS = []
  61. # Specify a configuration set. If you do not want to use a configuration
  62. # set, comment the following variable, and the
  63. # ConfigurationSetName=CONFIGURATION_SET argument below.
  64. #CONFIGURATION_SET = "ConfigSet"
  65. # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
  66. AWS_REGION = "us-east-1"
  67. # The subject line for the email.
  68. SUBJECT = "【幸福空間】BANNER露出:" + iCompany
  69. # The email body for recipients with non-HTML email clients.
  70. BODY_TEXT = ("\r\n" + iName + " 設計師 , 您好:\r\n\r\n您的幸福空間BANNER已露出!\r\n露出期間:"+ iDuration + "\r\n露出位置:"+ iSection+"Banner\r\n\r\n"+ iImg+"\r\n\r\n幸福空間 www.hhh.com.tw")
  71. # The HTML body of the email.
  72. BODY_HTML = """<html>
  73. <head></head>
  74. <body>
  75. <p>""" + iName + " 設計師 , 您好:<br /><br />您的幸福空間BANNER已露出!<br />露出期間:"+ iDuration + "<br />露出位置:"+ iSection+" Banner<br /><br /><img style='width:40%' src='" + iImg + """' /><br /><br />幸福空間 <a href='http://www.hhh.com.tw'>www.hhh.com.tw</a></p>
  76. </body>
  77. </html>"""
  78. # The character encoding for the email.
  79. CHARSET = "UTF-8"
  80. # Create a new SES resource and specify a region.
  81. client = boto3.client('ses'
  82. ,aws_access_key_id='AKIAQG3PJZCHXPCSTAJG'
  83. ,aws_secret_access_key='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'
  84. ,region_name=AWS_REGION)
  85. # Try to send the email.
  86. try:
  87. #Provide the contents of the email.
  88. response = client.send_email(
  89. Destination={
  90. 'ToAddresses': RECIPIENTS,
  91. 'CcAddresses': CCS,
  92. 'BccAddresses': BCCS
  93. },
  94. Message={
  95. 'Body': {
  96. 'Html': {
  97. 'Charset': CHARSET,
  98. 'Data': BODY_HTML,
  99. },
  100. 'Text': {
  101. 'Charset': CHARSET,
  102. 'Data': BODY_TEXT,
  103. },
  104. },
  105. 'Subject': {
  106. 'Charset': CHARSET,
  107. 'Data': SUBJECT,
  108. },
  109. },
  110. Source=SENDER,
  111. # If you are not using a configuration set, comment or delete the
  112. # following line
  113. #ConfigurationSetName=CONFIGURATION_SET,
  114. )
  115. # Display an error if something goes wrong.
  116. except ClientError as e:
  117. print(e.response['Error']['Message'])
  118. else:
  119. print("Email sent! Message ID:"),
  120. print(response['MessageId'])
  121. for item in GetList():
  122. #print(item)
  123. SendMail(item[0],item[1],item[2],item[3],item[4],item[5])