def initialize_client(self): """Initialize the connection to Mailgun.""" from pymailgunner import Client self._client = Client(self._api_key, self._domain, self._sandbox) _LOGGER.debug("Mailgun domain: %s", self._client.domain) self._domain = self._client.domain if not self._sender: self._sender = DEFAULT_SENDER.format(domain=self._domain)
def initialize_client(self): """Initialize the connection to Mailgun.""" self._client = Client(self._api_key, self._domain, self._sandbox) _LOGGER.debug("Mailgun domain: %s", self._client.domain) self._domain = self._client.domain if not self._sender: self._sender = f"hass@{self._domain}"
class MailgunNotificationService(BaseNotificationService): """Implement a notification service for the Mailgun mail service.""" def __init__(self, domain, sandbox, api_key, sender, recipient): """Initialize the service.""" self._client = None # Mailgun API client self._domain = domain self._sandbox = sandbox self._api_key = api_key self._sender = sender self._recipient = recipient def initialize_client(self): """Initialize the connection to Mailgun.""" from pymailgunner import Client self._client = Client(self._api_key, self._domain, self._sandbox) _LOGGER.debug("Mailgun domain: %s", self._client.domain) self._domain = self._client.domain if not self._sender: self._sender = DEFAULT_SENDER.format(domain=self._domain) def connection_is_valid(self): """Check whether the provided credentials are valid.""" from pymailgunner import MailgunCredentialsError, MailgunDomainError try: self.initialize_client() except MailgunCredentialsError: _LOGGER.exception("Invalid credentials") return False except MailgunDomainError as mailgun_error: _LOGGER.exception(mailgun_error) return False return True def send_message(self, message="", **kwargs): """Send a mail to the recipient.""" from pymailgunner import MailgunError subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) data = kwargs.get(ATTR_DATA) files = data.get(ATTR_IMAGES) if data else None try: # Initialize the client in case it was not. if self._client is None: self.initialize_client() resp = self._client.send_mail( sender=self._sender, to=self._recipient, subject=subject, text=message, files=files, ) _LOGGER.debug("Message sent: %s", resp) except MailgunError as mailgun_error: _LOGGER.exception("Failed to send message: %s", mailgun_error)
class MailgunNotificationService(BaseNotificationService): """Implement a notification service for the Mailgun mail service.""" def __init__(self, domain, sandbox, api_key, sender, recipient): """Initialize the service.""" self._client = None # Mailgun API client self._domain = domain self._sandbox = sandbox self._api_key = api_key self._sender = sender self._recipient = recipient def initialize_client(self): """Initialize the connection to Mailgun.""" from pymailgunner import Client self._client = Client(self._api_key, self._domain, self._sandbox) _LOGGER.debug("Mailgun domain: %s", self._client.domain) self._domain = self._client.domain if not self._sender: self._sender = DEFAULT_SENDER.format(domain=self._domain) def connection_is_valid(self): """Check whether the provided credentials are valid.""" from pymailgunner import (MailgunCredentialsError, MailgunDomainError) try: self.initialize_client() except MailgunCredentialsError: _LOGGER.exception("Invalid credentials") return False except MailgunDomainError as mailgun_error: _LOGGER.exception(mailgun_error) return False return True def send_message(self, message="", **kwargs): """Send a mail to the recipient.""" from pymailgunner import MailgunError subject = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) data = kwargs.get(ATTR_DATA) files = data.get(ATTR_IMAGES) if data else None try: # Initialize the client in case it was not. if self._client is None: self.initialize_client() resp = self._client.send_mail( sender=self._sender, to=self._recipient, subject=subject, text=message, files=files) _LOGGER.debug("Message sent: %s", resp) except MailgunError as mailgun_error: _LOGGER.exception("Failed to send message: %s", mailgun_error)