def _sendErrorMail( self, errorMail ):
    """ Send the reports by email

        args:
            errorMail : Error to report
    """

    mail = Mail()
    mail._subject = "Error popularity report %s" % self.startDate
    mail._message = "Error Popularity report %s: %s" % ( self.startDate, errorMail )
    mail._mailAddress = self.mailRecipients
    if self.mailSender:
      mail._fromAddress = self.mailSender
    mail._send()
  def _sendReport( self, listOfFiles ):
    """ Send the reports by email

        args:
            listOfFiles (list): list of files to be sent
    """

    mail = Mail()
    mail._subject = "Popularity report %s" % self.startDate
    mail._message = " Popularity report %s" % self.startDate
    mail._attachments = listOfFiles
    mail._mailAddress = self.mailRecipients
    if self.mailSender:
      mail._fromAddress = self.mailSender
    mail._send()
  def sendMail( self, address, subject, body, fromAddress = None, localAttempt = True, html = False ):
    """ Send an e-mail with subject and body to the specified address. Try to send
        from local area before central service by default.
    """
    self.log.verbose( 'Received signal to send the following mail to %s:\nSubject = %s\n%s' % ( address, subject, body ) )
    result = S_ERROR()
    if localAttempt:
      try:
        m = Mail()
        m._subject = subject
        m._message = body
        m._mailAddress = address
        m._html = html
        if fromAddress:
          m._fromAddress = fromAddress
        result = m._send()
      except Exception as x:
        self.log.warn( 'Sending mail failed with exception:\n%s' % ( str( x ) ) )

      if result['OK']:
        self.log.verbose( 'Mail sent successfully from local host to %s with subject %s' % ( address, subject ) )
        self.log.debug( result['Value'] )
        return result

      self.log.warn( 'Could not send mail with the following message:\n%s\n will attempt to send via NotificationService' % result['Message'] )

    notify = self.__getRPCClient( timeout = 120 )
    result = notify.sendMail( address, subject, body, str( fromAddress ) )
    if not result['OK']:
      self.log.error( 'Could not send mail via central Notification service', result['Message'] )
    else:
      self.log.verbose( result['Value'] )

    return result
Beispiel #4
0
    def export_sendSMS(self, userName, body, fromAddress):
        """ Send an SMS with supplied body to the specified DIRAC user using the Mail utility via an SMS switch.
    """
        gLogger.verbose(
            'Received signal to send the following SMS to %s:\n%s' %
            (userName, body))
        mobile = gConfig.getValue('/Registry/Users/%s/Mobile' % userName, '')
        if not mobile:
            return S_ERROR('No registered mobile number for %s' % userName)

        csSection = PathFinder.getServiceSection('Framework/Notification')
        smsSwitch = gConfig.getValue('%s/SMSSwitch' % csSection, '')
        if not smsSwitch:
            return S_ERROR('No SMS switch is defined in CS path %s/SMSSwitch' %
                           csSection)

        address = '%s@%s' % (mobile, smsSwitch)
        subject = 'DIRAC SMS'
        m = Mail()
        m._subject = subject
        m._message = body
        m._mailAddress = address
        if not fromAddress == 'None':
            m._fromAddress = fromAddress
        result = m._send()
        if not result['OK']:
            gLogger.warn(
                'Could not send SMS to %s with the following message:\n%s' %
                (userName, result['Message']))
        else:
            gLogger.info('SMS sent successfully to %s ' % (userName))
            gLogger.debug(result['Value'])

        return result
Beispiel #5
0
  def export_sendMail( self, address, subject, body, fromAddress, avoidSpam = False ):
    """ Send an email with supplied body to the specified address using the Mail utility.

        if avoidSpam is True, then emails are first added to a set so that duplicates are removed,
        and sent every hour.
    """
    gLogger.verbose( 'Received signal to send the following mail to %s:\nSubject = %s\n%s' % ( address, subject, body ) )
    eMail = Mail()
    notificationSection = PathFinder.getServiceSection( "Framework/Notification" )
    csSection = notificationSection + '/SMTP'
    eMail._smtpHost = gConfig.getValue( '%s/Host' % csSection )
    eMail._smtpPort = gConfig.getValue( '%s/Port' % csSection )
    eMail._smtpLogin = gConfig.getValue( '%s/Login' % csSection )
    eMail._smtpPasswd = gConfig.getValue( '%s/Password' % csSection )
    eMail._smtpPtcl = gConfig.getValue( '%s/Protocol' % csSection )
    eMail._subject = subject
    eMail._message = body
    eMail._mailAddress = address
    if not fromAddress == 'None':
      eMail._fromAddress = fromAddress
    if gConfig.getValue( '%s/FromAddress' % csSection ):
      eMail._fromAddress = gConfig.getValue( '%s/FromAddress' % csSection )
    if avoidSpam:
      gMailSet.add(eMail)
      return S_OK("Mail added to gMailSet")
    else:
      result = eMail._send()
      if not result['OK']:
        gLogger.warn( 'Could not send mail with the following message:\n%s' % result['Message'] )
      else:
        gLogger.info( 'Mail sent successfully to %s with subject %s' % ( address, subject ) )
        gLogger.debug( result['Value'] )

    return result
