Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organization or application name in emails' From header #12837

Merged
merged 11 commits into from May 15, 2024

Conversation

andreslucena
Copy link
Member

馃帺 What? Why?

All the emails sent by a Decidim instance don't show the Organization/Application name, so it's difficult to see which instance is the one sending it.

As an example, this is an email from an organization (aka Tenant) called "Comunitat Can貌drom", whose URL is https://comunitat.canodrom.barcelona/

Screenshot of the email

This PR fixes this issue. In the case of the mails from System, as they could not have an Organization associated, we use the Application name config accessor.

Testing

  1. Reset your password.
  2. Create a new organization in system
  3. See the emails in http://localhost:3000/letter_opener/

鈾ワ笍 Thank you!

github-actions[bot]
github-actions bot previously approved these changes May 6, 2024
github-actions[bot]
github-actions bot previously approved these changes May 7, 2024
github-actions[bot]
github-actions bot previously approved these changes May 7, 2024
- Add organization name in emails' from in core and admin modules
- Add application name in emails' from in system module
github-actions[bot]
github-actions bot previously approved these changes May 7, 2024
@andreslucena andreslucena marked this pull request as ready for review May 7, 2024 09:38
Copy link
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% that i understand what PR does differently than what is already existing within the application.
In system panel we have "show advanced settings" button that is revealing the SMTP from and label:
image

Additionally, there is a help message for label field that states: "Email sender will be: "your-organization-name your-organization@example.org". Leave blank to use the 'Email address' as label."

If i do not set the SMTP email and label or sending mails from system panel, i get as sender: "From: change-me@example.org", apart of that, the organization seems to be correctly set when using Decidim::ApplicationMailer.

image

I think it would be a much simpler fix if we could extend Decidim::Admin::ApplicationMailer from Decidim::ApplicationMailer, or just delete the class, as there is no mailer that has Decidim::Admin::ApplicationMailer as ancestor.

@andreslucena
Copy link
Member Author

In system panel we have "show advanced settings" button that is revealing the SMTP from and label:

This happens when the SMTP is configured through ENV VARS instead of the system panel, or at least that's how I could reproduce it with a new instance. Mind that the Canodrom instance isn't the only one with this behavior, this is a small sample from my email:

Bug in Metadecidim

Bug in PokeCode

Bug in AI Politics

By researching my email, I found out that even Decidim Barcelona had this bug until 2021:

Bug in Decidim Barcelona

github-actions[bot]
github-actions bot previously approved these changes May 8, 2024
@andreslucena andreslucena dismissed alecslupu鈥檚 stale review May 9, 2024 07:23

I've applied the suggestion by removing the unused class (Decidim::Admin::ApplicationMailer)

Copy link
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried the functionality in this PR, and i am not 100% sure is working as it should.

I have set the environment variable to: DECIDIM_MAILER_SENDER='Mimi <foobar@decidim.org>'

The obtained email sender was : foobar@decidim.org , reading the code, i would have expected to have either Mimi <foobar@decidim.org>, either My Organization <foobar@decidim.org>

Again, when combined with organization db settings works as intended.

In order to make it show My Organization <foobar@decidim.org>, i had to combine set_smtp and set_from as follows:

    def set_smtp
      return if @organization.nil? || @organization.smtp_settings.blank?

      # the following line has been changed
      mail.from = @organization.smtp_settings["from"].presence || email_address_with_name(Decidim.config.mailer_sender, @organization.name)
      mail.reply_to = mail.reply_to || Decidim.config.mailer_reply
      mail.delivery_method.settings.merge!(
        address: @organization.smtp_settings["address"],
        port: @organization.smtp_settings["port"],
        user_name: @organization.smtp_settings["user_name"],
        password: Decidim::AttributeEncryptor.decrypt(@organization.smtp_settings["encrypted_password"])
      ) { |_k, o, v| v.presence || o }.compact_blank!
    end

But this would make some specs to fail.

I have dissected the set_from method, and my tests are calling mail.from = email_address_with_name(mail.from.first, @organization.name) from set_from, to no effect.

Could expand a bit the testing steps, as i am not sure what i am doing bad.

While reviewing, it was found that if the from was defined in the
System settings, then the From header wasn't set correctly.

To prevent this bug we set-up the from only once in the after_action,
and make some checks if we should actually redefine this From or not.
github-actions[bot]
github-actions bot previously approved these changes May 10, 2024
github-actions[bot]
github-actions bot previously approved these changes May 10, 2024
Copy link
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just thinking that we may simplify a bit... What do u think ?

decidim-core/app/mailers/decidim/application_mailer.rb Outdated Show resolved Hide resolved
decidim-core/app/mailers/decidim/application_mailer.rb Outdated Show resolved Hide resolved
decidim-core/app/mailers/decidim/application_mailer.rb Outdated Show resolved Hide resolved
Co-authored-by: Alexandru Emil Lupu <contact@alecslupu.ro>
github-actions[bot]
github-actions bot previously approved these changes May 10, 2024
@andreslucena andreslucena dismissed alecslupu鈥檚 stale review May 13, 2024 06:43

Changes were applied

@andreslucena
Copy link
Member Author

@alecslupu this is ready for another review. Apart from what we've talked about, I also fixed the bug of having already defined the name in the from of the Decidim.config.mailer_sender accessor

Copy link
Contributor

@alecslupu alecslupu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested few scenarios and seems to work properly.

LGTM

@alecslupu alecslupu merged commit 8f182de into develop May 15, 2024
110 checks passed
@alecslupu alecslupu deleted the fix/mails-from-header branch May 15, 2024 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants