def post(self, request, command=None):
        with self._handle_exception(request):

            if (command is not None):
                if (command != 'send-test-email'):
                    e_msg = ('unknown command(%s) is not supported.' %
                             command)
                    handle_exception(Exception(e_msg), request)

                if (EmailClient.objects.count() == 0):
                    e_msg = ('E-mail account must be setup first before test '
                             'e-mail could be sent')
                    handle_exception(Exception(e_msg), request)

                eco = EmailClient.objects.all()[0]
                subject = ('Test message from Rockstor. Appliance id: %s' %
                           Appliance.objects.get(current_appliance=True).uuid)
                send_test_email(eco, subject)
                return Response()

            sender = request.data.get('sender')
            username = sender.split('@')[0]
            smtp_server = request.data.get('smtp_server')
            name = request.data.get('name')
            password = request.data.get('password')
            receiver = request.data.get('receiver')
            eco = EmailClient(smtp_server=smtp_server, name=name, sender=sender, receiver=receiver)
            eco.save()
            update_sasl(smtp_server, sender, password)
            update_forward(receiver)
            update_generic(sender)
            update_postfix(smtp_server)
            systemctl('postfix', 'restart')
            return Response(EmailClientSerializer(eco).data)
    def post(self, request, command=None):
        with self._handle_exception(request):

            if (command is not None):
                if (command != 'send-test-email'):
                    e_msg = ('unknown command(%s) is not supported.' %
                             command)
                    handle_exception(Exception(e_msg), request)

                if (EmailClient.objects.count() == 0):
                    e_msg = ('E-mail account must be setup first before test '
                             'e-mail could be sent')
                    handle_exception(Exception(e_msg), request)

                eco = EmailClient.objects.all()[0]
                subject = ('Test message from Rockstor. Appliance id: %s' %
                           Appliance.objects.get(current_appliance=True).uuid)
                send_test_email(eco, subject)
                return Response()

            sender = request.data.get('sender')
            username = sender.split('@')[0]
            smtp_server = request.data.get('smtp_server')
            name = request.data.get('name')
            password = request.data.get('password')
            receiver = request.data.get('receiver')
            eco = EmailClient(smtp_server=smtp_server, name=name, sender=sender, receiver=receiver)
            eco.save()
            update_sasl(smtp_server, sender, password)
            update_forward(receiver)
            update_postfix(smtp_server)
            systemctl('postfix', 'restart')
            return Response(EmailClientSerializer(eco).data)
    def post(self, request, command=None):
        with self._handle_exception(request):

            commands_list = ['send-test-email', 'check-smtp-auth']
            if (command is not None):
                if (command not in commands_list):
                    e_msg = ('unknown command(%s) is not supported.' % command)
                    handle_exception(Exception(e_msg), request)

                if (command == 'send-test-email'):
                    if (EmailClient.objects.count() == 0):
                        e_msg = (
                            'E-mail account must be setup first before test '
                            'e-mail could be sent')
                        handle_exception(Exception(e_msg), request)

                    eco = EmailClient.objects.all()[0]
                    subject = (
                        'Test message from Rockstor. Appliance id: %s' %
                        Appliance.objects.get(current_appliance=True).uuid)
                    send_test_email(eco, subject)
                    return Response()

                elif (command == 'check-smtp-auth'):
                    mail_auth = {}
                    sender = request.data.get('sender')
                    username = request.data.get('username')
                    mail_auth[
                        'username'] = sender if not username else username
                    mail_auth['password'] = request.data.get('password')
                    mail_auth['smtp_server'] = request.data.get('smtp_server')
                    mail_auth['port'] = int(request.data.get('port', 587))

                    return Response(json.dumps(
                        {'smtp_auth': test_smtp_auth(mail_auth)}),
                                    content_type="application/json")

            sender = request.data.get('sender')
            username = request.data.get(
                'username')  #collect new username field
            username = sender if not username else username  #smtp auth - use username or if empty use sender
            smtp_server = request.data.get('smtp_server')
            port = int(request.data.get('port', 587))
            name = request.data.get('name')
            password = request.data.get('password')
            receiver = request.data.get('receiver')
            eco = EmailClient(smtp_server=smtp_server,
                              port=port,
                              name=name,
                              sender=sender,
                              receiver=receiver,
                              username=username)
            eco.save()
            update_sasl(smtp_server, port, username, password)
            update_forward(receiver)
            update_generic(sender)
            update_postfix(smtp_server, port)
            systemctl('postfix', 'restart')
            return Response(EmailClientSerializer(eco).data)
    def post(self, request, command=None):
        with self._handle_exception(request):

            commands_list = ['send-test-email', 'check-smtp-auth']
            if (command is not None):
                if (command not in commands_list):
                    e_msg = ('unknown command(%s) is not supported.' %
                             command)
                    handle_exception(Exception(e_msg), request)

                if (command == 'send-test-email'):
                    if (EmailClient.objects.count() == 0):
                        e_msg = ('E-mail account must be setup first before '
                                 'test e-mail could be sent')
                        handle_exception(Exception(e_msg), request)

                    eco = EmailClient.objects.all()[0]
                    subject = ('Test message from Rockstor. Appliance id: %s' %
                               Appliance.objects.get(current_appliance=True).uuid)  # noqa E501
                    send_test_email(eco, subject)
                    return Response()

                elif (command == 'check-smtp-auth'):
                    mail_auth = {}
                    sender = request.data.get('sender')
                    username = request.data.get('username')
                    mail_auth['username'] = sender if not username else username  # noqa E501
                    mail_auth['password'] = request.data.get('password')
                    mail_auth['smtp_server'] = request.data.get('smtp_server')
                    mail_auth['port'] = int(request.data.get('port', 587))

                    return Response(
                        json.dumps({'smtp_auth': test_smtp_auth(mail_auth)}),
                        content_type="application/json")

            sender = request.data.get('sender')
            # collect new username field
            username = request.data.get('username')
            # smtp auth - use username or if empty use sender
            username = sender if not username else username
            smtp_server = request.data.get('smtp_server')
            port = int(request.data.get('port', 587))
            name = request.data.get('name')
            password = request.data.get('password')
            receiver = request.data.get('receiver')
            eco = EmailClient(smtp_server=smtp_server, port=port, name=name,
                              sender=sender, receiver=receiver,
                              username=username)
            eco.save()
            update_sasl(smtp_server, port, username, password)
            update_forward(receiver)
            update_generic(sender)
            update_postfix(smtp_server, port)
            systemctl('postfix', 'restart')
            return Response(EmailClientSerializer(eco).data)
