Skip to content

Commit

Permalink
Merge pull request #1181 from codidact/configurable-emails
Browse files Browse the repository at this point in the history
Configurable email senders
  • Loading branch information
ArtOfCode- authored Dec 3, 2023
2 parents 7e5c40b + df2ceca commit 010b7a2
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 39 deletions.
16 changes: 12 additions & 4 deletions app/mailers/admin_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class AdminMailer < ApplicationMailer
default from: 'Codidact Admins <[email protected]>'
default from: lambda {
"#{SiteSetting['ModeratorDistributionListSenderName']} " \
"<#{SiteSetting['ModeratorDistributionListSenderEmail']}>"
}

def to_moderators
@subject = params[:subject]
Expand All @@ -8,14 +11,19 @@ def to_moderators
"INNER JOIN community_users cu ON cu.user_id = u.id WHERE s.type = 'moderators' AND " \
'(u.is_global_admin = 1 OR u.is_global_moderator = 1 OR cu.is_admin = 1 OR cu.is_moderator = 1)'
emails = ActiveRecord::Base.connection.execute(query).to_a.flatten
mail subject: "Codidact Moderators: #{@subject}", to: '[email protected]', bcc: emails
from = "#{SiteSetting['ModeratorDistributionListSenderName']} " \
"<#{SiteSetting['ModeratorDistributionListSenderEmail']}>"
to = SiteSetting['ModeratorDistributionListSenderEmail']
mail subject: "Codidact Moderators: #{@subject}", to: to, from: from, bcc: emails
end

def to_all_users
@subject = params[:subject]
@body_markdown = params[:body_markdown]
@users = User.where('email NOT LIKE ?', '%localhost').select(:email).map(&:email)
mail subject: @subject, to: '[email protected]', from: 'Codidact Team <[email protected]>',
reply_to: '[email protected]', bcc: @users
to = SiteSetting['AllUsersSenderEmail']
from = "#{SiteSetting['AllUsersSenderName']} <#{SiteSetting['AllUsersSenderEmail']}>"
reply_to = SiteSetting['AllUsersReplyToEmail']
mail subject: @subject, to: to, from: from, reply_to: reply_to, bcc: @users
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'Codidact <[email protected]>'
default from: -> { "#{SiteSetting['NoReplySenderName']} <#{SiteSetting['NoReplySenderEmail']}>" }
layout 'mailer'
end
12 changes: 6 additions & 6 deletions app/mailers/donation_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ def donation_successful
@amount = params[:amount]
@email = params[:email]
@name = params[:name]
mail from: 'Codidact Donations <[email protected]>',
reply_to: 'Codidact Support <[email protected]>',
to: @email, subject: 'Thanks for your donation!'
from = "#{SiteSetting['DonationSenderName']} <#{SiteSetting['DonationSenderEmail']}>"
reply_to = "#{SiteSetting['DonationSupportReceiverName']} <#{SiteSetting['DonationSupportReceiverEmail']}>"
mail from: from, reply_to: reply_to, to: @email, subject: 'Thanks for your donation!'
end

def donation_uncaptured
Expand All @@ -15,8 +15,8 @@ def donation_uncaptured
@email = params[:email]
@name = params[:name]
@intent = params[:intent]
mail from: 'Codidact Donations <[email protected]>',
reply_to: 'Codidact Support <[email protected]>',
to: @email, subject: 'Your donation is unfinished - was everything okay?'
from = "#{SiteSetting['DonationSenderName']} <#{SiteSetting['DonationSenderEmail']}>"
reply_to = "#{SiteSetting['DonationSupportReceiverName']} <#{SiteSetting['DonationSupportReceiverEmail']}>"
mail from: from, reply_to: reply_to, to: @email, subject: 'Your donation is unfinished - was everything okay?'
end
end
5 changes: 3 additions & 2 deletions app/mailers/flag_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def flag_escalated
.or(User.joins(:community_user)
.where(community_users: { is_admin: true, community_id: @flag.community_id }))
.select(:email).map(&:email)
mail from: 'Codidact <[email protected]>', to: '[email protected]', bcc: emails,
subject: "New flag escalation on #{@flag.community.name}"
from = "#{SiteSetting['NoReplySenderName']} <#{SiteSetting['NoReplySenderEmail']}>"
to = SiteSetting['NoReplySenderEmail']
mail from: from, to: to, bcc: emails, subject: "New flag escalation on #{@flag.community.name}"
end
end
5 changes: 4 additions & 1 deletion app/mailers/subscription_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def subscription
return
end

# Load request community to ensure we can access the settings/posts of the correct community
RequestContext.community = @subscription.community
site_name = @subscription.community.name
subject = if @subscription.name.present?
"Latest questions from your '#{@subscription.name}' subscription on #{site_name}"
Expand All @@ -19,6 +21,7 @@ def subscription
end

