<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from builtins import str <EOL> from past . builtins import basestring <EOL> import importlib <EOL> import logging <EOL> import os <EOL> import smtplib <EOL> from email . mime . text import MIMEText <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . application import MIMEApplication <EOL> from email . utils import formatdate <EOL> from airflow import configuration <EOL> def send_email ( to , subject , html_content , files = None , dryrun = False ) : <EOL> """<STR_LIT>""" <EOL> path , attr = configuration . get ( '<STR_LIT:email>' , '<STR_LIT>' ) . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = importlib . import_module ( path ) <EOL> backend = getattr ( module , attr ) <EOL> return backend ( to , subject , html_content , files = files , dryrun = dryrun ) <EOL> def send_email_smtp ( to , subject , html_content , files = None , dryrun = False ) : <EOL> """<STR_LIT>""" <EOL> SMTP_MAIL_FROM = configuration . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if isinstance ( to , basestring ) : <EOL> if '<STR_LIT:U+002C>' in to : <EOL> to = to . split ( '<STR_LIT:U+002C>' ) <EOL> elif '<STR_LIT:;>' in to : <EOL> to = to . split ( '<STR_LIT:;>' ) <EOL> else : <EOL> to = [ to ] <EOL> msg = MIMEMultipart ( '<STR_LIT>' ) <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msg [ '<STR_LIT>' ] = SMTP_MAIL_FROM <EOL> msg [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( to ) <EOL> msg [ '<STR_LIT>' ] = formatdate ( localtime = True ) <EOL> mime_text = MIMEText ( html_content , '<STR_LIT:html>' ) <EOL> msg . attach ( mime_text ) <EOL> for fname in files or [ ] : <EOL> basename = os . path . basename ( fname ) <EOL> with open ( fname , "<STR_LIT:rb>" ) as f : <EOL> msg . attach ( MIMEApplication ( <EOL> f . read ( ) , <EOL> Content_Disposition = '<STR_LIT>' % basename , <EOL> Name = basename <EOL> ) ) <EOL> send_MIME_email ( SMTP_MAIL_FROM , to , msg , dryrun ) <EOL> def send_MIME_email ( e_from , e_to , mime_msg , dryrun = False ) : <EOL> SMTP_HOST = configuration . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMTP_PORT = configuration . getint ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMTP_USER = configuration . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMTP_PASSWORD = configuration . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMTP_STARTTLS = configuration . getboolean ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SMTP_SSL = configuration . getboolean ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not dryrun : <EOL> s = smtplib . SMTP_SSL ( SMTP_HOST , SMTP_PORT ) if SMTP_SSL else smtplib . SMTP ( SMTP_HOST , SMTP_PORT ) <EOL> if SMTP_STARTTLS : <EOL> s . starttls ( ) <EOL> if SMTP_USER and SMTP_PASSWORD : <EOL> s . login ( SMTP_USER , SMTP_PASSWORD ) <EOL> logging . info ( "<STR_LIT>" + str ( e_to ) ) <EOL> s . sendmail ( e_from , e_to , mime_msg . as_string ( ) ) <EOL> s . quit ( ) </s>