Exemple #5
0
 def post(self, request):
     with self._handle_exception(request):
         sender = request.data.get('sender')
         username = sender.split('@')[0]
         smtp_server = request.data.get('smtp_server')
         name = request.data.get('name')
         password = request.data.get('password')
         receiver = request.data.get('receiver')
         eco = EmailClient(smtp_server=smtp_server, name=name, sender=sender, receiver=receiver)
         eco.save()
         update_sasl(smtp_server, sender, password)
         update_forward(receiver)
         update_postfix(smtp_server)
         systemctl('postfix', 'restart')
         return Response(EmailClientSerializer(eco).data)
Exemple #6
0
    def post(self, request, command=None):
        with self._handle_exception(request):

            commands_list = ["send-test-email", "check-smtp-auth"]
            if command is not None:
                if command not in commands_list:
                    e_msg = ("Unknown command ({}) is not supported.").format(command)
                    handle_exception(Exception(e_msg), request)

                if command == "send-test-email":
                    if EmailClient.objects.count() == 0:
                        e_msg = (
                            "E-mail account must be setup before a "
                            "test e-mail can be sent."
                        )
                        handle_exception(Exception(e_msg), request)

                    eco = EmailClient.objects.all()[0]
                    subject = ("Test message from Rockstor. Appliance id: {}").format(
                        Appliance.objects.get(current_appliance=True).uuid
                    )  # noqa E501
                    send_test_email(eco, subject)
                    return Response()

                elif command == "check-smtp-auth":
                    mail_auth = {}
                    sender = request.data.get("sender")
                    username = request.data.get("username")
                    mail_auth["username"] = (
                        sender if not username else username
                    )  # noqa E501
                    mail_auth["password"] = request.data.get("password")
                    mail_auth["smtp_server"] = request.data.get("smtp_server")
                    mail_auth["port"] = int(request.data.get("port", 587))

                    return Response(
                        json.dumps({"smtp_auth": test_smtp_auth(mail_auth)}),
                        content_type="application/json",
                    )

            sender = request.data.get("sender")
            # collect new username field
            username = request.data.get("username")
            # smtp auth - use username or if empty use sender
            username = sender if not username else username
            smtp_server = request.data.get("smtp_server")
            port = int(request.data.get("port", 587))
            name = request.data.get("name")
            password = request.data.get("password")
            receiver = request.data.get("receiver")
            eco = EmailClient(
                smtp_server=smtp_server,
                port=port,
                name=name,
                sender=sender,
                receiver=receiver,
                username=username,
            )
            eco.save()
            update_sasl(smtp_server, port, username, password)
            update_forward(receiver)
            update_generic(sender)
            update_postfix(smtp_server, port)
            disable_sysconfig_mail()
            update_master()
            # Restart ensures sevice is running, even if not running previously.
            systemctl("postfix", "enable")
            systemctl("postfix", "restart")
            return Response(EmailClientSerializer(eco).data)