@subscription.update(last_sent_at: DateTime.now)
mail from: 'Codidact Subscriptions <[email protected]>', to: @subscription.user.email, subject: subject
from = "#{SiteSetting['SubscriptionSenderName']} <#{SiteSetting['SubscriptionSenderEmail']}>"
mail from: from, to: @subscription.user.email, subject: subject
end
end
2 changes: 1 addition & 1 deletion app/mailers/two_factor_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TwoFactorMailer < ApplicationMailer
default from: 'Codidact <[email protected]>'
default from: -> { "#{SiteSetting['NoReplySenderName']} <#{SiteSetting['NoReplySenderEmail']}>" }

def disable_email
user = params[:user]
Expand Down
11 changes: 9 additions & 2 deletions app/views/admin/all_email.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
<%= render 'posts/markdown_script' %>

<div class="notice is-danger">
<p>
<i class="fas fa-exclamation-triangle"></i> Please be careful, as this tool sends a lot of emails.
</p>
</div>

<h1><%= t 'admin.tools.email_all' %></h1>
<p><%= t 'admin.email_all_blurb' %></p>

<%= form_with url: send_all_email_path do |f| %>
<div class="form-group">
<%= f.label :subject, t('g.subject').capitalize, class: 'form-element' %>
<%= f.text_field :subject, class: 'form-element' %>
<%= f.text_field :subject, class: 'form-element', required: true %>
</div>

<%= render 'shared/body_field', f: f, field_name: :body_markdown, field_label: t('g.body').capitalize, post: nil %>

<div class="post-preview"></div>

<%= f.submit t('g.send').capitalize, class: 'button is-filled' %>
<%= f.submit t('g.send').capitalize, class: 'button is-filled',
onclick: "return confirm('Are you sure you want to send this email to all users?')" %>
<% end %>
30 changes: 15 additions & 15 deletions app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,28 @@
<div class="grid--cell is-4-lg is-6-md is-12-sm" data-ckb-list-item data-ckb-item-type="link">
<div class="widget">
<div class="widget--body">
<i class="fas fa-globe has-color-red-700"></i> <i class="fas fa-envelope"></i>
<%= link_to t('admin.tools.email_moderators'), moderator_email_path, 'data-ckb-item-link' => '' %>
<i class="fas fa-globe has-color-red-700"></i> <i class="fas fa-mail-bulk"></i>
<%= link_to t('admin.tools.post_types'), post_types_path, 'data-ckb-item-link' => '' %>
</div>
</div>
</div>

<% if current_user.developer? %>
<div class="grid--cell is-4-lg is-6-md is-12-sm" data-ckb-list-item data-ckb-item-type="link">
<div class="widget">
<div class="widget--body">
<i class="fas fa-globe has-color-red-700"></i> <i class="fas fa-folder-plus"></i>
<%= link_to 'Email All Users', email_all_path, 'data-ckb-item-link' => '' %>
</div>
</div>
</div>
<% end %>

<div class="grid--cell is-4-lg is-6-md is-12-sm" data-ckb-list-item data-ckb-item-type="link">
<div class="widget">
<div class="widget--body">
<i class="fas fa-globe has-color-red-700"></i> <i class="fas fa-mail-bulk"></i>
<%= link_to t('admin.tools.post_types'), post_types_path, 'data-ckb-item-link' => '' %>
<i class="fas fa-globe has-color-red-700"></i> <i class="fas fa-envelope"></i>
<%= link_to t('admin.tools.email_moderators'), moderator_email_path, 'data-ckb-item-link' => '' %>
</div>
</div>
</div>
Expand Down Expand Up @@ -128,15 +139,4 @@
</div>
</div>
<% end %>

<% if current_user.developer? %>
<div class="grid--cell is-4-lg is-6-md is-12-sm" data-ckb-list-item data-ckb-item-type="link">
<div class="widget">
<div class="widget--body">
<i class="fas fa-folder-plus"></i>
<%= link_to 'Email All Users', email_all_path, 'data-ckb-item-link' => '' %>
</div>
</div>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/donation_mailer/donation_uncaptured.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<p>
We want to make sure that everything's okay. If you just changed your mind, that's absolutely fine! We're not here
to push. If you ran into problems, however, please let us know if we can help &mdash; you can reply to this email to
reach us at <a href="mailto:[email protected]">[email protected]</a>, or you can post on our
reach us at <a href="mailto:<%= message.reply_to %>"><%= message.reply_to %></a>, or you can post on our
<a href="https://meta.codidact.com/">Meta</a> site, and someone will lend a hand.
</p>