Beispiel #6
0
  def export_sendMail( self, address, subject, body, fromAddress, avoidSpam = False ):
    """ Send an email with supplied body to the specified address using the Mail utility.

        if avoidSpam is True, then emails are first added to a set so that duplicates are removed,
        and sent every hour.
    """
    gLogger.verbose( 'Received signal to send the following mail to %s:\nSubject = %s\n%s' % ( address, subject, body ) )
    eMail = Mail()
    notificationSection = PathFinder.getServiceSection( "Framework/Notification" )
    csSection = notificationSection + '/SMTPServer'
    eMail._smtpHost = gConfig.getValue( '%s/Host' % csSection )
    eMail._smtpPort = gConfig.getValue( '%s/Port' % csSection )
    eMail._smtpLogin = gConfig.getValue( '%s/Login' % csSection )
    eMail._smtpPasswd = gConfig.getValue( '%s/Password' % csSection )
    eMail._smtpPtcl = gConfig.getValue( '%s/Protocol' % csSection )
    eMail._subject = subject
    eMail._message = body
    eMail._mailAddress = address
    if not fromAddress == 'None':
      eMail._fromAddress = fromAddress
    if gConfig.getValue( '%s/FromAddress' % csSection ):
      eMail._fromAddress = gConfig.getValue( '%s/FromAddress' % csSection )
    if avoidSpam:
      gMailSet.add(eMail)
      return S_OK("Mail added to gMailSet")
    else:
      result = eMail._send()
      if not result['OK']:
        gLogger.warn( 'Could not send mail with the following message:\n%s' % result['Message'] )
      else:
        gLogger.info( 'Mail sent successfully to %s with subject %s' % ( address, subject ) )
        gLogger.debug( result['Value'] )

    return result
Beispiel #7
0
  def export_sendSMS( self, userName, body, fromAddress ):
    """ Send an SMS with supplied body to the specified DIRAC user using the Mail utility via an SMS switch.
    """
    gLogger.verbose( 'Received signal to send the following SMS to %s:\n%s' % ( userName, body ) )
    mobile = gConfig.getValue( '/Registry/Users/%s/Mobile' % userName, '' )
    if not mobile:
      return S_ERROR( 'No registered mobile number for %s' % userName )

    csSection = PathFinder.getServiceSection( 'Framework/Notification' )
    smsSwitch = gConfig.getValue( '%s/SMSSwitch' % csSection, '' )
    if not smsSwitch:
      return S_ERROR( 'No SMS switch is defined in CS path %s/SMSSwitch' % csSection )

    address = '%s@%s' % ( mobile, smsSwitch )
    subject = 'DIRAC SMS'
    m = Mail()
    m._subject = subject
    m._message = body
    m._mailAddress = address
    if not fromAddress == 'None':
      m._fromAddress = fromAddress
    result = m._send()
    if not result['OK']:
      gLogger.warn( 'Could not send SMS to %s with the following message:\n%s' % ( userName, result['Message'] ) )
    else:
      gLogger.info( 'SMS sent successfully to %s ' % ( userName ) )
      gLogger.debug( result['Value'] )

    return result
