Set "from" email address using settings table instead of hard-coding#273
Merged
tom93 merged 7 commits intoNZOI:masterfrom Mar 20, 2025
Merged
Set "from" email address using settings table instead of hard-coding#273tom93 merged 7 commits intoNZOI:masterfrom
tom93 merged 7 commits intoNZOI:masterfrom
Conversation
Passwords should never be hard-coded in the source code for security. The default user name was somewhat useful for the official instance, but it's cleaner to avoid it. (Note: Now that the default value is blank, it's not clear that the setting "system/mailer/username" is supposed to be an email address. This will be addressed in a later commit that renames that setting to "system/mailer/email".)
Refactor the code to set the sender email address in one place instead of multiple places. This is in preparation for making the "from" address configurable. Note that `config.action_mailer.default()` doesn't work[1], so we must use `default_options=`. Also drop `:charset => "utf-8"`, because the default is already UTF-8 (and that line wasn't even doing anything due to [1]). [1] https://www.github.com/rails/rails/issues/54700
Previously the "from" address was hard-coded to nztrain@gmail.com and could not be configured. This commit uses the value of the "system/mailer/username" setting, which is the user name for SMTP authentication. It will be the same as the "from" email when using Gmail and in most other setups. (A later commit will rename that setting to "system/mailer/email".) * We provide a default email in the test environment, otherwise the specs fail. * We don't provide a default email in the production environment, because the admin must configure email properly in order for mail delivery to work. * We don't provide a default email in the development environment, because mail delivery will fail anyway (unless SMTP is configured properly). The admin will still be able to read the emails by looking in the development log. In the future we may wish to add a setting for overriding the "Reply-To" address; it could be useful for redirecting responses to an mailbox that's actually monitored (https://www.github.com/NZOI/nztrain/pull/272#issuecomment-2704959896). But for now we don't bother.
It is now used for both SMTP authentication and for the "from" email address, so the term "email" is clearer and more accurate.
It's better to use seeds for adding initial data to the database.
Ideally it should be configurable, but we hard-code it for now like in other places for simplicity. Suggested-by: Jonathan Khoo <jkhoo98@gmail.com>
Contributor
Author
|
Deployed, thanks for your patience :) (I forgot you already created #254 to use seeds.rb.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Second version of #272.
This PR renames the setting system/mailer/username to system/mailer/email and uses that for both SMTP authentication and as "from" email address (previously it was used only for SMTP authentication, and the "from" address was hard-coded to "nztrain@gmail.com").
It also sets the sender's display name to "NZOI Training" and cleans up a couple of things.