Expand Down
2 changes: 1 addition & 1 deletion app/views/donation_mailer/donation_uncaptured.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You started the process to make a donation of <%= number_to_currency(@amount, un

We want to make sure that everything's okay. If you just changed your mind, that's absolutely fine! We're not here
to push. If you ran into problems, however, please let us know if we can help — you can reply to this email to
reach us at [email protected], or you can post on our Meta site (https://meta.codidact.com/), and someone will
reach us at <%= message.reply_to %>, or you can post on our Meta site (https://meta.codidact.com/), and someone will
lend a hand.

Whatever happened, thanks for thinking of us!
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/devise_mailer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
London, UK<br/>
WC2H 9JQ
</p>
<p><a href="mailto:[email protected]">[email protected]</a></p>
<p>
<a href="mailto:<%= SiteSetting['SupportReceiverEmail'] || '[email protected]' %>">
<%= SiteSetting['SupportReceiverEmail'] || '[email protected]' %>
</a>
</p>
</div>
</div>
</body>
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
London, UK<br/>
WC2H 9JQ
</p>
<p><a href="mailto:[email protected]">[email protected]</a></p>
<p>
<a href="mailto:<%= SiteSetting['SupportReceiverEmail'] || '[email protected]' %>">
<%= SiteSetting['SupportReceiverEmail'] || '[email protected]' %>
</a>
</p>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
71-75 Shelton Street
London, UK
WC2H 9JQ
[email protected]
<%= SiteSetting['SupportReceiverEmail'] || '[email protected]' %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameSiteSettingCategoryEmailSubscriptions < ActiveRecord::Migration[7.0]
def change
SiteSetting.where(category: 'EmailSubscriptions').update_all(category: 'Email')
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_08_03_191600) do
ActiveRecord::Schema[7.0].define(version: 2023_08_17_213150) do
create_table "abilities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "community_id"
t.string "name"
Expand Down
114 changes: 113 additions & 1 deletion db/seeds/site_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,122 @@
- name: InterestingSubscriptionScoreThreshold
value: 1
value_type: integer
category: EmailSubscriptions
category: Email
description: >
The minimum score a question must have to qualify for selection for the Interesting email subscription.
- name: SubscriptionSenderName
value: Codidact Subscriptions
value_type: string
category: Email
description: >
The name of the sender of subscription emails.
- name: SubscriptionSenderEmail
value: [email protected]
value_type: string
category: Email
description: >
The address to send subscription emails from (can be a fake address).
Make sure your server is allowed to send email from this address, or your mails will not be received.
- name: ModeratorDistributionListSenderName
value: Codidact Admins
value_type: string
category: Email
description: >
The name of the sender of the moderator distribution list.
- name: ModeratorDistributionListSenderEmail
value: [email protected]
value_type: string
category: Email
description: >
The address to send moderator distribution list emails from (can be a fake address).
Make sure your server is allowed to send email from this address, or your mails will not be received.
- name: NoReplySenderName
value: Codidact
value_type: string
category: Email
description: >
The name of the sender of no-reply emails.
- name: NoReplySenderEmail
value: [email protected]
value_type: string
category: Email
description: >
The address to send no-reply emails from (can be a fake address).
Example uses of this address are 2FA emails, flag notifications, account emails, and more.
Make sure your server is allowed to send email from this address, or your mails will not be received.
- name: DonationSupportReceiverName
value: Codidact Support
value_type: string
category: Email
description: >
The name of the donation support email address.
- name: DonationSupportReceiverEmail
value: [email protected]
value_type: string
category: Email
description: >
The (real) address to receive donation support emails on. Used for users who want to reply to donation emails.
- name: DonationSenderName
value: Codidact Donations
value_type: string
category: Email
description: >
The name of the sender of donation related emails.
- name: DonationSenderEmail
value: [email protected]
value_type: string
category: Email
description: >
The address to send donation related emails from (can be a fake address).
Make sure your server is allowed to send email from this address, or your mails will not be received.
- name: SupportReceiverName
value: Codidact Support
value_type: string
category: Email
description: >
The name of the donation support email address.
- name: SupportReceiverEmail
value: [email protected]
value_type: string
category: Email
description: >
The (real) address to receive support emails on.
- name: AllUsersSenderName
value: Codidact Team
value_type: string
category: Email
description: >
The name of the sender of emails sent to all users of the network via the admin tools.
- name: AllUsersSenderEmail
value: [email protected]
value_type: string
category: Email
description: >
The address to use as sender for emails sent to all users of the network via the admin tools (can be a fake address).
Make sure your server is allowed to send email from this address, or your mails will not be received.
- name: AllUsersReplyToEmail
value: [email protected]
value_type: string
category: Email
description: >
The (real) address to use as reply-to for emails sent to all users of the network via the admin tools.
- name: LotteryAgeDeprecationSpeed
value: 0.002
value_type: float
Expand Down

0 comments on commit 010b7a2

Please sign in to comment.