text: # -*- coding: utf-8 -*-
  #
  # Licensed to the Apache Software Foundation (ASF) under one
  # or more contributor license agreements.  See the NOTICE file
  # distributed with this work for additional information
  # regarding copyright ownership.  The ASF licenses this file
  # to you under the Apache License, Version 2.0 (the
  # "License"); you may not use this file except in compliance
  # with the License.  You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing,
  # software distributed under the License is distributed on an
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  # KIND, either express or implied.  See the License for the
  # specific language governing permissions and limitations
  # under the License.
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  from builtins import str
  from past.builtins import basestring
  
  import importlib
  import os
  import smtplib
  
  from email.mime.text import MIMEText
  from email.mime.multipart import MIMEMultipart
  from email.mime.application import MIMEApplication
  from email.utils import formatdate
  
  from airflow import configuration
  from xTool.utils.log.logging_mixin import LoggingMixin
  from xTool.exceptions import XToolConfigException
  
  
  def send_email(to, subject, html_content,
                 files=None, dryrun=False, cc=None, bcc=None,
                 mime_subtype='mixed', mime_charset='utf-8', **kwargs):
      """
      Send email using backend specified in EMAIL_BACKEND.
  
      :param dryrun: true表示测试邮件配置，不会发送邮件
      """
      # 动态加载模块发送函数
      # 可以通过插件的方式自定义邮件发送函数
      # e.g. email_backend = airflow.utils.email.send_email_smtp
      path, attr = configuration.conf.get('email', 'EMAIL_BACKEND').rsplit('.', 1)
      module = importlib.import_module(path)
      backend = getattr(module, attr)
      to = get_email_address_list(to)
      to = ", ".join(to)
  
      return backend(to, subject, html_content, files=files,
                     dryrun=dryrun, cc=cc, bcc=bcc,
                     mime_subtype=mime_subtype, mime_charset=mime_charset, **kwargs)
  
  
  def send_email_smtp(to, subject, html_content, files=None,
                      dryrun=False, cc=None, bcc=None,
                      mime_subtype='mixed', mime_charset='utf-8',
                      **kwargs):
      """
      Send an email with html content
  
      >>> send_email('test@example.com', 'foo', '<b>Foo</b> bar', ['/dev/null'], dryrun=True)
      """
      # 发件人
      smtp_mail_from = configuration.conf.get('smtp', 'SMTP_MAIL_FROM')
  
      # 收件人格式化
      to = get_email_address_list(to)
  
      # 构建混合邮件体
      msg = MIMEMultipart(mime_subtype)
      msg['Subject'] = subject
      msg['From'] = smtp_mail_from
      msg['To'] = ", ".join(to)
      recipients = to
      if cc:
          cc = get_email_address_list(cc)
          msg['CC'] = ", ".join(cc)
          recipients = recipients + cc
  
      if bcc:
          # don't add bcc in header
          bcc = get_email_address_list(bcc)
          recipients = recipients + bcc
  
      # 添加邮件内容
      msg['Date'] = formatdate(localtime=True)
      mime_text = MIMEText(html_content, 'html', mime_charset)
      msg.attach(mime_text)
  
      # 添加附件
      for fname in files or []:
          # 注意：获得文件名，文件路径在调用的当前路径下
          basename = os.path.basename(fname)
          with open(fname, "rb") as f:
              part = MIMEApplication(
                  f.read(),
                  Name=basename
              )
              part['Content-Disposition'] = 'attachment; filename="%s"' % basename
              part['Content-ID'] = '<%s>' % basename
              msg.attach(part)
  
      # 发送邮件
      send_MIME_email(smtp_mail_from, recipients, msg, dryrun)
  
  
  def send_MIME_email(e_from, e_to, mime_msg, dryrun=False):
      """发送邮件 ."""
      log = LoggingMixin().log
  
      SMTP_HOST = configuration.conf.get('smtp', 'SMTP_HOST')
      SMTP_PORT = configuration.conf.getint('smtp', 'SMTP_PORT')
      SMTP_STARTTLS = configuration.conf.getboolean('smtp', 'SMTP_STARTTLS')
      SMTP_SSL = configuration.conf.getboolean('smtp', 'SMTP_SSL')
      SMTP_USER = None
      SMTP_PASSWORD = None
  
      try:
          SMTP_USER = configuration.conf.get('smtp', 'SMTP_USER')
          SMTP_PASSWORD = configuration.conf.get('smtp', 'SMTP_PASSWORD')
      except XToolConfigException:
          log.debug("No user/password found for SMTP, so logging in with no authentication.")
  
      if not dryrun:
          s = smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT) if SMTP_SSL else smtplib.SMTP(SMTP_HOST, SMTP_PORT)
          if SMTP_STARTTLS:
              s.starttls()
          if SMTP_USER and SMTP_PASSWORD:
              s.login(SMTP_USER, SMTP_PASSWORD)
          log.info("Sent an alert email to %s", e_to)
          s.sendmail(e_from, e_to, mime_msg.as_string())
          s.quit()
  
  
  def get_email_address_list(address_string):
      """多个收件人用逗号或分号分隔，并转化为数组 ."""
      if isinstance(address_string, basestring):
          if ',' in address_string:
              address_string = [address.strip() for address in address_string.split(',')]
          elif ';' in address_string:
              address_string = [address.strip() for address in address_string.split(';')]
          else:
              address_string = [address_string]
  
      return address_string
  