SesClassObject.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from Ansjer.config import AWS_SES_ACCESS_ID,AWS_SES_ACCESS_SECRET,AWS_SES_ACCESS_REGION,SES_COMPANY_EMAIL
  2. from boto3.session import Session
  3. import traceback
  4. class SesClassObject:
  5. def __init__(self, *args, **kwargs):
  6. self.access_id = AWS_SES_ACCESS_ID
  7. self.access_secret = AWS_SES_ACCESS_SECRET
  8. self.region_name = AWS_SES_ACCESS_REGION
  9. self.company_email = SES_COMPANY_EMAIL
  10. session = Session(
  11. aws_access_key_id=AWS_SES_ACCESS_ID,
  12. aws_secret_access_key=AWS_SES_ACCESS_SECRET,
  13. region_name=AWS_SES_ACCESS_REGION,
  14. )
  15. self.conn = session.client('ses')
  16. def send_email(self, send_address_list=[], subject='', body=''):
  17. import os
  18. os.fork()
  19. try:
  20. response = self.conn.send_email(
  21. # 发送人
  22. Source=self.company_email,
  23. Destination={
  24. # 收件人
  25. 'ToAddresses': send_address_list
  26. # 'ToAddresses': [
  27. # '1758730877@qq.com',
  28. # ],
  29. },
  30. Message={
  31. # 标题
  32. 'Subject': {
  33. 'Data': subject,
  34. 'Charset': 'utf-8'
  35. },
  36. 'Body': {
  37. 'Html': {
  38. 'Data': body,
  39. 'Charset': 'utf-8'
  40. }
  41. }
  42. },
  43. )
  44. except Exception as e:
  45. errorInfo = traceback.format_exc()
  46. print(errorInfo)
  47. self.send_email(send_address_list=[self.company_email], subject='邮件发送错误信息提醒', body='<br>请向开发人员反馈并修改</br>' +'\n错误信息位'+str(errorInfo))
  48. if response['MessageId']:
  49. return True
  50. else:
  51. print(response)
  52. self.send_email(send_address_list=[self.company_email],subject='邮件发送错误信息提醒',body='<br>请向开发人员反馈并修改</br>')
  53. return False