I have created a rails app for booking cabs online. I want my app to have the functionality of sending emails to users once their booking is confirmed. For this, I am following Ryan Bates railscasts:
https://www.youtube.com/watch?v=v1KI571TdUQ
I started with creating a setup_mail.rb file in app/config/initializers, with the following content:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "railscasts.com",
:user_name => "taxibol",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
I then generated a mailer named user_mailer and included the following in app/mailers/user_mailer.rb:
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
and the following in app/views/user_mailer/registration_confirmation.text.erb:
Welcome to example.com!
===============================================
You have successfully signed up to taxibol.in.
Thanks for joining and have a great day!
Finally, in my static_pages_controller.rb, I included the following:
class StaticPagesController < ApplicationController
def booking_confirmation
UserMailer.registration_confirmation(current_user).deliver
end
end
I tested this in my localhost env, and it seems to have worked. Following is the relevant section in
Started GET "/booking/confirmation" for 127.0.0.1 at 2014-12-13 08:30:14 -0800
Processing by StaticPagesController#booking_confirmation as HTML
←[1m←[35mUser Load (2.5ms)←[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Rendered user_mailer/registration_confirmation.text.erb (0.0ms)
UserMailer#registration_confirmation: processed outbound mail in 10.0ms
Sent mail to [email protected] (2037.5ms)
Date: Sat, 13 Dec 2014 08:30:14 -0800
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Welcome to example.com!
===============================================
You have successfully signed up to taxibol.in.
Thanks for joining and have a great day!
However, I checked my gmail account ([email protected]), but I didn't receive any mail. What could be the reason?
Update:
Instead of creating the setup_mail.rb file, I tried to include the following in config/environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = {host: 'localhost', port: 3000}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: "587",
domain: "gmail.com"
authentication: "plain",
enable_starttls_auto: true,
user_name: "[email protected]"
password: "some_password"
}
Again got the same log trails, but no mail yet :(
gmail
id inuser_name
– Nithinmailcatcher
first. – D-side