Merge "Add support for authentication/STARTTLS to SMTP"

This commit is contained in:
Zuul 2018-10-23 15:25:37 +00:00 committed by Gerrit Code Review
commit a2addf7e64
2 changed files with 30 additions and 3 deletions

View File

@ -40,6 +40,22 @@ Connection Configuration
Who the report should be emailed to by default. Who the report should be emailed to by default.
This can be overridden by individual pipelines. This can be overridden by individual pipelines.
.. attr:: user
Optional user name used to authenticate to the SMTP server. Used only in
conjunction with a password. If no password is present, this option is
ignored.
.. attr:: password
Optional password used to authenticate to the SMTP server.
.. attr:: use_starttls
:default: false
Issue a STARTTLS request to establish an encrypted channel after having
connected to the SMTP server.
Reporter Configuration Reporter Configuration
---------------------- ----------------------

View File

@ -36,6 +36,13 @@ class SMTPConnection(BaseConnection):
'default_from', 'zuul') 'default_from', 'zuul')
self.smtp_default_to = self.connection_config.get( self.smtp_default_to = self.connection_config.get(
'default_to', 'zuul') 'default_to', 'zuul')
self.smtp_user = self.connection_config.get('user')
self.smtp_pass = self.connection_config.get('password')
starttls = self.connection_config.get('use_starttls', 'false')
if starttls.lower() == 'false':
self.smtp_starttls = False
else:
self.smtp_starttls = True
def sendMail(self, subject, message, from_email=None, to_email=None): def sendMail(self, subject, message, from_email=None, to_email=None):
# Create a text/plain email message # Create a text/plain email message
@ -50,11 +57,15 @@ class SMTPConnection(BaseConnection):
try: try:
s = smtplib.SMTP(self.smtp_server, self.smtp_port) s = smtplib.SMTP(self.smtp_server, self.smtp_port)
if self.smtp_starttls:
s.starttls()
s.ehlo()
if self.smtp_user is not None and self.smtp_pass is not None:
s.login(self.smtp_user, self.smtp_pass)
s.sendmail(from_email, to_email.split(','), msg.as_string()) s.sendmail(from_email, to_email.split(','), msg.as_string())
s.quit() s.quit()
except Exception: except Exception as e:
return "Could not send email via SMTP" self.log.warning("Error sending mail via SMTP: %s", e)
return
def getSchema(): def getSchema():