Skip to content

Commit eb956ac

Browse files
committed
Mailers
1 parent c1063e5 commit eb956ac

File tree

25 files changed

+4158
-67
lines changed

25 files changed

+4158
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
/yarn-error.log
3535
yarn-debug.log*
3636
.yarn-integrity
37+
sendgrid.env

.idea/elaka.iml

Lines changed: 199 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 23 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ gem 'paper_trail'
4949
gem 'paper_trail-association_tracking'
5050
gem "recaptcha"
5151
gem 'jquery-rails'
52+
gem 'mailgun-ruby', '~>1.2.3'
5253

Gemfile.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ GEM
100100
railties (>= 4.1.0)
101101
responders
102102
warden (~> 1.2.3)
103+
domain_name (0.5.20190701)
104+
unf (>= 0.0.5, < 1.0.0)
103105
erubi (1.10.0)
104106
ffi (1.14.2)
105107
friendly_id (5.4.2)
@@ -109,6 +111,9 @@ GEM
109111
haml (5.2.1)
110112
temple (>= 0.8.0)
111113
tilt
114+
http-accept (1.7.0)
115+
http-cookie (1.0.3)
116+
domain_name (~> 0.5)
112117
i18n (1.8.9)
113118
concurrent-ruby (~> 1.0)
114119
jbuilder (2.11.2)
@@ -140,14 +145,20 @@ GEM
140145
nokogiri (>= 1.5.9)
141146
mail (2.7.1)
142147
mini_mime (>= 0.1.1)
148+
mailgun-ruby (1.2.3)
149+
rest-client (>= 2.0.2)
143150
marcel (0.3.3)
144151
mimemagic (~> 0.3.2)
145152
method_source (1.0.0)
153+
mime-types (3.3.1)
154+
mime-types-data (~> 3.2015)
155+
mime-types-data (3.2021.0225)
146156
mimemagic (0.3.5)
147157
mini_mime (1.0.2)
148158
minitest (5.14.4)
149159
msgpack (1.4.2)
150160
nested_form (0.3.2)
161+
netrc (0.11.0)
151162
nio4r (2.5.5)
152163
nokogiri (1.11.1-x86_64-darwin)
153164
racc (~> 1.4)
@@ -224,6 +235,11 @@ GEM
224235
responders (3.0.1)
225236
actionpack (>= 5.0)
226237
railties (>= 5.0)
238+
rest-client (2.1.0)
239+
http-accept (>= 1.7.0, < 2.0)
240+
http-cookie (>= 1.0.2, < 2.0)
241+
mime-types (>= 1.16, < 4.0)
242+
netrc (~> 0.8)
227243
rubyzip (2.3.0)
228244
sass-rails (6.0.0)
229245
sassc-rails (~> 2.1, >= 2.1.1)
@@ -255,6 +271,9 @@ GEM
255271
turbolinks-source (5.2.0)
256272
tzinfo (2.0.4)
257273
concurrent-ruby (~> 1.0)
274+
unf (0.1.4)
275+
unf_ext
276+
unf_ext (0.0.7.7)
258277
warden (1.2.9)
259278
rack (>= 2.0.9)
260279
web-console (4.1.0)
@@ -294,6 +313,7 @@ DEPENDENCIES
294313
jquery-rails
295314
kaminari
296315
listen (~> 3.3)
316+
mailgun-ruby (~> 1.2.3)
297317
paper_trail
298318
paper_trail-association_tracking
299319
pg (~> 1.2, >= 1.2.3)

app/controllers/application_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def devise_mapping
3131

3232
# https://dev.to/morinoko/adding-recaptcha-v3-to-a-rails-app-without-a-gem-46jj
3333
def verify_recaptcha?(token, recaptcha_action)
34-
secret_key = Rails.application.credentials.dig(:recaptcha_secret_key)
35-
36-
uri = URI.parse("https://www.google.com/recaptcha/api/siteverify?secret=#{secret_key}&response=#{token}")
34+
uri = URI.parse("https://www.google.com/recaptcha/api/siteverify?secret=#{ ENV["RECAPTCHA_SECRET_KEY"] }&response=#{token}")
3735
response = Net::HTTP.get_response(uri)
3836
json = JSON.parse(response.body)
3937
json['success'] && json['score'] > RECAPTCHA_MINIMUM_SCORE && json['action'] == recaptcha_action
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MessagesController < ApplicationController
2+
def new
3+
@message = Message.new
4+
end
5+
6+
def create
7+
@message = Message.new message_params
8+
9+
unless verify_recaptcha?(params[:recaptcha_token], 'message')
10+
flash.now[:error] = t('recaptcha.errors.verification_failed')
11+
return render :new
12+
end
13+
14+
if @message.valid?
15+
MessageMailer.contact(@message).deliver_now
16+
redirect_to new_message_url
17+
flash[:notice] = "We have received your message and will be in touch soon!"
18+
else
19+
flash[:error] = "There was an error sending your message. Please try again."
20+
render :new
21+
end
22+
end
23+
24+
private
25+
26+
def message_params
27+
params.require(:message).permit(:name, :email, :subject, :content)
28+
end
29+
end

app/javascript/packs/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ ActiveStorage.start()
2020
//= require bootstrap-select
2121
//= require_tree .
2222

23+

app/mailers/message_mailer.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class MessageMailer < ApplicationMailer
2+
require 'mailgun-ruby'
3+
4+
def contact(message)
5+
mg_client = Mailgun::Client.new ENV["MAILGUN_API_KEY"]
6+
message_params = {:from => message.email,
7+
:to => ENV["DEFAULT_EMAIL"],
8+
:subject => 'Elaka Form: ' + message.subject ,
9+
:text => message.content}
10+
mg_client.send_message ENV["MAILGUN_DOMAIN"], message_params
11+
end
12+
end

app/models/message.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Message
2+
include ActiveModel::Model
3+
attr_accessor :name, :email, :subject, :content
4+
validates :name, :email, :subject, :content, presence: { message: "can't be empty"}
5+
end

0 commit comments

Comments
 (0)