Add the ability to override the sender header of email via SMTP (#1657)

* Add the ability to override the sender header of email via SMTP with the `MAILSENDER_ADDR` config value
* Closes #1644
This commit is contained in:
Kevin Chung
2020-09-20 23:54:19 -04:00
committed by GitHub
parent 1be3659996
commit 4cd4d0cb92
3 changed files with 19 additions and 1 deletions

View File

@@ -84,6 +84,14 @@ MAIL_TLS =
# Whether to connect to the SMTP server over SSL # Whether to connect to the SMTP server over SSL
MAIL_SSL = MAIL_SSL =
# MAILSENDER_ADDR
# The email address that is responsible for the transmission of emails.
# This is very often the MAILFROM_ADDR value but can be specified if your email
# is delivered by a different domain than what's specified in your MAILFROM_ADDR.
# If this isn't specified, the MAILFROM_ADDR value is used.
# It is fairly rare to need to set this value.
MAILSENDER_ADDR =
# MAILGUN_API_KEY # MAILGUN_API_KEY
# Mailgun API key to send email over Mailgun. As of CTFd v3, Mailgun integration is deprecated. # Mailgun API key to send email over Mailgun. As of CTFd v3, Mailgun integration is deprecated.
# Installations using the Mailgun API should migrate over to SMTP settings. # Installations using the Mailgun API should migrate over to SMTP settings.

View File

@@ -147,6 +147,8 @@ class ServerConfig(object):
MAIL_SSL: bool = process_boolean_str(config_ini["email"]["MAIL_SSL"]) MAIL_SSL: bool = process_boolean_str(config_ini["email"]["MAIL_SSL"])
MAILSENDER_ADDR: str = empty_str_cast(config_ini["email"]["MAILSENDER_ADDR"])
MAILGUN_API_KEY: str = empty_str_cast(config_ini["email"]["MAILGUN_API_KEY"]) MAILGUN_API_KEY: str = empty_str_cast(config_ini["email"]["MAILGUN_API_KEY"])
MAILGUN_BASE_URL: str = empty_str_cast(config_ini["email"]["MAILGUN_API_KEY"]) MAILGUN_BASE_URL: str = empty_str_cast(config_ini["email"]["MAILGUN_API_KEY"])

View File

@@ -56,7 +56,15 @@ def sendmail(addr, text, subject):
msg["From"] = mailfrom_addr msg["From"] = mailfrom_addr
msg["To"] = addr msg["To"] = addr
# Check whether we are using an admin-defined SMTP server
custom_smtp = bool(get_config("mail_server"))
# We should only consider the MAILSENDER_ADDR value on servers defined in config
if custom_smtp:
smtp.send_message(msg) smtp.send_message(msg)
else:
mailsender_addr = get_app_config("MAILSENDER_ADDR")
smtp.send_message(msg, from_addr=mailsender_addr)
smtp.quit() smtp.quit()
return True, "Email sent" return True, "Email sent"