Beispiel #8
0
    def export_sendMail(self,
                        address,
                        subject,
                        body,
                        fromAddress,
                        avoidSpam=False):
        """ Send an email with supplied body to the specified address using the Mail utility.

        if avoidSpam is True, then emails are first added to a set so that duplicates are removed,
        and sent every hour.
    """
        gLogger.verbose(
            'Received signal to send the following mail to %s:\nSubject = %s\n%s'
            % (address, subject, body))
        eMail = Mail()
        eMail._subject = subject
        eMail._message = body
        eMail._mailAddress = address
        if not fromAddress == 'None':
            eMail._fromAddress = fromAddress
        if avoidSpam:
            gMailSet.add(eMail)
            return S_OK("Mail added to gMailSet")
        else:
            result = eMail._send()
            if not result['OK']:
                gLogger.warn(
                    'Could not send mail with the following message:\n%s' %
                    result['Message'])
            else:
                gLogger.info('Mail sent successfully to %s with subject %s' %
                             (address, subject))
                gLogger.debug(result['Value'])

        return result
Beispiel #9
0
    def sendMail(self,
                 addresses,
                 subject,
                 body,
                 fromAddress=None,
                 localAttempt=True,
                 html=False):
        """Send an e-mail with subject and body to the specified address. Try to send
        from local area before central service by default.
        """
        self.log.verbose(
            "Received signal to send the following mail to %s:\nSubject = %s\n%s"
            % (addresses, subject, body))
        result = S_ERROR()

        if not fromAddress:
            fromAddress = ""

        addresses = [addresses] if isinstance(addresses,
                                              str) else list(addresses)
        for address in addresses:

            if localAttempt:
                try:
                    m = Mail()
                    m._subject = subject
                    m._message = body
                    m._mailAddress = address
                    m._html = html
                    if fromAddress:
                        m._fromAddress = fromAddress
                    result = m._send()
                except Exception as x:
                    self.log.warn("Sending mail failed with exception:\n%s" %
                                  (str(x)))

                if result["OK"]:
                    self.log.verbose(
                        "Mail sent successfully from local host to %s with subject %s"
                        % (address, subject))
                    self.log.debug(result["Value"])
                    return result

                self.log.warn(
                    "Could not send mail with the following message:\n%s\n will attempt to send via NotificationService"
                    % result["Message"])

            result = self._getRPC().sendMail(address, subject, body,
                                             fromAddress)
            if not result["OK"]:
                self.log.error(
                    "Could not send mail via central Notification service",
                    result["Message"])
                return result
            else:
                self.log.verbose(result["Value"])

        return result
Beispiel #10
0
    def export_sendMail(self,
                        address,
                        subject,
                        body,
                        fromAddress,
                        avoidSpam=False):
        """ Send an email with supplied body to the specified address using the Mail utility.

        :param basestring address: recipient addresses
        :param basestring subject: subject of letter
        :param basestring body: body of letter
        :param basestring fromAddress: sender address, if None, will be used default from CS
        :param bool avoidSpam: if True, then emails are first added to a set so that duplicates are removed,
               and sent every hour.

        :return: S_OK(basestring)/S_ERROR() -- basestring is status message
    """
        gLogger.verbose(
            'Received signal to send the following mail to %s:\nSubject = %s\n%s'
            % (address, subject, body))
        if gMailCache.exists(hash(address + subject + body)) and not avoidSpam:
            return S_OK(
                'Email with the same content already sent today to current addresses, come back tomorrow'
            )
        eMail = Mail()
        notificationSection = PathFinder.getServiceSection(
            "Framework/Notification")
        csSection = notificationSection + '/SMTP'
        eMail._smtpHost = gConfig.getValue('%s/Host' % csSection)
        eMail._smtpPort = gConfig.getValue('%s/Port' % csSection)
        eMail._smtpLogin = gConfig.getValue('%s/Login' % csSection)
        eMail._smtpPasswd = gConfig.getValue('%s/Password' % csSection)
        eMail._smtpPtcl = gConfig.getValue('%s/Protocol' % csSection)
        eMail._subject = subject
        eMail._message = body
        eMail._mailAddress = address
        if not fromAddress == 'None':
            eMail._fromAddress = fromAddress
        eMail._fromAddress = gConfig.getValue(
            '%s/FromAddress' % csSection) or eMail._fromAddress
        if avoidSpam and eMail in gMailSet:
            return S_OK("Mail already sent")
        else:
            result = eMail._send()
            if not result['OK']:
                gLogger.warn(
                    'Could not send mail with the following message:\n%s' %
                    result['Message'])
            else:
                gMailCache.add(hash(address + subject + body), 3600 * 24)
                gLogger.info('Mail sent successfully to %s with subject %s' %
                             (address, subject))
                gLogger.debug(result['Value'])
                if avoidSpam:
                    gMailSet.add(eMail)

        return result
