import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class S3Email:

   def __init__(self, *args, **kwargs):
       SENDER = 'rdpublic@ansjer.com'  # 邮箱名
       SENDERNAME = 'rdpublic@ansjer.com'
       USERNAME_SMTP = "AKIA2E67UIMD6MOSFKXW"  # 带有邮件权限的 IAM 帐号
       PASSWORD_SMTP = "BHuQ6EQTtFK4qh46o9omO9ZzO3NXzjk/JCWLXnVFmqzM"  # 带有邮件权限的 IAM 密码
       PORT = 587
       HOST = "email-smtp.us-east-1.amazonaws.com"


   def faEmail(self, content ,RECIPIENT):

        SUBJECT = 'Amazon SES Error (Python smtplib)'
        BODY_TEXT = ("Amazon SES Test\r\n"
                     "This email was sent through the Amazon SES SMTP "
                     "Interface using the Python smtplib package."
                     )
        BODY_HTML = """<html>
        <head></head>
        <body>
          <h1>{}<h1>
        </body>
        </html>
                    """.format(content)

        msg = MIMEMultipart('alternative')
        msg['Subject'] = SUBJECT
        msg['From'] = email.utils.formataddr((self.SENDERNAME, self.SENDER))
        msg['To'] = RECIPIENT
        part1 = MIMEText(BODY_TEXT, 'plain')
        part2 = MIMEText(BODY_HTML, 'html')
        msg.attach(part1)
        msg.attach(part2)

        try:
            server = smtplib.SMTP(self.HOST, self.PORT)
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(self.USERNAME_SMTP, self.PASSWORD_SMTP)
            server.sendmail(self.SENDER, RECIPIENT, msg.as_string())
            server.close()
        except Exception as e:
            print("Error: ", e)
        else:
            print("Email sent!")
