From 469d757623f06dfce3d4889556f31835b3a3ced5 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Wed, 21 Sep 2022 16:55:29 +0200 Subject: [PATCH 1/2] configure smtp for emails --- .env.example | 3 +++ app/controllers/rents_controller.rb | 1 + app/mailers/rent_mailer.rb | 6 ++++++ app/views/rent_mailer/create_email.html.erb | 7 +++++++ config/application.rb | 12 ++++++++++++ test/mailers/previews/rent_mailer_preview.rb | 7 +++++++ test/mailers/rent_mailer_test.rb | 7 +++++++ 7 files changed, 43 insertions(+) create mode 100644 app/mailers/rent_mailer.rb create mode 100644 app/views/rent_mailer/create_email.html.erb create mode 100644 test/mailers/previews/rent_mailer_preview.rb create mode 100644 test/mailers/rent_mailer_test.rb diff --git a/.env.example b/.env.example index 67cc0c9..89fb089 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,6 @@ DATABASE_USER=postgres DATABASE_PASSWORD=postgres DATABASE_HOST=localhost DATABASE_PORT=5432 + +GMAIL_SMTP_USER_NAME= +GMAIL_SMTP_APP_PASSWORD= \ No newline at end of file diff --git a/app/controllers/rents_controller.rb b/app/controllers/rents_controller.rb index bd57a49..77b9c5d 100644 --- a/app/controllers/rents_controller.rb +++ b/app/controllers/rents_controller.rb @@ -31,6 +31,7 @@ def create respond_to do |format| if @rent.save + RentMailer.with(rent: @rent).create_email.deliver_later format.html { redirect_to users_rents_url, notice: "Sikeres kölcsönzés." } format.json { render :show, status: :created, location: @rent } else diff --git a/app/mailers/rent_mailer.rb b/app/mailers/rent_mailer.rb new file mode 100644 index 0000000..4bba505 --- /dev/null +++ b/app/mailers/rent_mailer.rb @@ -0,0 +1,6 @@ +class RentMailer < ApplicationMailer + def create_email + @rent = params[:rent] + mail(to:'sepsi.laszlo99@gmail.com', subject: 'Schörpong mail') + end +end diff --git a/app/views/rent_mailer/create_email.html.erb b/app/views/rent_mailer/create_email.html.erb new file mode 100644 index 0000000..118a1de --- /dev/null +++ b/app/views/rent_mailer/create_email.html.erb @@ -0,0 +1,7 @@ +

Új feldolgozatlan eszköz igénylés

+

Az igénylés részlete:

+ diff --git a/config/application.rb b/config/application.rb index 5652589..0336a36 100644 --- a/config/application.rb +++ b/config/application.rb @@ -12,6 +12,18 @@ class Application < Rails::Application config.load_defaults 5.2 config.i18n.default_locale = :hu + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { + address: 'smtp.gmail.com', + port: 587, + domain: 'example.com', + user_name: ENV['GMAIL_SMTP_USER_NAME'], + password: ENV['GMAIL_SMTP_APP_PASSWORD'], + authentication: 'plain', + enable_starttls_auto: true, + open_timeout: 5, + read_timeout: 5 } + # Configuration for the application, engines, and railties goes here. # # These settings can be overridden in specific environments using the files diff --git a/test/mailers/previews/rent_mailer_preview.rb b/test/mailers/previews/rent_mailer_preview.rb new file mode 100644 index 0000000..0a71f39 --- /dev/null +++ b/test/mailers/previews/rent_mailer_preview.rb @@ -0,0 +1,7 @@ +# Preview all emails at http://localhost:3000/rails/mailers/rent_mailer +class RentMailerPreview < ActionMailer::Preview + def create_email + @rent = Rent.last + RentMailer.with(rent: @rent).create_email + end +end diff --git a/test/mailers/rent_mailer_test.rb b/test/mailers/rent_mailer_test.rb new file mode 100644 index 0000000..2077617 --- /dev/null +++ b/test/mailers/rent_mailer_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class RentMailerTest < ActionMailer::TestCase + # test "the truth" do + # assert true + # end +end From 2ef2906157e01e1aa7cd3da08deccab6a1dc2ac3 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Thu, 20 Oct 2022 10:27:10 +0200 Subject: [PATCH 2/2] update email template --- app/mailers/previews/rent_mailer_preview.rb | 6 +++ app/views/rent_mailer/create_email.html.erb | 51 ++++++++++++++++++--- config/application.rb | 2 +- config/environments/production.rb | 1 + 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 app/mailers/previews/rent_mailer_preview.rb diff --git a/app/mailers/previews/rent_mailer_preview.rb b/app/mailers/previews/rent_mailer_preview.rb new file mode 100644 index 0000000..6db03b7 --- /dev/null +++ b/app/mailers/previews/rent_mailer_preview.rb @@ -0,0 +1,6 @@ +class RentMailerPreview < ActionMailer::Preview + def create_email + @rent = params[:rent] + mail(to: User.first.mail, subject: 'Schörpong mail') + end +end diff --git a/app/views/rent_mailer/create_email.html.erb b/app/views/rent_mailer/create_email.html.erb index 118a1de..0462bdf 100644 --- a/app/views/rent_mailer/create_email.html.erb +++ b/app/views/rent_mailer/create_email.html.erb @@ -1,7 +1,44 @@ -

Új feldolgozatlan eszköz igénylés

-

Az igénylés részlete:

- + + + + + + + +
+
+
+
+

Új feldolgozatlan eszközigénylés

+
+
+
+
+

Eszköz: <%= @rent.item.name %>

+
+
+
+
+

Kölcsönzés kezdete:

+
+
+

Kölcsönzés vége:

+
+
+
+
+

<%= @rent.begin.strftime("%Y-%m-%d %H:%M") %>

+
+
+

<%= @rent.end.strftime("%Y-%m-%d %H:%M") %>

+
+
+
+
+ <%= link_to "Részletek", rents_url, class:"ui red huge button" %> +
+
+
+
+ + diff --git a/config/application.rb b/config/application.rb index 0336a36..4918248 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,7 +11,7 @@ class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 5.2 config.i18n.default_locale = :hu - + routes.default_url_options[:host] = 'localhost:3001' config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', diff --git a/config/environments/production.rb b/config/environments/production.rb index 5a067d5..a6ebb37 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -2,6 +2,7 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. + routes.default_url_options[:host] = 'https://schorpong.sch.bme.hu/' # Code is not reloaded between requests. config.cache_classes = true