Beispiel #11
0
    def export_sendMail(self,
                        address,
                        subject,
                        body,
                        fromAddress,
                        avoidSpam=False):
        """ Send an email with supplied body to the specified address using the Mail utility.

        :param six.string_types address: recipient addresses
        :param six.string_types subject: subject of letter
        :param six.string_types body: body of letter
        :param six.string_types fromAddress: sender address, if None, will be used default from CS
        :param bool avoidSpam: Deprecated

        :return: S_OK(six.string_types)/S_ERROR() -- six.string_types is status message
    """
        self.log.verbose(
            'Received signal to send the following mail to %s:\nSubject = %s\n%s'
            % (address, subject, body))
        if self.mailCache.exists(hash(address + subject + body)):
            return S_OK(
                'Email with the same content already sent today to current addresses, come back tomorrow'
            )
        eMail = Mail()
        notificationSection = PathFinder.getServiceSection(
            "Framework/Notification")
        csSection = notificationSection + '/SMTP'
        eMail._smtpHost = gConfig.getValue('%s/Host' % csSection)
        eMail._smtpPort = gConfig.getValue('%s/Port' % csSection)
        eMail._smtpLogin = gConfig.getValue('%s/Login' % csSection)
        eMail._smtpPasswd = gConfig.getValue('%s/Password' % csSection)
        eMail._smtpPtcl = gConfig.getValue('%s/Protocol' % csSection)
        eMail._subject = subject
        eMail._message = body
        eMail._mailAddress = address
        if not fromAddress == 'None':
            eMail._fromAddress = fromAddress
        eMail._fromAddress = gConfig.getValue(
            '%s/FromAddress' % csSection) or eMail._fromAddress
        result = eMail._send()
        if not result['OK']:
            self.log.warn(
                'Could not send mail with the following message:\n%s' %
                result['Message'])
        else:
            self.mailCache.add(hash(address + subject + body), 3600 * 24)
            self.log.info('Mail sent successfully to %s with subject %s' %
                          (address, subject))
            self.log.debug(result['Value'])

        return result
Beispiel #12
0
    def sendMail(self,
                 address,
                 subject,
                 body,
                 fromAddress=None,
                 localAttempt=True,
                 html=False):
        """ Send an e-mail with subject and body to the specified address. Try to send
        from local area before central service by default.
    """
        self.log.verbose(
            'Received signal to send the following mail to %s:\nSubject = %s\n%s'
            % (address, subject, body))
        result = S_ERROR()
        if localAttempt:
            try:
                m = Mail()
                m._subject = subject
                m._message = body
                m._mailAddress = address
                m._html = html
                if fromAddress:
                    m._fromAddress = fromAddress
                result = m._send()
            except Exception as x:
                self.log.warn('Sending mail failed with exception:\n%s' %
                              (str(x)))

            if result['OK']:
                self.log.verbose(
                    'Mail sent successfully from local host to %s with subject %s'
                    % (address, subject))
                self.log.debug(result['Value'])
                return result

            self.log.warn(
                'Could not send mail with the following message:\n%s\n will attempt to send via NotificationService'
                % result['Message'])

        notify = self.__getRPCClient(timeout=120)
        result = notify.sendMail(address, subject, body, str(fromAddress))
        if not result['OK']:
            self.log.error(
                'Could not send mail via central Notification service',
                result['Message'])
        else:
            self.log.verbose(result['Value'])

        return result
Beispiel #13
0
  def __sendMailToUser( self, user, subject, message ):
    address = gConfig.getValue( "/Registry/Users/%s/Email" % user, "" )
    if not address:
      self.log.error( "User does not have an email registered", user )
      return S_ERROR( "User %s does not have an email registered" % user )
    self.log.info( "Sending mail (%s) to user %s at %s" % ( subject, user, address ) )
    m = Mail()
    m._subject = "[DIRAC] %s" % subject
    m._message = message
    m._mailAddress = address
    result = m._send()
    if not result['OK']:
      gLogger.warn( 'Could not send mail with the following message:\n%s' % result['Message'] )

    return result
