Пример #1
0
    def test_config(cls, params=None):
        mailserver = getParam(params, "email.mailserver", optional=False)
        subject = "Your TEST OTP"
        message = "This is a test."
        mail_from = getParam(params, "email.mailfrom", optional=False)
        recipient = getParam(params, "email.recipient", optional=False)
        password = getParam(params, "email.password")
        username = getParam(params, "email.username")
        port = getParam(params, "email.port", default=25)
        email_tls = getParam(params, "email.tls", default=False)
        r = send_email_data(mailserver,
                            subject,
                            message,
                            mail_from,
                            recipient,
                            username=username,
                            password=password,
                            port=port,
                            email_tls=email_tls)

        description = "Could not send email."
        if r:
            description = TEST_SUCCESSFUL

        return r, description
Пример #2
0
    def _compose_email(self,
                       message="<otp>",
                       subject="Your OTP",
                       mimetype="plain"):
        """
        send email

        :param message: the email submit message - could contain placeholders
            like <otp> or <serial>
        :type message: string
        :param mimetype: the message MIME type - one of "plain", "html"
        :type mimetype: basestring

        :return: submitted message
        :rtype: string
        """
        ret = None

        recipient = self._email_address
        otp = self.get_otp()[2]
        serial = self.get_serial()

        message = message.replace("<otp>", otp)
        message = message.replace("<serial>", serial)

        message = message.format(otp=otp, serial=serial)

        subject = subject.replace("<otp>", otp)
        subject = subject.replace("<serial>", serial)

        subject = subject.format(otp=otp, serial=serial)

        log.debug("sending Email to {0!r}".format(recipient))

        identifier = get_from_config("email.identifier")
        if identifier:
            # New way to send email
            ret = send_email_identifier(identifier,
                                        recipient,
                                        subject,
                                        message,
                                        mimetype=mimetype)
        else:
            # old way to send email / DEPRECATED
            mailserver = get_from_config("email.mailserver", "localhost")
            port = int(get_from_config("email.port", 25))
            username = get_from_config("email.username")
            password = get_from_config("email.password")
            mail_from = get_from_config("email.mailfrom",
                                        "privacyidea@localhost")
            email_tls = get_from_config("email.tls",
                                        default=False,
                                        return_bool=True)
            ret = send_email_data(mailserver, subject, message, mail_from,
                                  recipient, username, password, port,
                                  email_tls)
        return ret, message
Пример #3
0
    def _compose_email(self, message="<otp>", subject="Your OTP", mimetype="plain"):
        """
        send email

        :param message: the email submit message - could contain placeholders
            like <otp> or <serial>
        :type message: string
        :param mimetype: the message MIME type - one of "plain", "html"
        :type mimetype: basestring

        :return: submitted message
        :rtype: string
        """
        ret = None

        recipient = self._email_address
        otp = self.get_otp()[2]
        serial = self.get_serial()

        message = message.replace("<otp>", otp)
        message = message.replace("<serial>", serial)

        tags = create_tag_dict(serial=serial,
                               tokenowner=self.user,
                               tokentype=self.get_tokentype(),
                               recipient={"givenname": self.user.info.get("givenname"),
                                          "surname": self.user.info.get("surname")},
                               escape_html=mimetype.lower() == "html")

        message = message.format(otp=otp, **tags)

        subject = subject.replace("<otp>", otp)
        subject = subject.replace("<serial>", serial)

        subject = subject.format(otp=otp, **tags)

        log.debug("sending Email to {0!r}".format(recipient))

        # The token specific identifier has priority over the system wide identifier
        identifier = self.get_tokeninfo("email.identifier") or get_from_config("email.identifier")
        if identifier:
            # New way to send email
            ret = send_email_identifier(identifier, recipient, subject, message,
                                        mimetype=mimetype)
        else:
            # old way to send email / DEPRECATED
            mailserver = get_from_config("email.mailserver", "localhost")
            port = int(get_from_config("email.port", 25))
            username = get_from_config("email.username")
            password = get_from_config("email.password")
            mail_from = get_from_config("email.mailfrom", "privacyidea@localhost")
            email_tls = get_from_config("email.tls", default=False,
                                        return_bool=True)
            ret = send_email_data(mailserver, subject, message, mail_from,
                                  recipient, username, password, port,
                                  email_tls)
        return ret, message
