Skip to content

Commit

Permalink
Merge pull request #28 from OmarMWarraich/27-send-emails-w-mandrill
Browse files Browse the repository at this point in the history
27 send emails w mandrill
  • Loading branch information
OmarMWarraich authored Oct 17, 2024
2 parents 20ccaae + d51adf7 commit 16aebad
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ gem 'will_paginate', '~> 4.0', '>= 4.0.1'
gem 'carrierwave', '~> 3.0', '>= 3.0.7'

gem 'annotate', '~> 3.2'
gem 'MailchimpTransactional', '~> 1.0', '>= 1.0.59'
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
GEM
remote: https://rubygems.org/
specs:
MailchimpTransactional (1.0.59)
excon (>= 0.76.0, < 1)
json (~> 2.1, >= 2.1.0)
actioncable (7.0.4)
actionpack (= 7.0.4)
activesupport (= 7.0.4)
Expand Down Expand Up @@ -127,6 +130,7 @@ GEM
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.12.0)
excon (0.112.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
Expand Down Expand Up @@ -317,6 +321,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
MailchimpTransactional (~> 1.0, >= 1.0.59)
annotate (~> 3.2)
better_errors (~> 2.10, >= 2.10.1)
binding_of_caller (~> 1.0, >= 1.0.1)
Expand Down
34 changes: 34 additions & 0 deletions app/mailers/admin_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class AdminMailer < ApplicationMailer
default from: 'no-reply@topgeardirect.com'

def new_user(user)
@user = user
mail(to: @user.email, subject: "New User: #{user.email}")
end

def mandrill_client
@mandrill_client ||= MailchimpTransactional::Client.new Rails.application.credentials.dig(:smtp, :api_key)
end

def new_comment(comment)
template_name = 'new-comment'
template_content = []
message = {
to: [{ email: 'omar@topgeardirect.com' }],
subject: "New Comment Added: #{comment.text}",
merge_vars: [
{
rcpt: 'omar@topgeardirect.com',
vars: [
{
name: 'COMMENT_NAME',
content: comment.text
}
]
}
]
}

mandrill_client.messages.send_template template_name, template_content, message
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class User < ApplicationRecord
extend FriendlyId
friendly_id :name, use: %i[slugged history finders]

after_create :send_welcome_email

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, foreign_key: :author_id
Expand All @@ -48,4 +50,8 @@ def recent_three_posts
def is?(requested_role)
role == requested_role.to_s
end

def send_welcome_email
AdminMailer.new_user(self).deliver
end
end
6 changes: 6 additions & 0 deletions app/views/admin_mailer/new_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Welcome, <%= @user.email %>!</h1>
<p>Dear <%= @user.name %>,</p>
<p>We are excited to have you join our community. Thank you for signing up!</p>
<p>If you have any questions, feel free to reach out to us at support@topgeardirect.com.</p>
<p>Best regards,</p>
<p>The Team</p>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</head>

<body>
<%= render "shared/navbar" %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% if user_signed_in? %>
<li><%= link_to 'Sign out', destroy_user_session_path, data: { turbo_method: :delete } %></li>
<% end %>
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
B5s7+BYCscIXqQowLIC6T5A8XurUUO8fcLm3TPKqy7dmpjp731LbtPfRQ4qWmGQP8ltNJxsiyxSbvE1Qsr5BXz5K0PtS9hIgxtIe8H0m4eNYGlOXldSljmWgRoh9+CmS9SP51VptFlnKZXAI6IuRb6/7Ut0tG5UjqWzPR8JfJB/10xfDgeZA86MDa5vFo2BohaqzZKWpxImNiPsBgr1yFhnA4yhRqc1/uiUjrW7IKny7puI90q9f16OrMPJ4Mh2rVJYUhQc38Ey81wUGf4e5edeCYCbxFbkGghCQ68LJgElSWyv2z8Pc0X3aE3ITZkezZga7pKfz4fe+VXDsdUjb8+iFFeWpjOYXCYAjeMqQZoPLoqujShdynIRnKso8wKWZ4OfOU8PgnnZvHbSw5dXPpimEhWXAgW+G1tYc--jBT5JswiAfe1MlW9--YP2xDZrbT8C4DjHFqD3uiw==
C6JJYFRlSXWEe6EZHIPAJDh4qIE0MJ1VOc+sAaq0vJTAKe3BdpADENlPPx7X9FqT8Q63/ujGblE4NPsq1LSV4fGRpgQtRcrZ+QT7kjw8GQyvkIPMSCD8u2Uh4PUzrTNYX31w105Ob0f4Ngh8U0gCIW0Xsf4+d3VaACLGav43lYU9eR4cA4AHOChQyIWpo7Yd1zagaQqBNR+NMvcFmf6DUmnW4qwLF2bh6PlMfaFV0pS8BTcEq7OIfYbm+S0p3r3lJNz+tkt79rhx+gJGhfJ0IBrii0d6G+7da4WAO9wBEyL5yJtSYFd3XPvGtYleIS0eDKHLXCitAbphUHNUh7+9DM/Tr0b0nioYcLYKzDlJDSXHwTY6cdBsJV1OLqffRULFa9PFq9SgsdXL7MJKwX0x+uTxoUeqgzlY7P4kBT8ZI0IHnxXZLAoHFJT9iqNNsEX5HSsScWLDKeqTWOu8DpPtbzqkoRQ6EL4TxtreGhrByRChQY/uobOdKKs7MzEJrb/IGzSuAy2np9Krm0e7PdV9Qk/yQnG6g0vQ7UgY/QIN0GomiWe0OjNKD/B1m+j379OpN5T77Ebva1vH3VIjenKLzcoqQyv/wkRpNQifIA==--zmXR3nXkpekjs4eT--AmeABzUYkvSaNJDHpg69Vw==
18 changes: 15 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@
config.active_storage.service = :local

# Set up mailer for development.
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
address: Rails.application.credentials.dig(:smtp, :address),
port: Rails.application.credentials.dig(:smtp, :port),
domain: Rails.application.credentials.dig(:smtp, :domain),
user_name: Rails.application.credentials.dig(:smtp, :username),
password: Rails.application.credentials.dig(:smtp, :api_key),
authentication: "login",
enable_starttls_auto: true
}

config.action_mailer.default charset: "utf-8"

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

# Set up default URL options for the Devise mailer in each environment.
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
5 changes: 3 additions & 2 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'contact@ovatec.com'
config.mailer_sender = 'Omar at Owatec <omar@topgeardirect.com>'
config.parent_mailer = 'ActionMailer::Base'

# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
Expand Down Expand Up @@ -263,7 +264,7 @@
# should add them to the navigational formats lists.
#
# The "*/*" below is required to match Internet Explorer requests.
# config.navigational_formats = ['*/*', :html]
config.navigational_formats = ['*/*', :html, :turbo_stream]

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
resources :likes, only: [:create]
end
end

root "users#index"
end
5 changes: 5 additions & 0 deletions spec/mailers/admin_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe AdminMailer, type: :mailer do
pending "add some examples to (or delete) #{__FILE__}"
end
4 changes: 4 additions & 0 deletions spec/mailers/previews/admin_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer
class AdminMailerPreview < ActionMailer::Preview

end

0 comments on commit 16aebad

Please sign in to comment.