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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow emails to be turned off per deployment #720

Merged
merged 3 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 18 additions & 15 deletions app/mailers/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class Notifier < ActionMailer::Base
def new_identity_waiting_for_approval identity
@identity = identity

email = Rails.env == 'production' ? ADMIN_MAIL_TO : DEFAULT_MAIL_TO
cc = Rails.env == 'production' ? NEW_USER_CC : nil
subject = Rails.env == 'production' ? "New Question from #{t(:mailer)[:application_title]}" : "[#{Rails.env.capitalize} - EMAIL TO #{ADMIN_MAIL_TO} AND CC TO #{NEW_USER_CC}] Request for new #{t(:mailer)[:application_title]} account submitted and awaiting approval"
email = ADMIN_MAIL_TO
cc = NEW_USER_CC

##REVIEW: This subject appears incorrect? Copy paste from previous method?
subject = "New Question from #{t(:mailer)[:application_title]}"

mail(:to => email, :cc => cc, :from => @identity.email, :subject => subject)
end
Expand All @@ -50,8 +52,8 @@ def notify_user(project_role, service_request, xls, approval, user_current)
attachments["service_request_#{@service_request.protocol.id}.xlsx"] = xls

# only send these to the correct person in the production env
email = Rails.env == 'production' ? @identity.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{t(:mailer)[:application_title]} service request" : "[#{Rails.env.capitalize} - EMAIL TO #{@identity.email}] #{t(:mailer)[:application_title]} service request"
email = @identity.email
subject = "#{t(:mailer)[:application_title]} service request"

mail(:to => email, :from => NO_REPLY_FROM, :subject => subject)
end
Expand All @@ -76,8 +78,8 @@ def notify_admin(submission_email_address, xls, user_current, ssr, audit_report=
@audit_report = audit_report
attachments["service_request_#{@service_request.protocol.id}.xlsx"] = xls

email = Rails.env == 'production' ? submission_email_address : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{t(:mailer)[:application_title]} service request" : "[#{Rails.env.capitalize} - EMAIL TO #{submission_email_address}] #{t(:mailer)[:application_title]} service request"
email = submission_email_address
subject = "#{t(:mailer)[:application_title]} service request"

mail(:to => email, :from => NO_REPLY_FROM, :subject => subject)
end
Expand Down Expand Up @@ -116,18 +118,19 @@ def notify_service_provider(service_provider, service_request, attachments_to_ad
end

# only send these to the correct person in the production env
email = Rails.env == 'production' ? service_provider.identity.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{@protocol.id} - #{t(:mailer)[:application_title]} service request" : "#{@protocol.id} - [#{Rails.env.capitalize} - EMAIL TO #{service_provider.identity.email}] #{t(:mailer)[:application_title]} service request"
email = service_provider.identity.email
subject = "#{@protocol.id} - #{t(:mailer)[:application_title]} service request"

mail(:to => email, :from => NO_REPLY_FROM, :subject => subject)
end

def account_status_change identity, approved
@approved = approved

##REVIEW: Why do we care what the from is?
email_from = Rails.env == 'production' ? ADMIN_MAIL_TO : DEFAULT_MAIL_TO
email_to = Rails.env == 'production' ? identity.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{t(:mailer)[:application_title]} account request - status change" : "[#{Rails.env.capitalize} - EMAIL TO #{identity.email}] #{t(:mailer)[:application_title]} account request - status change"
email_to = identity.email
subject = "#{t(:mailer)[:application_title]} account request - status change"

mail(:to => email_to, :from => email_from, :subject => subject)
end
Expand All @@ -139,7 +142,7 @@ def obtain_research_pricing service_provider, service_request
def provide_feedback feedback
@feedback = feedback

email_to = Rails.env == 'production' ? FEEDBACK_MAIL_TO : DEFAULT_MAIL_TO
email_to = FEEDBACK_MAIL_TO
email_from = @feedback.email.blank? ? DEFAULT_MAIL_TO : @feedback.email

mail(:to => email_to, :from => email_from, :subject => "Feedback")
Expand All @@ -152,8 +155,8 @@ def sub_service_request_deleted identity, sub_service_request, user_current
@service_request = sub_service_request.service_request
@ssr = sub_service_request

email_to = Rails.env == 'production' ? identity.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{t(:mailer)[:application_title]} - service request deleted" : "[#{Rails.env.capitalize} - EMAIL TO #{identity.email}] #{t(:mailer)[:application_title]} - service request deleted"
email_to = identity.email
subject = "#{t(:mailer)[:application_title]} - service request deleted"

mail(:to => email_to, :from => NO_REPLY_FROM, :subject => subject)
end
Expand All @@ -171,7 +174,7 @@ def notify_primary_pi_for_epic_user_final_review protocol
@protocol = protocol
@primary_pi = @protocol.primary_principal_investigator

email_to = Rails.env == 'production' ? @primary_pi.email : DEFAULT_MAIL_TO
email_to = @primary_pi.email
subject = 'Epic Rights User Approval'

mail(:to => email_to, :from => NO_REPLY_FROM, :subject => subject)
Expand Down
11 changes: 6 additions & 5 deletions app/mailers/survey_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ def system_satisfaction_survey response_set
@response_set = response_set
@identity = Identity.find response_set.user_id

email = Rails.env == 'production' ? ADMIN_MAIL_TO : DEFAULT_MAIL_TO
cc = Rails.env == 'production' ? SYSTEM_SATISFACTION_SURVEY_CC : nil
subject = Rails.env == 'production' ? "System satisfaction survey completed in #{t(:mailer)[:application_title]}" : "[#{Rails.env.capitalize} - EMAIL TO #{ADMIN_MAIL_TO} AND CC TO #{SYSTEM_SATISFACTION_SURVEY_CC}] System satisfaction survey completed in #{t(:mailer)[:application_title]}"
email = ADMIN_MAIL_TO
cc = SYSTEM_SATISFACTION_SURVEY_CC
subject = "System satisfaction survey completed in #{t(:mailer)[:application_title]}"

mail(:to => email, :cc => cc, :from => @identity.email, :subject => subject)
end

def service_survey surveys, identity, ssr
@identity = identity
@surveys = surveys
@ssr = ssr
email = Rails.env == 'production' ? @identity.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? "#{t(:mailer)[:application_title]} Survey Notification" : "[#{Rails.env.capitalize} - EMAIL TO #{@identity.email}] #{t(:mailer)[:application_title]} Survey Notification"
email = @identity.email
subject = "#{t(:mailer)[:application_title]} Survey Notification"
mail(:to => email, :from => NO_REPLY_FROM, :subject => subject)
end

Expand Down
3 changes: 1 addition & 2 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def notification_received(user, ssr)
private

def send_message subject, is_service_provider='false', ssr_id=''
email = Rails.env == 'production' ? @send_to.email : DEFAULT_MAIL_TO
subject = Rails.env == 'production' ? subject : "[#{Rails.env.capitalize} - EMAIL TO #{@send_to.email}] #{subject}"
email = @send_to.email
Copy link
Contributor

Choose a reason for hiding this comment

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

subject is missing here now. Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes intentional, subject is passed straight through to mail.

Otherwise it would have been subject = subject I think

@is_service_provider = is_service_provider
@ssr_id = ssr_id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
application_config ||= YAML.load_file(Rails.root.join('config', 'application.yml'))[Rails.env]

DEFAULT_MAIL_TO = application_config['default_mail_to']

# Flag must be enabled for the system to
# send emails to actual users.
# Otherwise all emails will be routed to DEFAULT_MAIL_TO
SEND_EMAILS_TO_REAL_USERS = application_config['send_emails_to_real_users']

ADMIN_MAIL_TO = application_config['admin_mail_to']
EPIC_RIGHTS_MAIL_TO = application_config['approve_epic_rights_mail_to']
FEEDBACK_MAIL_TO = application_config['feedback_mail_to']
Expand Down Expand Up @@ -132,4 +138,4 @@
ALERT_STATUSES = config['alert_statuses']
rescue
raise "constants.yml not found"
end
end
17 changes: 17 additions & 0 deletions config/initializers/mail_send_interceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class MailSendInterceptor

class << self
def delivering_email(mail)
cc_to = " AND CC TO #{mail.cc}" if mail.cc.present?

mail.subject = "[#{HOST} - EMAIL TO #{mail.to} #{cc_to}] #{mail.subject}"
mail.to = DEFAULT_MAIL_TO
mail.cc = nil
end
end
end

# This initializer depends on obis_setup having been run first in order to read in application config values
if SEND_EMAILS_TO_REAL_USERS != true
ActionMailer::Base.register_interceptor(MailSendInterceptor)
end
2 changes: 1 addition & 1 deletion spec/mailers/notifier/get_a_cost_estimate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
identity,
service_request.sub_service_requests.first.id) }
it 'should display correct subject' do
expect(mail).to have_subject("#{service_request.protocol.id} - [Test - EMAIL TO glennj@musc.edu] SPARCRequest service request")
expect(mail).to have_subject("#{service_request.protocol.id} - SPARCRequest service request")
end

# Expected service provider message is defined under get_a_cost_estimate_service_provider_admin_message
Expand Down
2 changes: 1 addition & 1 deletion spec/mailers/notifier/submitted_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
false) }

it 'should display correct subject' do
expect(mail).to have_subject("#{service_request.protocol.id} - [Test - EMAIL TO glennj@musc.edu] SPARCRequest service request")
expect(mail).to have_subject("#{service_request.protocol.id} - SPARCRequest service request")
end
# Expected service provider message is defined under submitted_service_provider_and_admin_message
it 'should display service provider intro message, conclusion, link, and should not display acknowledgments' do
Expand Down
8 changes: 4 additions & 4 deletions spec/mailers/survey_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

#ensure that the subject is correct
it 'renders the subject' do
expect(mail).to have_subject("[Test - EMAIL TO catesa@musc.edu AND CC TO amcates@gmail.com, catesa@musc.edu] System satisfaction survey completed in SPARCRequest")
expect(mail).to have_subject("System satisfaction survey completed in SPARCRequest")
end

#ensure that the receiver is correct
Expand All @@ -55,7 +55,7 @@

#ensure that the sender is correct
it 'renders the sender email' do
expect(mail).to deliver_to(DEFAULT_MAIL_TO)
expect(mail).to deliver_to(ADMIN_MAIL_TO)
end

#ensure that the e-mail body is correct
Expand All @@ -75,12 +75,12 @@

#ensure that the subject is correct
it 'renders the subject' do
expect(mail).to have_subject("[Test - EMAIL TO nobody@nowhere.com] SPARCRequest Survey Notification")
expect(mail).to have_subject("SPARCRequest Survey Notification")
end

#ensure that the receiver is correct
it 'renders the receiver email' do
expect(mail).to deliver_to(DEFAULT_MAIL_TO)
expect(mail).to deliver_to(identity.email)
end

#ensure that the sender is correct
Expand Down