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

class S3Email:


   def faEmail(self, sys_msg_text ,username):
        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'
        SUBJECT = sys_msg_text
        BODY_TEXT = (sys_msg_text
                     )
        BODY_HTML = """<html>
        <head></head>
        <body>
          <h1>{}<h1>
        </body>
        </html>
                    """.format(sys_msg_text)

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

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