I am new to Rails and trying to get a example working to register with a confirmation email with Rails 4 and devise. I am using this example:
https://github.com/mwlang/lazy_registration_demos
I created the following files:
initialisers/setup_mail.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "[email protected]",
:password => "passwork",
:authentication => "plain",
:enable_starttls_auto => true
}
/app/mailers/welcome_email.rb
class WelcomeMailer < ActionMailer::Base
def registration_confirmation(user)
mail :to => user, :from => "[email protected]", :subject => "Subject line"
end
end
/devise/mailer/confirmations_instructions.html.erb
<p>
Welcome #{@email}!
</p>
<p>You can confirm your account email through the link below:</p>
<p>
<%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>
</p>
confirmations_controller.rb
class ConfirmationsController < Devise::ConfirmationsController
# Remove the first skip_before_filter (:require_no_authentication) if you
# don't want to enable logged users to access the confirmation page.
skip_before_filter :require_no_authentication
skip_before_filter :authenticate_user!, except: [:confirm_user]
def update
with_unconfirmed_confirmable do
if confirmable_user.blank_password?
confirmable_user.update_password(params[:user])
if confirmable_user.valid?
do_confirm
else
do_show
confirmable_user.errors.clear # prevent rendering :new
end
else
self.class.add_error_on(self, :email, :password_allready_set)
end
end
render 'devise/confirmations/new' unless confirmable_user.errors.empty?
end
def confirm_user
if confirmation_token && (@confirmable_user = User.find_by(:confirmation_token => confirmation_token))
do_show
else
flash[:error] = "Invalid confirmation token"
redirect_to :unconfirmed
end
end
def show
with_unconfirmed_confirmable do
confirmable_user.blank_password? ? do_show : do_confirm
end
unless confirmable_user.errors.empty?
self.resource = confirmable_user
render 'devise/confirmations/new'
end
end
protected
def confirmation_token
@confirmation_token ||= params["user"] && params["user"]["confirmation_token"] || params["confirmation_token"]
end
def confirmable_user
@confirmable_user ||= User.find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
end
def with_unconfirmed_confirmable
unless confirmable_user.new_record?
confirmable_user.only_if_unconfirmed {yield}
end
end
def do_show
self.resource = confirmable_user
render 'devise/confirmations/show'
end
def do_confirm
confirmable_user.confirm!
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, confirmable_user)
end
end
/devise/registrations/new.html.haml
%h2 Sign up
= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= devise_error_messages!
%div
= f.label :email
= f.email_field :email, :autofocus => true
%div{style: "margin-top: 25px"}= f.submit "Sign up", class: "btn btn-primary btn-large"
%hr
= render "devise/shared/links"
To trigger the email to be send I need to add
WelcomeMailer.registration_confirmation(@user).deliver
But I have no clue where I need to add this trigger. Do I need to do this in the controller? Or in the view?
Thanks a lot
Managed to fix this issue myself using mailcatcher
Steps to fix:
add lines to /environments/development.rb
config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
run mailcatcher
ignore all other code above, just needed a night of sleep :)