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
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
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
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
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 _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 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
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
def test_createEmail(): m = Mail() res = m._create("*****@*****.**") assert not res["OK"] m._subject = "subject" m._fromAddress = "*****@*****.**" m._mailAddress = "*****@*****.**" m._message = "This is a message" res = m._create("*****@*****.**") assert res["OK"] assert res["Value"].__dict__["_headers"] == [ ("Content-Type", "multipart/mixed"), ("MIME-Version", "1.0"), ("Subject", "subject"), ("From", "*****@*****.**"), ("To", "*****@*****.**"), ]
def test_createEmail(self): """ test _create """ m = Mail() res = m._create('*****@*****.**') self.assertFalse(res['OK']) m._subject = 'subject' m._fromAddress = '*****@*****.**' m._mailAddress = '*****@*****.**' m._message = 'This is a message' res = m._create('*****@*****.**') self.assertTrue(res['OK']) self.assertEqual(res['Value'].__dict__['_headers'], [('Content-Type', 'multipart/mixed'), ('MIME-Version', '1.0'), ('Subject', 'subject'), ('From', '*****@*****.**'), ('To', '*****@*****.**')])
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
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
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
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
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
def test_compareEmails(self): """ test comparing of Email objects (also for insertion in sets) """ m1 = Mail() m2 = Mail() self.assertEqual(m1, m2) m1 = Mail() m1._subject = 'subject' m1._fromAddress = '*****@*****.**' m1._mailAddress = '*****@*****.**' m1._message = 'This is a message' m2 = Mail() m2._subject = 'subject' m2._fromAddress = '*****@*****.**' m2._mailAddress = '*****@*****.**' m2._message = 'This is a message' self.assertEqual(m1, m2) m3 = Mail() m3._subject = 'subject' m3._fromAddress = '*****@*****.**' m3._mailAddress = '*****@*****.**' m3._message = 'This is a message a bit different' self.assertNotEqual(m1, m3) s = set() s.add(m1) s.add(m2) self.assertTrue(len(s) == 1) s.add(m2) self.assertTrue(len(s) == 1) s.add(m3) self.assertTrue(len(s) == 2) s.add(m3) self.assertTrue(len(s) == 2)
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
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
def test_compareEmails(monkeypatch): # The hostname on GitHub actions can change randomly so mock it monkeypatch.setattr("socket.getfqdn", lambda: "localhost.example") m1 = Mail() m2 = Mail() assert m1 == m2, (m1.__dict__, m2.__dict__) m1 = Mail() m1._subject = "subject" m1._fromAddress = "*****@*****.**" m1._mailAddress = "*****@*****.**" m1._message = "This is a message" m2 = Mail() m2._subject = "subject" m2._fromAddress = "*****@*****.**" m2._mailAddress = "*****@*****.**" m2._message = "This is a message" assert m1 == m2 m3 = Mail() m3._subject = "subject" m3._fromAddress = "*****@*****.**" m3._mailAddress = "*****@*****.**" m3._message = "This is a message a bit different" assert m1 != m3 s = {m1, m2} assert len(s) == 1 s.add(m2) assert len(s) == 1 s.add(m3) assert len(s) == 2 s.add(m3) assert len(s) == 2
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
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