diff --git a/keep/providers/smtp_provider/smtp_provider.py b/keep/providers/smtp_provider/smtp_provider.py index c0606349a..f8f187c09 100644 --- a/keep/providers/smtp_provider/smtp_provider.py +++ b/keep/providers/smtp_provider/smtp_provider.py @@ -4,11 +4,11 @@ import dataclasses import typing - -import pydantic -from smtplib import SMTP, SMTP_SSL from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from smtplib import SMTP, SMTP_SSL + +import pydantic from keep.contextmanager.contextmanager import ContextManager from keep.providers.base.base_provider import BaseProvider @@ -17,23 +17,6 @@ @pydantic.dataclasses.dataclass class SmtpProviderAuthConfig: - smtp_username: str = dataclasses.field( - metadata={ - "required": True, - "description": "SMTP username", - "config_main_group": "authentication", - } - ) - - smtp_password: str = dataclasses.field( - metadata={ - "required": True, - "sensitive": True, - "description": "SMTP password", - "config_main_group": "authentication", - } - ) - smtp_server: str = dataclasses.field( metadata={ "required": True, @@ -47,7 +30,8 @@ class SmtpProviderAuthConfig: "required": True, "description": "SMTP port", "config_main_group": "authentication", - } + }, + default=587, ) encryption: typing.Literal["SSL", "TLS"] = dataclasses.field( @@ -61,6 +45,25 @@ class SmtpProviderAuthConfig: }, ) + smtp_username: str = dataclasses.field( + metadata={ + "required": False, + "description": "SMTP username", + "config_main_group": "authentication", + }, + default="", + ) + + smtp_password: str = dataclasses.field( + metadata={ + "required": False, + "sensitive": True, + "description": "SMTP password", + "config_main_group": "authentication", + }, + default="", + ) + class SmtpProvider(BaseProvider): PROVIDER_SCOPES = [ @@ -75,18 +78,18 @@ class SmtpProvider(BaseProvider): PROVIDER_DISPLAY_NAME = "SMTP" def __init__( - self, context_manager: ContextManager, provider_id: str, config: ProviderConfig + self, context_manager: ContextManager, provider_id: str, config: ProviderConfig ): super().__init__(context_manager, provider_id, config) def dispose(self): pass - + def validate_config(self): self.authentication_config = SmtpProviderAuthConfig( **self.config.authentication ) - + def validate_scopes(self): """ Validate that the scopes provided are correct. @@ -97,7 +100,7 @@ def validate_scopes(self): return {"send_email": True} except Exception as e: return {"send_email": str(e)} - + def generate_smtp_client(self): """ Generate an SMTP client. @@ -108,18 +111,20 @@ def generate_smtp_client(self): smtp_port = self.authentication_config.smtp_port encryption = self.authentication_config.encryption - if (encryption == "SSL"): + if encryption == "SSL": smtp = SMTP_SSL(smtp_server, smtp_port) - smtp.login(smtp_username, smtp_password) - return smtp - - elif (encryption == "TLS"): + elif encryption == "TLS": smtp = SMTP(smtp_server, smtp_port) smtp.starttls() + + if smtp_username and smtp_password: smtp.login(smtp_username, smtp_password) - return smtp - - def send_email(self, from_email: str, from_name: str, to_email: str, subject: str, body: str): + + return smtp + + def send_email( + self, from_email: str, from_name: str, to_email: str, subject: str, body: str + ): """ Send an email using SMTP protocol. """ @@ -127,9 +132,9 @@ def send_email(self, from_email: str, from_name: str, to_email: str, subject: st if from_name == "": msg["From"] = from_email msg["From"] = f"{from_name} <{from_email}>" - msg['To'] = to_email - msg['Subject'] = subject - msg.attach(MIMEText(body, 'plain')) + msg["To"] = to_email + msg["Subject"] = subject + msg.attach(MIMEText(body, "plain")) try: smtp = self.generate_smtp_client() @@ -138,18 +143,16 @@ def send_email(self, from_email: str, from_name: str, to_email: str, subject: st except Exception as e: raise Exception(f"Failed to send email: {str(e)}") - - def _notify(self, from_email: str, from_name: str, to_email: str, subject: str, body: str): + + def _notify( + self, from_email: str, from_name: str, to_email: str, subject: str, body: str + ): """ Send an email using SMTP protocol. """ self.send_email(from_email, from_name, to_email, subject, body) - return { - "from": from_email, - "to": to_email, - "subject": subject, - "body": body - } + return {"from": from_email, "to": to_email, "subject": subject, "body": body} + if __name__ == "__main__": import logging diff --git a/pyproject.toml b/pyproject.toml index 758f57d9e..0fea838e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "keep" -version = "0.27.7" +version = "0.27.8" description = "Alerting. for developers, by developers." authors = ["Keep Alerting LTD"] readme = "README.md"