5252
5353from mig .shared .base import force_utf8 , generate_https_urls , extract_field , \
5454 canonical_user_with_peers , cert_field_map , get_site_base_url
55+ from mig .shared .conf import RuntimeConfiguration
5556from mig .shared .defaults import email_keyword_list , job_output_dir , \
5657 transfer_output_dir , keyword_auto , cert_auto_extend_days , \
5758 oid_auto_extend_days
@@ -561,11 +562,35 @@ def send_instant_message(
561562
562563
563564def send_email (
565+ configuration ,
566+ recipients ,
567+ subject ,
568+ message ,
569+ files = [],
570+ custom_sender = None ):
571+ """Send email to recipients via the actively configured method."""
572+
573+ assert RuntimeConfiguration .is_runtime_configuration (configuration )
574+
575+ notifier = configuration .context_get ('notifier' )
576+ if not notifier :
577+ notifier = Notifier (configuration )
578+ configuration .context_set ('notifier' , notifier )
579+
580+ return notifier .send_email (
581+ recipients ,
582+ subject ,
583+ message ,
584+ files = files ,
585+ custom_sender = custom_sender ,
586+ )
587+
588+
589+ def direct_send_email (
590+ configuration ,
564591 recipients ,
565592 subject ,
566593 message ,
567- logger ,
568- configuration ,
569594 files = [],
570595 custom_sender = None
571596):
@@ -589,13 +614,14 @@ def send_email(
589614 """
590615
591616 _logger = configuration .logger
617+
592618 gpg_sign = False
593619 if configuration .site_gpg_passphrase is not None :
594620 if gnupg is None :
595- logger .warning ("the gnupg module is required for gpg signing" )
621+ _logger .warning ("the gnupg module is required for gpg signing" )
596622 else :
597623 gpg_sign = True
598- logger .debug ("enabling automatic gpg signing of email" )
624+ _logger .debug ("enabling automatic gpg signing of email" )
599625
600626 if recipients .find (', ' ) > - 1 :
601627 recipients_list = recipients .split (', ' )
@@ -631,7 +657,7 @@ def send_email(
631657 basemsg = MIMEText (force_utf8 (message ), "plain" , "utf8" )
632658 mime_msg .attach (basemsg )
633659 if gpg_sign :
634- logger .info ("signing message with gpg" )
660+ _logger .info ("signing message with gpg" )
635661 gpg = gnupg .GPG ()
636662 basetext = basemsg .as_string ().replace ('\n ' , '\r \n ' )
637663 signature = gpg .sign (basetext , detach = True ,
@@ -643,7 +669,7 @@ def send_email(
643669 msg_sig .set_payload ("%s" % signature )
644670 mime_msg .attach (msg_sig )
645671 else :
646- logger .warning ("failed to create gnupg signature" )
672+ _logger .warning ("failed to create gnupg signature" )
647673
648674 for name in files :
649675 part = MIMEBase ('application' , "octet-stream" )
@@ -652,7 +678,7 @@ def send_email(
652678 part .add_header ('Content-Disposition' ,
653679 'attachment' , filename = os .path .basename (name ))
654680 mime_msg .attach (part )
655- logger .debug ('sending email from %s to %s:\n %s' %
681+ _logger .debug ('sending email from %s to %s:\n %s' %
656682 (from_email , recipients , mime_msg .as_string ()))
657683 server = smtplib .SMTP (configuration .smtp_server )
658684 server .set_debuglevel (0 )
@@ -664,10 +690,10 @@ def send_email(
664690 % errors )
665691 return False
666692 else :
667- logger .debug ('Email was sent to %s' % recipients )
693+ _logger .debug ('Email was sent to %s' % recipients )
668694 return True
669695 except Exception as err :
670- logger .error ('Sending email to %s through %s failed!: %s'
696+ _logger .error ('Sending email to %s through %s failed!: %s'
671697 % (recipients , configuration .smtp_server , err ))
672698 return False
673699
@@ -819,8 +845,8 @@ def notify_user(
819845
820846 continue
821847
822- if send_email (single_dest , header , message , logger ,
823- configuration , custom_sender = email_sender ):
848+ if send_email (configuration , single_dest , header , message ,
849+ custom_sender = email_sender ):
824850 logger .info ('email sent to %s telling that %s %s'
825851 % (single_dest , jobid , status ))
826852 else :
@@ -906,7 +932,7 @@ def send_resource_create_request_mail(
906932 os .path .basename (pending_file ),
907933 )
908934
909- status = send_email (recipients , subject , txt , logger , configuration )
935+ status = send_email (configuration , recipients , subject , txt )
910936 if status :
911937 msg += '\n Email was sent to admins'
912938 else :
@@ -961,3 +987,13 @@ def send_system_notification(user_id, category, message, configuration):
961987 return send_message_to_grid_notify (pickled_notification ,
962988 configuration .logger ,
963989 configuration )
990+
991+
992+ class Notifier :
993+ def __init__ (self , configuration ):
994+ self .configuration = configuration
995+
996+ def send_email (self , recipients , subject , message , files = [], custom_sender = None ):
997+ return direct_send_email (self .configuration , recipients , subject , message ,
998+ files = [],
999+ custom_sender = None )
0 commit comments