Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if config is None:
config = CONFIG
server = CONFIG.get(section, 'smtp-server')
LOG.debug('sending message to {} via {}'.format(recipient, server))
ssl = CONFIG.getboolean(section, 'smtp-ssl')
if ssl:
smtp = _smtplib.SMTP_SSL()
else:
smtp = _smtplib.SMTP()
smtp.ehlo()
try:
smtp.connect(SMTP_SERVER)
except KeyboardInterrupt:
raise
except Exception as e:
raise SMTPConnectionError(server=server) from e
if CONFIG.getboolean(section, 'smtp-auth'):
username = CONFIG.get(section, 'smtp-username')
password = CONFIG.get(section, 'smtp-password')
try:
if not ssl:
smtp.starttls()
smtp.login(username, password)
except KeyboardInterrupt:
raise
except Exception as e:
raise SMTPAuthenticationError(server=server, username=username)
smtp.send_message(message, sender, [recipient])
smtp.quit()
def __init__(self, server, message=None):
if message is None:
message = 'could not connect to mail server {}'.format(server)
super(SMTPConnectionError, self).__init__(message=message)
self.server = server
def log(self):
super(SMTPConnectionError, self).log()
LOG.warning(
'check your config file to confirm that smtp-server and other '
'mail server settings are configured properly')
if hasattr(self.__cause__, 'reason'):
LOG.error('reason: {}'.format(self.__cause__.reason))
class SMTPAuthenticationError (SMTPConnectionError):
def __init__(self, server, username):
message = (
'could not authenticate with mail server {} as user {}'.format(
server, username))
super(SMTPConnectionError, self).__init__(
server=server, message=message)
self.server = server
self.username = username
class SendmailError (RSS2EmailError):
def __init__(self, status=None, stdout=None, stderr=None):
if status:
message = 'sendmail exited with code {}'.format(status)
else:
message = ''
def log(self):
super(SMTPConnectionError, self).log()
LOG.warning(
'check your config file to confirm that smtp-server and other '
'mail server settings are configured properly')
if hasattr(self.__cause__, 'reason'):
LOG.error('reason: {}'.format(self.__cause__.reason))