Commit d5e3c08a by Tan Phat Nguyen

Send email when client order once.

parent 29f02cb8
class OrdersController < ApplicationController class OrdersController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!, only: [:show, :edit, :update, :destroy, :index]
before_action :set_order, only: [:show, :edit, :update, :destroy] before_action :set_order, only: [:show, :edit, :update, :destroy]
def index def index
...@@ -25,6 +25,7 @@ class OrdersController < ApplicationController ...@@ -25,6 +25,7 @@ class OrdersController < ApplicationController
if @order.save if @order.save
current_cart.destroy current_cart.destroy
session[:cart_id] = nil session[:cart_id] = nil
Notifier.order_received(@order).deliver
flash[:success] = 'Successfully. Thank you for your order.' flash[:success] = 'Successfully. Thank you for your order.'
redirect_to root_path redirect_to root_path
else else
......
class Notifier < ActionMailer::Base
  • @phatnt I think you should specify name more clearly. mailer class should be 'Order' and method should be 'confirm'

    Edited
Please register or sign in to reply
default from: 'bigforest20@gmail.com'
def order_received(order)
@order = order
mail to: order.email, subject: 'Venshop Store Order Confirmation'
end
end
\ No newline at end of file
<p>Dear <b><%= @order.name %></b>
<br/>
<div>Thank you for your recent order from The Venshop Store.</div>
<br/>
<div>You ordered the following items:</div>
<br/>
<table style='border: 1px solid; border-collapse: collapse;'>
<tr>
<th style='border: 1px solid;'>Title</th>
<th style='border: 1px solid;'>Category</th>
<th style='border: 1px solid;'>Quantity/Price</th>
<th style='border: 1px solid;'>Price Item</th>
</tr>
<%= render @order.line_items %>
<tr>
<td colspan='3' style='border: 1px solid;'>Total</td>
<td style='border: 1px solid;'>$<%= @order.total_price %></td>
</tr>
</table>
<br/>
<div>We'll send you a separate e-mail when your order ships.</div>
Dear <%= @order.name %>
Thank you for your recent order from The Venshop Store.
You ordered the following items:
We'll send you a separate e-mail when your order ships.
...@@ -15,6 +15,17 @@ Rails.application.configure do ...@@ -15,6 +15,17 @@ Rails.application.configure do
# Don't care if the mailer can't send. # Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
authentication: 'plain',
user_name: ENV['pusher_email_user'],
Please register or sign in to reply
password: ENV['pusher_email_password'],
enable_starttls_auto: true
}
# Print deprecation notices to the Rails logger. # Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log config.active_support.deprecation = :log
......
require 'test_helper'
class AmazonHelperTest < ActionView::TestCase
end
require 'test_helper'
class NotifierTest < ActionMailer::TestCase
test "order_received" do
mail = Notifier.order_received
assert_equal "Order received", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end
end
# Preview all emails at http://localhost:3000/rails/mailers/notifier
class NotifierPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/notifier/order_received
def order_received
Notifier.order_received
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment