
import traceback

from boto3.session import Session


# 发送邮件邮箱
SES_COMPANY_EMAIL = 'user_server@nsst.com'
AWS_SES_ACCESS_ID = 'AKIAJKPU23EU5QWHFPKQ'
AWS_SES_ACCESS_SECRET = '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'
AWS_SES_ACCESS_REGION = 'us-east-1'
AWS_BUCKET = 'ansjertest'

class SesClassObject:
   def __init__(self, *args, **kwargs):
        self.access_id = AWS_SES_ACCESS_ID
        self.access_secret = AWS_SES_ACCESS_SECRET
        self.region_name = AWS_SES_ACCESS_REGION
        self.company_email = SES_COMPANY_EMAIL
        session = Session(
            aws_access_key_id=AWS_SES_ACCESS_ID,
            aws_secret_access_key=AWS_SES_ACCESS_SECRET,
            region_name=AWS_SES_ACCESS_REGION,
        )
        self.conn = session.client('ses')


   def send_email(self, send_address_list=[], subject='', body=''):
        try:
            response = self.conn.send_email(
                # 发送人
                Source=self.company_email,
                Destination={
                    # 收件人
                    'ToAddresses': send_address_list
                    # 'ToAddresses': [
                    #     '1758730877@qq.com',
                    # ],
                },
                Message={
                    # 标题
                    'Subject': {
                        'Data': subject,
                        'Charset': 'utf-8'
                    },
                    'Body': {
                        'Html': {
                            'Data': body,
                            'Charset': 'utf-8'
                        }
                    }
                },
            )
        except Exception as e:
            errorInfo = traceback.format_exc()
            print(errorInfo)
            self.send_email(send_address_list=[self.company_email], subject='邮件发送错误信息提醒', body='<br>请向开发人员反馈并修改</br>' +'\n错误信息位'+str(errorInfo))
        else:
            if response['MessageId']:
                return True
            else:
                print(response)
                self.send_email(send_address_list=[self.company_email], subject='邮件发送错误信息提醒',
                                body='<br>请向开发人员反馈并修改</br>')
                return False
#
# sesObj = SesClassObject()
# sesObj.send_email(send_address_list=['1758730877@qq.com'],subject='hi test',body='hello')