Пример #4
0
    def submit_message(self, phone, message):
        """
        Submits the message for phone to the email gateway.

        Returns true in case of success

        In case of a failure an exception is raised
        """
        if self.smsgateway:
            identifier = self.smsgateway.option_dict.get("SMTPIDENTIFIER")
            recipient = self.smsgateway.option_dict.get("MAILTO").format(
                otp=message, phone=phone)
            subject = self.smsgateway.option_dict.get("SUBJECT",
                                                      "{phone}").format(
                otp=message, phone=phone)
            body = self.smsgateway.option_dict.get("BODY", "{otp}").format(
                otp=message, phone=phone)
        else:
            identifier = self.config.get("IDENTIFIER")
            server = self.config.get("MAILSERVER")
            sender = self.config.get("MAILSENDER")
            recipient = self.config.get("MAILTO")
            subject = self.config.get("SUBJECT", PHONE_TAG)
            body = self.config.get("BODY", MSG_TAG)

            if not (server and recipient and sender) and not (identifier and \
                    recipient):
                log.error("incomplete config: %s. MAILTO and (IDENTIFIER or "
                          "MAILSERVER and MAILSENDER) needed" % self.config)
                raise SMSError(-1, "Incomplete SMS config.")

            log.debug("submitting message {0!r} to {1!s}".format(body, phone))
            recipient = string.replace(recipient, PHONE_TAG, phone)
            subject = string.replace(subject, PHONE_TAG, phone)
            subject = string.replace(subject, MSG_TAG, message)
            body = string.replace(body, PHONE_TAG, phone)
            body = string.replace(body, MSG_TAG, message)

        if identifier:
            r = send_email_identifier(identifier, recipient, subject, body)
        else:
            username = self.config.get("MAILUSER")
            password = self.config.get("MAILPASSWORD")
            r = send_email_data(server, subject, body, sender, recipient,
                                username, password)
        if not r:
            raise SMSError(500, "Failed to deliver SMS to SMTP Gateway.")

        return True
Пример #5
0
    def submit_message(self, phone, message):
        """
        Submits the message for phone to the email gateway.

        Returns true in case of success

        In case of a failure an exception is raised
        """
        if self.smsgateway:
            identifier = self.smsgateway.option_dict.get("SMTPIDENTIFIER")
            recipient = self.smsgateway.option_dict.get("MAILTO").format(
                otp=message, phone=phone)
            subject = self.smsgateway.option_dict.get(
                "SUBJECT", "{phone}").format(otp=message, phone=phone)
            body = self.smsgateway.option_dict.get("BODY",
                                                   "{otp}").format(otp=message,
                                                                   phone=phone)
        else:
            identifier = self.config.get("IDENTIFIER")
            server = self.config.get("MAILSERVER")
            sender = self.config.get("MAILSENDER")
            recipient = self.config.get("MAILTO")
            subject = self.config.get("SUBJECT", PHONE_TAG)
            body = self.config.get("BODY", MSG_TAG)

            if not (server and recipient and sender) and not (identifier and \
                    recipient):
                log.error("incomplete config: %s. MAILTO and (IDENTIFIER or "
                          "MAILSERVER and MAILSENDER) needed" % self.config)
                raise SMSError(-1, "Incomplete SMS config.")

            log.debug("submitting message {0!r} to {1!s}".format(body, phone))
            recipient = string.replace(recipient, PHONE_TAG, phone)
            subject = string.replace(subject, PHONE_TAG, phone)
            subject = string.replace(subject, MSG_TAG, message)
            body = string.replace(body, PHONE_TAG, phone)
            body = string.replace(body, MSG_TAG, message)

        if identifier:
            r = send_email_identifier(identifier, recipient, subject, body)
        else:
            username = self.config.get("MAILUSER")
            password = self.config.get("MAILPASSWORD")
            r = send_email_data(server, subject, body, sender, recipient,
                                username, password)
        if not r:
            raise SMSError(500, "Failed to deliver SMS to SMTP Gateway.")

        return True
Пример #6
0
    def _compose_email(self, message="<otp>", subject="Your OTP", mimetype="plain"):
        """
        send email

        :param message: the email submit message - could contain placeholders
            like <otp> or <serial>
        :type message: string
        :param mimetype: the message MIME type - one of "plain", "html"
        :type mimetype: basestring

        :return: submitted message
        :rtype: string
        """
        ret = None

        recipient = self._email_address
        otp = self.get_otp()[2]
        serial = self.get_serial()

        message = message.replace("<otp>", otp)
        message = message.replace("<serial>", serial)

        message = message.format(otp=otp, serial=serial)

        subject = subject.replace("<otp>", otp)
        subject = subject.replace("<serial>", serial)

        subject = subject.format(otp=otp, serial=serial)

        log.debug("sending Email to {0!r}".format(recipient))

        identifier = get_from_config("email.identifier")
        if identifier:
            # New way to send email
            ret = send_email_identifier(identifier, recipient, subject, message,
                                        mimetype=mimetype)
        else:
            # old way to send email / DEPRECATED
            mailserver = get_from_config("email.mailserver", "localhost")
            port = int(get_from_config("email.port", 25))
            username = get_from_config("email.username")
            password = get_from_config("email.password")
            mail_from = get_from_config("email.mailfrom", "privacyidea@localhost")
            email_tls = get_from_config("email.tls", default=False,
                                        return_bool=True)
            ret = send_email_data(mailserver, subject, message, mail_from,
                                  recipient, username, password, port,
                                  email_tls)
        return ret, message
Пример #7
0
    def test_config(cls, params=None):
        mailserver = getParam(params, "email.mailserver", optional=False)
        subject = "Your TEST OTP"
        message = "This is a test."
        mail_from = getParam(params, "email.mailfrom", optional=False)
        recipient = getParam(params, "email.recipient", optional=False)
        password = getParam(params, "email.password")
        username = getParam(params, "email.username")
        port = getParam(params, "email.port", default=25)
        email_tls = getParam(params, "email.tls", default=False)
        r = send_email_data(mailserver, subject, message, mail_from,
                            recipient, username=username,
                            password=password, port=port, email_tls=email_tls)

        description = "Could not send email."
        if r:
            description = TEST_SUCCESSFUL

        return r, description