Update email parameters in parameters.yaml and modify SMTP connection in survBot.py

- Update mailserver, port, user, password, and sender in parameters.yaml
- Modify SMTP connection in survBot.py to support starttls connection if server is not "localhost"
- Read password from docker secret or environment variable if set to 'DOCKER' or 'ENV' respectively
This commit is contained in:
Kasper D. Fischer 2025-03-20 15:58:51 +01:00
parent 3e47f4275b
commit 43912135e9
2 changed files with 41 additions and 3 deletions

View File

@ -137,9 +137,12 @@ html_logo: "logo.png"
# E-mail notifications
EMAIL:
mailserver: "localhost"
mailserver: "smtp.rub.de" # mail server
port: 465 # mail port
user: "DOCKER" # mail user, read from environment variable if set to "ENV" or from docker secret if set to "DOCKER"
password: "DOCKER" # mail password, read from environment variable if set to "ENV" or from docker secret if set to "DOCKER"
addresses: ["marcel.paffrath@rub.de", "kasper.fischer@rub.de"] # list of mail addresses for info mails
sender: "webmaster@geophysik.ruhr-uni-bochum.de" # mail sender
sender: "RUB SeisObs <seisobs@ruhr-uni-bochum.de>" # mail sender
stations_blacklist: [] # do not send emails for specific stations
networks_blacklist: [] # do not send emails for specific network
# specify recipients for single stations in a yaml: key = email-address, val = station list (e.g. [1Y.GR01, 1Y.GR02])

View File

@ -758,7 +758,42 @@ class StationQC(object):
msg.add_alternative(html_str, subtype='html')
# send message via SMTP server
s = smtplib.SMTP(server)
# set up starttls connection if server is not "localhost"
if server == 'localhost':
# create connection to localhost
s = smtplib.SMTP(server)
else:
user = mail_params.get('user')
# read user from docker secret if it is set to 'DOCKER' or
# read user from environment variable if it is set to 'ENV'
if user == 'DOCKER':
try:
with open('/run/secrets/mail_user', 'r') as f:
user = f.read().strip()
except FileNotFoundError as e:
logging.error('Could not read mail user from docker secret')
logging.error(e)
elif user == 'ENV':
user = os.environ.get(mail_params.get('user_env'))
password = mail_params.get('password')
# read password from docker secret if it is set to 'DOCKER' or
# read password from environment variable if it is set to 'ENV'
if password == 'DOCKER':
try:
with open('/run/secrets/mail_password', 'r') as f:
password = f.read().strip()
except FileNotFoundError as e:
logging.error('Could not read mail password from docker secret')
logging.error(e)
elif password == 'ENV':
password = os.environ.get(mail_params.get('password_env'))
# create SSL connection to server
s = smtplib.SMTP_SSL(server, mail_params.get('port'))
s.login(mail_params.get('user'), mail_params.get('password'))
# send mail and close connection
s.send_message(msg)
s.quit()