diff --git a/.env.sample b/.env.sample
index 9df289ce7..75211a527 100644
--- a/.env.sample
+++ b/.env.sample
@@ -1,3 +1,8 @@
GCS_CREDENTIALS_FILE=
GCS_BUCKET=
MQ_BETA_ENABLED=true
+
+# SMTP API
+SMTP_API_KEY=
+SMTP_API_SECRET=
+SMTP_DEFAULT_FROM=
diff --git a/Gemfile b/Gemfile
index 9c0436d25..22c6bc915 100644
--- a/Gemfile
+++ b/Gemfile
@@ -44,6 +44,7 @@ gem 'react-rails'
gem 'appsignal'
gem 'roo'
gem 'caxlsx'
+gem 'mailjet'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
diff --git a/Gemfile.lock b/Gemfile.lock
index bd1c07330..397d68a14 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -291,6 +291,11 @@ GEM
net-imap
net-pop
net-smtp
+ mailjet (1.8.0)
+ activesupport (>= 5.0.0)
+ faraday (~> 2.1)
+ rack (>= 1.4.0)
+ yajl-ruby
marcel (1.0.4)
matrix (0.4.2)
memoist (0.16.2)
@@ -547,6 +552,7 @@ GEM
rails (>= 3.2.16)
xpath (3.2.0)
nokogiri (~> 1.8)
+ yajl-ruby (1.4.3)
zeitwerk (2.6.18)
PLATFORMS
@@ -589,6 +595,7 @@ DEPENDENCIES
image_processing
language_list
listen (>= 3.0.5, < 3.2)
+ mailjet
pg (>= 0.18, < 2.0)
pg_search (= 2.3.2)
pry
diff --git a/app/controllers/tpi/sectors_controller.rb b/app/controllers/tpi/sectors_controller.rb
index b3d4eb544..086105b29 100644
--- a/app/controllers/tpi/sectors_controller.rb
+++ b/app/controllers/tpi/sectors_controller.rb
@@ -78,6 +78,11 @@ def cp_performance_chart_data
render json: data.chart_json
end
+ def send_download_file_info_email
+ DataDownloadMailer.send_download_file_info_email(permitted_email_params).deliver_now
+ head :ok
+ end
+
def user_download_all
send_user_download_file(
Company.published.select(:id).where(sector_id: @sectors.pluck(:id)),
@@ -163,5 +168,9 @@ def companies_scope(params)
Company.published.active
end
end
+
+ def permitted_email_params
+ params.permit(:email, :job_title, :forename, :surname, :location, :organisation, purposes: [])
+ end
end
end
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 286b2239d..131c38e5a 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
- default from: 'from@example.com'
+ default from: ENV['SMTP_DEFAULT_FROM']
layout 'mailer'
end
diff --git a/app/mailers/data_download_mailer.rb b/app/mailers/data_download_mailer.rb
new file mode 100644
index 000000000..4b41fe7ba
--- /dev/null
+++ b/app/mailers/data_download_mailer.rb
@@ -0,0 +1,6 @@
+class DataDownloadMailer < ApplicationMailer
+ def send_download_file_info_email(data)
+ @data = data
+ mail(to: 'martintomas.it@gmail.com', subject: 'TPI data has been downloaded')
+ end
+end
diff --git a/app/views/data_download_mailer/send_download_file_info_email.html.erb b/app/views/data_download_mailer/send_download_file_info_email.html.erb
new file mode 100644
index 000000000..a555a850d
--- /dev/null
+++ b/app/views/data_download_mailer/send_download_file_info_email.html.erb
@@ -0,0 +1,15 @@
+
Following user downloaded data from TPI website
+
+Email: <%= @data[:email] %>
+Job Title: <%= @data[:job_title] %>
+Forename: <%= @data[:forename] %>
+Surname: <%= @data[:surname] %>
+Location: <%= @data[:location] %>
+Organisation: <%= @data[:organisation] %>
+
+Purposes
+
+ <% Array.wrap(@data[:purposes]).each do |purpose| %>
+ - <%= purpose %>
+ <% end %>
+
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 444e6b0a1..fff936694 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -33,10 +33,8 @@
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
- # Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
-
- config.action_mailer.perform_caching = false
+ config.action_mailer.perform_deliveries = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
diff --git a/config/environments/production.rb b/config/environments/production.rb
index 99067f050..0cbeb7711 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -62,11 +62,9 @@
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "laws_and_pathways_production"
- config.action_mailer.perform_caching = false
-
- # Ignore bad email addresses and do not raise email delivery errors.
- # Set this to true and configure the email server for immediate delivery to raise delivery errors.
- # config.action_mailer.raise_delivery_errors = false
+ config.action_mailer.raise_delivery_errors = true
+ config.action_mailer.delivery_method = :mailjet
+ config.action_mailer.perform_deliveries = false
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
diff --git a/config/environments/staging.rb b/config/environments/staging.rb
index 3a1b12b98..040f46b11 100644
--- a/config/environments/staging.rb
+++ b/config/environments/staging.rb
@@ -65,11 +65,9 @@
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "laws_and_pathways_#{Rails.env}"
- config.action_mailer.perform_caching = false
-
- # Ignore bad email addresses and do not raise email delivery errors.
- # Set this to true and configure the email server for immediate delivery to raise delivery errors.
- # config.action_mailer.raise_delivery_errors = false
+ config.action_mailer.raise_delivery_errors = true
+ config.action_mailer.delivery_method = :mailjet
+ config.action_mailer.perform_deliveries = false
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
diff --git a/config/initializers/mailjet.rb b/config/initializers/mailjet.rb
new file mode 100644
index 000000000..55c247c7c
--- /dev/null
+++ b/config/initializers/mailjet.rb
@@ -0,0 +1,6 @@
+# kindly generated by appropriated Rails generator
+Mailjet.configure do |config|
+ config.api_key = ENV['SMTP_API_KEY']
+ config.secret_key = ENV['SMTP_API_SECRET']
+ config.default_from = ENV['SMTP_DEFAULT_FROM']
+end
diff --git a/config/routes.rb b/config/routes.rb
index 677a8d07c..ab6986f29 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -24,6 +24,7 @@
get :cp_performance_chart_data
get :user_download_all
get :user_download_methodology
+ post :send_download_file_info_email
end
member do
get :levels_chart_data
diff --git a/spec/controllers/tpi/sectors_controller_spec.rb b/spec/controllers/tpi/sectors_controller_spec.rb
index 15aae7aa4..ffbce90de 100644
--- a/spec/controllers/tpi/sectors_controller_spec.rb
+++ b/spec/controllers/tpi/sectors_controller_spec.rb
@@ -254,4 +254,28 @@
end
end
end
+
+ describe 'POST send_download_file_info_email' do
+ subject { post :send_download_file_info_email, params: data }
+ let(:data) do
+ {
+ email: 'test@test.test',
+ job_title: 'job_title',
+ forename: 'forename',
+ surname: 'surname',
+ location: 'location',
+ organisation: 'organisation',
+ purposes: %w[purposes1 purposes2]
+ }
+ end
+
+ it 'returns ok status' do
+ subject
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'sends email' do
+ expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
+ end
+ end
end
diff --git a/spec/mailers/data_download_mailer_spec.rb b/spec/mailers/data_download_mailer_spec.rb
new file mode 100644
index 000000000..882386cd9
--- /dev/null
+++ b/spec/mailers/data_download_mailer_spec.rb
@@ -0,0 +1,33 @@
+require 'rails_helper'
+
+RSpec.describe DataDownloadMailer, type: :mailer do
+ describe 'send_download_file_info_email' do
+ let(:data) {
+ {
+ email: 'test@test.test',
+ job_title: 'job_title',
+ forename: 'forename',
+ surname: 'surname',
+ location: 'location',
+ organisation: 'organisation',
+ purposes: ['purpose1', 'purpose2']
+ }
+ }
+ let(:mail) { DataDownloadMailer.send_download_file_info_email data }
+
+ it 'renders the headers' do
+ expect(mail.subject).to eq('TPI data has been downloaded')
+ expect(mail.to).to eq(['martintomas.it@gmail.com'])
+ expect(mail.from).to eq([ENV['SMTP_DEFAULT_FROM']].reject(&:blank?))
+
+ expect(mail.body.encoded).to include('test@test.test')
+ expect(mail.body.encoded).to include('job_title')
+ expect(mail.body.encoded).to include('forename')
+ expect(mail.body.encoded).to include('surname')
+ expect(mail.body.encoded).to include('location')
+ expect(mail.body.encoded).to include('organisation')
+ expect(mail.body.encoded).to include('purpose1')
+ expect(mail.body.encoded).to include('purpose2')
+ end
+ end
+end