Beispiel #14
0
    def export_sendMail(self, address, subject, body, fromAddress):
        """Send an email with supplied body to the specified address using the Mail utility.

        :param str address: recipient addresses
        :param str subject: subject of letter
        :param str body: body of letter
        :param str fromAddress: sender address, if "", will be used default from CS

        :return: S_OK(str)/S_ERROR() -- str is status message
        """
        self.log.verbose(
            "Received signal to send the following mail to %s:\nSubject = %s\n%s"
            % (address, subject, body))
        if self.mailCache.exists(hash(address + subject + body)):
            return S_OK(
                "Email with the same content already sent today to current addresses, come back tomorrow"
            )
        eMail = Mail()
        notificationSection = PathFinder.getServiceSection(
            "Framework/Notification")
        csSection = notificationSection + "/SMTP"
        eMail._smtpHost = gConfig.getValue("%s/Host" % csSection)
        eMail._smtpPort = gConfig.getValue("%s/Port" % csSection)
        eMail._smtpLogin = gConfig.getValue("%s/Login" % csSection)
        eMail._smtpPasswd = gConfig.getValue("%s/Password" % csSection)
        eMail._smtpPtcl = gConfig.getValue("%s/Protocol" % csSection)
        eMail._subject = subject
        eMail._message = body
        eMail._mailAddress = address
        if fromAddress:
            eMail._fromAddress = fromAddress
        eMail._fromAddress = gConfig.getValue(
            "%s/FromAddress" % csSection) or eMail._fromAddress
        result = eMail._send()
        if not result["OK"]:
            self.log.warn(
                "Could not send mail with the following message:\n%s" %
                result["Message"])
        else:
            self.mailCache.add(hash(address + subject + body), 3600 * 24)
            self.log.info("Mail sent successfully to %s with subject %s" %
                          (address, subject))
            self.log.debug(result["Value"])

        return result
Beispiel #15
0
  def export_sendMail( self, address, subject, body, fromAddress ):
    """ Send an email with supplied body to the specified address using the Mail utility.
    """
    gLogger.verbose( 'Received signal to send the following mail to %s:\nSubject = %s\n%s' % ( address, subject, body ) )
    m = Mail()
    m._subject = subject
    m._message = body
    m._mailAddress = address
    if not fromAddress == 'None':
      m._fromAddress = fromAddress
    result = m._send()
    if not result['OK']:
      gLogger.warn( 'Could not send mail with the following message:\n%s' % result['Message'] )
    else:
      gLogger.info( 'Mail sent successfully to %s with subject %s' % ( address, subject ) )
      gLogger.debug( result['Value'] )

    return result
Beispiel #16
0
    def export_sendSMS(self, userName, body, fromAddress):
        """Send an SMS with supplied body to the specified DIRAC user using the Mail utility via an SMS switch.

        :param str userName: user name
        :param str body: message
        :param str fromAddress: sender address

        :return: S_OK()/S_ERROR()
        """
        self.log.verbose(
            "Received signal to send the following SMS to %s:\n%s" %
            (userName, body))
        mobile = gConfig.getValue("/Registry/Users/%s/Mobile" % userName, "")
        if not mobile:
            return S_ERROR("No registered mobile number for %s" % userName)

        csSection = PathFinder.getServiceSection("Framework/Notification")
        smsSwitch = gConfig.getValue("%s/SMSSwitch" % csSection, "")
        if not smsSwitch:
            return S_ERROR("No SMS switch is defined in CS path %s/SMSSwitch" %
                           csSection)

        address = "%s@%s" % (mobile, smsSwitch)
        subject = "DIRAC SMS"
        eMail = Mail()
        eMail._subject = subject
        eMail._message = body
        eMail._mailAddress = address
        if fromAddress:
            eMail._fromAddress = fromAddress
        result = eMail._send()
        if not result["OK"]:
            self.log.warn(
                "Could not send SMS to %s with the following message:\n%s" %
                (userName, result["Message"]))
        else:
            self.log.info("SMS sent successfully to %s " % (userName))
            self.log.debug(result["Value"])

        return result
Beispiel #17
0
    def export_sendMail(self, address, subject, body, fromAddress):
        """ Send an email with supplied body to the specified address using the Mail utility.
    """
        gLogger.verbose(
            'Received signal to send the following mail to %s:\nSubject = %s\n%s'
            % (address, subject, body))
        m = Mail()
        m._subject = subject
        m._message = body
        m._mailAddress = address
        if not fromAddress == 'None':
            m._fromAddress = fromAddress
        result = m._send()
        if not result['OK']:
            gLogger.warn(
                'Could not send mail with the following message:\n%s' %
                result['Message'])
        else:
            gLogger.info('Mail sent successfully to %s with subject %s' %
                         (address, subject))
            gLogger.debug(result['Value'])

        return result