diff --git a/setup.py b/setup.py index ae8eb6a94..d3ce43d71 100644 --- a/setup.py +++ b/setup.py @@ -59,6 +59,7 @@ "werkzeug>=2,<3; python_version < '3.8'", "werkzeug>=3,<4; python_version >= '3.8'", "watchdog>=2,<=4", + "phonenumbers>=8.12,<9", ], # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index f2ac770fa..42bf617ac 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -40,7 +40,11 @@ def send(self, notifiable, notification): sms = self.build(notifiable, notification) client = self.get_sms_client() recipients = sms._to + if not isinstance(recipients, list): + recipients = [recipients] for recipient in recipients: + if not self.is_valid_phone_number(recipient): + raise NotificationException(f"Invalid phone number: {recipient}") payload = sms.to(recipient).build().get_options() response = client.send_message(payload) self._handle_errors(response) @@ -67,3 +71,11 @@ def _handle_errors(self, response): status, message["error-text"] ) ) + + def is_valid_phone_number(self, phone_number): + import phonenumbers + try: + parsed_number = phonenumbers.parse(phone_number, None) + return phonenumbers.is_valid_number(parsed_number) + except phonenumbers.NumberParseException: + return False diff --git a/tests/integrations/app/notifications/TestNotification.py b/tests/integrations/app/notifications/TestNotification.py new file mode 100644 index 000000000..373e713b9 --- /dev/null +++ b/tests/integrations/app/notifications/TestNotification.py @@ -0,0 +1,11 @@ +from src.masonite.notification import Notification +from src.masonite.mail import Mailable +from src.masonite.notification import Sms + +class TestNotification(Notification, Mailable): + + def to_vonage(self, notifiable): + return Sms().text("Test message" ) + + def via(self, notifiable): + return ["vonage"] \ No newline at end of file