From 8164effa0bd60ea48af4c028afb7ec64762c0848 Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 11:13:51 +0200 Subject: [PATCH 01/11] feat: send email --- app/controllers/users_controller.rb | 1 + app/mailers/uuid_mailer.rb | 7 +++++++ app/views/uuid_mailer/send_email.html.erb | 1 + 3 files changed, 9 insertions(+) create mode 100644 app/mailers/uuid_mailer.rb create mode 100644 app/views/uuid_mailer/send_email.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9107b06e..94f1a203 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,6 +25,7 @@ def create if user.valid? user.lock_access! + UuidMailer.with(user: user).send_email.deliver_later user.save else return head 406 # Not acceptable diff --git a/app/mailers/uuid_mailer.rb b/app/mailers/uuid_mailer.rb new file mode 100644 index 00000000..c4c97759 --- /dev/null +++ b/app/mailers/uuid_mailer.rb @@ -0,0 +1,7 @@ +class UuidMailer < ApplicationMailer + def send_email + @user = params[:user] + + mail(to: @user.email, subject: "Votre compte Pia à été créé") + end +end \ No newline at end of file diff --git a/app/views/uuid_mailer/send_email.html.erb b/app/views/uuid_mailer/send_email.html.erb new file mode 100644 index 00000000..607651aa --- /dev/null +++ b/app/views/uuid_mailer/send_email.html.erb @@ -0,0 +1 @@ +Votre uuid: <%= @user.uuid %> \ No newline at end of file From 7b1003bf84298894bbf4df5ab763ad38dbe6a1d2 Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 11:45:05 +0200 Subject: [PATCH 02/11] feat: rename user mailer --- app/controllers/users_controller.rb | 2 +- app/mailers/{uuid_mailer.rb => user_mailer.rb} | 4 ++-- .../send_email.html.erb => user_mailer/uuid_created.html.erb} | 0 app/views/user_mailer/uuid_created.text.erb | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) rename app/mailers/{uuid_mailer.rb => user_mailer.rb} (66%) rename app/views/{uuid_mailer/send_email.html.erb => user_mailer/uuid_created.html.erb} (100%) create mode 100644 app/views/user_mailer/uuid_created.text.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 94f1a203..5b531dde 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,7 +25,7 @@ def create if user.valid? user.lock_access! - UuidMailer.with(user: user).send_email.deliver_later + UserMailer.with(user: user).uuid_created.deliver_later user.save else return head 406 # Not acceptable diff --git a/app/mailers/uuid_mailer.rb b/app/mailers/user_mailer.rb similarity index 66% rename from app/mailers/uuid_mailer.rb rename to app/mailers/user_mailer.rb index c4c97759..12c01e11 100644 --- a/app/mailers/uuid_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,5 +1,5 @@ -class UuidMailer < ApplicationMailer - def send_email +class UserMailer < ApplicationMailer + def uuid_created @user = params[:user] mail(to: @user.email, subject: "Votre compte Pia à été créé") diff --git a/app/views/uuid_mailer/send_email.html.erb b/app/views/user_mailer/uuid_created.html.erb similarity index 100% rename from app/views/uuid_mailer/send_email.html.erb rename to app/views/user_mailer/uuid_created.html.erb diff --git a/app/views/user_mailer/uuid_created.text.erb b/app/views/user_mailer/uuid_created.text.erb new file mode 100644 index 00000000..607651aa --- /dev/null +++ b/app/views/user_mailer/uuid_created.text.erb @@ -0,0 +1 @@ +Votre uuid: <%= @user.uuid %> \ No newline at end of file From 0cee89aec5be192f5c64d7e5c8489f0b6a7249c1 Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 11:52:51 +0200 Subject: [PATCH 03/11] feat: replace deliver_later by deliver_now --- app/controllers/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5b531dde..d8526061 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,7 +25,7 @@ def create if user.valid? user.lock_access! - UserMailer.with(user: user).uuid_created.deliver_later + UserMailer.with(user: user).uuid_created.deliver_now user.save else return head 406 # Not acceptable From 0079fc7216dc0170c2ebfd931424ee20cdac90f5 Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 14:07:07 +0200 Subject: [PATCH 04/11] feat: add translation and i18n to mail --- app/views/user_mailer/uuid_created.html.erb | 2 +- app/views/user_mailer/uuid_created.text.erb | 2 +- config/locales/en.yml | 2 ++ config/locales/fr.yml | 4 ++++ 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 config/locales/fr.yml diff --git a/app/views/user_mailer/uuid_created.html.erb b/app/views/user_mailer/uuid_created.html.erb index 607651aa..2be48ea6 100644 --- a/app/views/user_mailer/uuid_created.html.erb +++ b/app/views/user_mailer/uuid_created.html.erb @@ -1 +1 @@ -Votre uuid: <%= @user.uuid %> \ No newline at end of file +<%= I18n.t 'mail.uuid_created' %><%= @user.uuid %> \ No newline at end of file diff --git a/app/views/user_mailer/uuid_created.text.erb b/app/views/user_mailer/uuid_created.text.erb index 607651aa..2be48ea6 100644 --- a/app/views/user_mailer/uuid_created.text.erb +++ b/app/views/user_mailer/uuid_created.text.erb @@ -1 +1 @@ -Votre uuid: <%= @user.uuid %> \ No newline at end of file +<%= I18n.t 'mail.uuid_created' %><%= @user.uuid %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index decc5a85..b2ed9ddb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -31,3 +31,5 @@ en: hello: "Hello world" + mail: + uuid_created: "Your pia account was created, your uuid is: " diff --git a/config/locales/fr.yml b/config/locales/fr.yml new file mode 100644 index 00000000..405239d6 --- /dev/null +++ b/config/locales/fr.yml @@ -0,0 +1,4 @@ +fr: + hello: "Hello world" + mail: + uuid_created: "Votre compte à été créé, voilà votre uuid: " From faaa7cbca5af9dfaeb4625f632cc0605933b582c Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 14:23:55 +0200 Subject: [PATCH 05/11] fix: commit after review --- app/controllers/users_controller.rb | 2 +- app/mailers/user_mailer.rb | 2 +- config/locales/en.yml | 1 + config/locales/fr.yml | 3 ++- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d8526061..44722ea1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -25,8 +25,8 @@ def create if user.valid? user.lock_access! - UserMailer.with(user: user).uuid_created.deliver_now user.save + UserMailer.with(user: user).uuid_created.deliver_now else return head 406 # Not acceptable end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 12c01e11..61c17399 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -2,6 +2,6 @@ class UserMailer < ApplicationMailer def uuid_created @user = params[:user] - mail(to: @user.email, subject: "Votre compte Pia à été créé") + mail(to: @user.email, subject: I18n.t 'mail.subject') end end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index b2ed9ddb..8766c764 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -32,4 +32,5 @@ en: hello: "Hello world" mail: + uuid_subject: "Your PIA account" uuid_created: "Your pia account was created, your uuid is: " diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 405239d6..b8ffd856 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -1,4 +1,5 @@ fr: hello: "Hello world" mail: - uuid_created: "Votre compte à été créé, voilà votre uuid: " + uuid_subject: "Votre compte PIA" + uuid_created: "Votre compte a été créé, voilà votre uuid: " From e0fb8884c430a2b83f6da3ad2078ee446ca19599 Mon Sep 17 00:00:00 2001 From: syl-p Date: Wed, 8 Sep 2021 14:48:32 +0200 Subject: [PATCH 06/11] feat: translate for subject --- app/mailers/user_mailer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 61c17399..403a18f0 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -2,6 +2,6 @@ class UserMailer < ApplicationMailer def uuid_created @user = params[:user] - mail(to: @user.email, subject: I18n.t 'mail.subject') + mail(to: @user.email, subject: I18n.t('mail.uuid_subject')) end end \ No newline at end of file From 925daee0a9f7e03a6a5e868ff1d9411c84705c81 Mon Sep 17 00:00:00 2001 From: syl-p Date: Thu, 9 Sep 2021 10:25:12 +0200 Subject: [PATCH 07/11] feat: add smtp config and test files --- app/mailers/user_mailer.rb | 12 ++++++------ config/environments/development.rb | 13 +++++++++++++ config/environments/production.rb | 12 ++++++++++++ test/mailers/previews/user_mailer_preview.rb | 4 ++++ test/mailers/user_mailer_test.rb | 7 +++++++ 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 test/mailers/previews/user_mailer_preview.rb create mode 100644 test/mailers/user_mailer_test.rb diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 403a18f0..f72cd42c 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,7 +1,7 @@ class UserMailer < ApplicationMailer - def uuid_created - @user = params[:user] - - mail(to: @user.email, subject: I18n.t('mail.uuid_subject')) - end -end \ No newline at end of file + def uuid_created + @user = params[:user] + + mail(to: @user.email, subject: I18n.t('mail.uuid_subject')) + end +end diff --git a/config/environments/development.rb b/config/environments/development.rb index 61200567..ffc67c8f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -33,6 +33,19 @@ # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false + + config.action_mailer.delivery_method = :smtp + + config.action_mailer.smtp_settings = { + address: ENV['SMTP_ADDRESS'], + port: ENV['SMTP_PORT'], + domain: ENV['SMTP_DOMAIN'], + user_name: ENV['SMTP_USER_NAME'], + password: ENV['SMTP_PASSWORD'], + authentication: ENV['SMTP_AUTHENTICATION'], + enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'] + } + config.action_mailer.perform_caching = false # Print deprecation notices to the Rails logger. diff --git a/config/environments/production.rb b/config/environments/production.rb index 0bf8a168..d00382ab 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -56,6 +56,18 @@ config.action_mailer.perform_caching = false + config.action_mailer.delivery_method = :smtp + + config.action_mailer.smtp_settings = { + address: ENV['SMTP_ADDRESS'], + port: ENV['SMTP_PORT'], + domain: ENV['SMTP_DOMAIN'], + user_name: ENV['SMTP_USER_NAME'], + password: ENV['SMTP_PASSWORD'], + authentication: ENV['SMTP_AUTHENTICATION'], + enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'] + } + # 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 diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb new file mode 100644 index 00000000..957e12b6 --- /dev/null +++ b/test/mailers/previews/user_mailer_preview.rb @@ -0,0 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/user_mailer +class UserMailerPreview < ActionMailer::Preview + +end diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb new file mode 100644 index 00000000..67a1629c --- /dev/null +++ b/test/mailers/user_mailer_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserMailerTest < ActionMailer::TestCase + # test "the truth" do + # assert true + # end +end From e81dcbca07db459b7ead3518cf41bc68ce886181 Mon Sep 17 00:00:00 2001 From: syl-p Date: Thu, 9 Sep 2021 10:29:32 +0200 Subject: [PATCH 08/11] feat: use env var for email from --- app/mailers/application_mailer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b2239..ad08f94c 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["EMAIL_FROM"] layout 'mailer' end From e8bf3e4a936eeabb521b3a0f68b3d7a4b13c00e2 Mon Sep 17 00:00:00 2001 From: syl-p Date: Thu, 9 Sep 2021 11:38:27 +0200 Subject: [PATCH 09/11] fix: update string lang --- config/locales/en.yml | 3 +-- config/locales/fr.yml | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 8766c764..ae337195 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,7 +30,6 @@ # available at http://guides.rubyonrails.org/i18n.html. en: - hello: "Hello world" mail: uuid_subject: "Your PIA account" - uuid_created: "Your pia account was created, your uuid is: " + uuid_created: "Your PIA account was created, your uuid is: " diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b8ffd856..dcea67ae 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -1,5 +1,4 @@ fr: - hello: "Hello world" mail: uuid_subject: "Votre compte PIA" - uuid_created: "Votre compte a été créé, voilà votre uuid: " + uuid_created: "Votre compte PIA a été créé, voilà votre uuid: " From 758028bcbde6903e8b5064c8f3269d9155c61a01 Mon Sep 17 00:00:00 2001 From: syl-p Date: Thu, 9 Sep 2021 13:50:39 +0200 Subject: [PATCH 10/11] fix: pass env var to credentials --- app/mailers/application_mailer.rb | 2 +- config/environments/development.rb | 14 +++++++------- config/environments/production.rb | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index ad08f94c..6b360268 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: ENV["EMAIL_FROM"] + default from: Rails.application.credentials.email_from layout 'mailer' end diff --git a/config/environments/development.rb b/config/environments/development.rb index ffc67c8f..8c108176 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -37,13 +37,13 @@ config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { - address: ENV['SMTP_ADDRESS'], - port: ENV['SMTP_PORT'], - domain: ENV['SMTP_DOMAIN'], - user_name: ENV['SMTP_USER_NAME'], - password: ENV['SMTP_PASSWORD'], - authentication: ENV['SMTP_AUTHENTICATION'], - enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'] + address: Rails.application.credentials.smtp_address, + port: Rails.application.credentials.smtp_port, + domain: Rails.application.credentials.smtp_domain, + user_name: Rails.application.credentials.smtp_user_name, + password: Rails.application.credentials.smtp_password, + authentication: Rails.application.credentials.smtp_authentication, + enable_starttls_auto: Rails.application.credentials.smtp_enable_starttls_auto } config.action_mailer.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index d00382ab..ea9ec61a 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -59,13 +59,13 @@ config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { - address: ENV['SMTP_ADDRESS'], - port: ENV['SMTP_PORT'], - domain: ENV['SMTP_DOMAIN'], - user_name: ENV['SMTP_USER_NAME'], - password: ENV['SMTP_PASSWORD'], - authentication: ENV['SMTP_AUTHENTICATION'], - enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'] + address: Rails.application.credentials.smtp_address, + port: Rails.application.credentials.smtp_port, + domain: Rails.application.credentials.smtp_domain, + user_name: Rails.application.credentials.smtp_user_name, + password: Rails.application.credentials.smtp_password, + authentication: Rails.application.credentials.smtp_authentication, + enable_starttls_auto: Rails.application.credentials.smtp_enable_starttls_auto } # Ignore bad email addresses and do not raise email delivery errors. From a20325890c9b112c0e14555823d6b76864e9c87c Mon Sep 17 00:00:00 2001 From: syl-p Date: Thu, 9 Sep 2021 14:17:06 +0200 Subject: [PATCH 11/11] feat: pass smtp config to mailer.rb --- config/environments/development.rb | 10 ---------- config/environments/production.rb | 10 ---------- config/initializers/mailer.rb | 9 +++++++++ 3 files changed, 9 insertions(+), 20 deletions(-) create mode 100644 config/initializers/mailer.rb diff --git a/config/environments/development.rb b/config/environments/development.rb index 8c108176..acf917fc 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -35,16 +35,6 @@ config.action_mailer.delivery_method = :smtp - - config.action_mailer.smtp_settings = { - address: Rails.application.credentials.smtp_address, - port: Rails.application.credentials.smtp_port, - domain: Rails.application.credentials.smtp_domain, - user_name: Rails.application.credentials.smtp_user_name, - password: Rails.application.credentials.smtp_password, - authentication: Rails.application.credentials.smtp_authentication, - enable_starttls_auto: Rails.application.credentials.smtp_enable_starttls_auto - } config.action_mailer.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index ea9ec61a..5bab0857 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -57,16 +57,6 @@ config.action_mailer.perform_caching = false config.action_mailer.delivery_method = :smtp - - config.action_mailer.smtp_settings = { - address: Rails.application.credentials.smtp_address, - port: Rails.application.credentials.smtp_port, - domain: Rails.application.credentials.smtp_domain, - user_name: Rails.application.credentials.smtp_user_name, - password: Rails.application.credentials.smtp_password, - authentication: Rails.application.credentials.smtp_authentication, - enable_starttls_auto: Rails.application.credentials.smtp_enable_starttls_auto - } # 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. diff --git a/config/initializers/mailer.rb b/config/initializers/mailer.rb new file mode 100644 index 00000000..b5b191e8 --- /dev/null +++ b/config/initializers/mailer.rb @@ -0,0 +1,9 @@ +ActionMailer::Base.smtp_settings = { + address: Rails.application.credentials.smtp_address, + port: Rails.application.credentials.smtp_port, + domain: Rails.application.credentials.smtp_domain, + user_name: Rails.application.credentials.smtp_user_name, + password: Rails.application.credentials.smtp_password, + authentication: Rails.application.credentials.smtp_authentication, + enable_starttls_auto: Rails.application.credentials.smtp_enable_starttls_auto +} \ No newline at end of file