Skip to content

stasandrichuk/Email-Confirmation-with-Devise

Repository files navigation

Confirm your account email through devise mailer
========

This application was generated with the rails_apps_composer gem:
https://github.com/RailsApps/rails_apps_composer
provided by the RailsApps Project:
http://railsapps.github.io/


Session Expired Time Settings:

In order to set expiration time to 1 hour, add ‘devise :timeoutable, :timeout_in => 1.hour’ to ‘app/models/user.rb’


Add A Custom Mailer

First make the file controllers/mailers/user_mailer.rb and insert the following code:

class UserMailer < Devise::Mailer 
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def confirmation_instructions(record, token, opts={})
    opts[:from] = 'my_custom_from@domain.com'
    # opts[:reply_to] = 'my_custom_from@domain.com'
    super
  end
end

Let devise Get UserMailer:

Open config/initializers/devise.rb and add the following code:

config.mailer = 'UserMailer'

Make An Email Form For Confirmation

Make the file app/views/devise/mailer/confirmation_instructions.html.erb and insert the following code:

<p>Hi <%= @user.name %></p>

<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: @token) %></p>

SMTP Settings:

configure settings like the bellowings in config/environments/development.rb:

config.action_mailer.smtp_settings = {
  address: "127.0.0.1",
  port: 1025,
  domain: Rails.application.secrets.domain_name,
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: Rails.application.secrets.email_provider_username,
  password: Rails.application.secrets.email_provider_password
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => '127.0.0.1:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true

Setup SMTP Server:

To test the setup in development install the mailcatcher gem, that you will use as a SMTP server in development, catching all incoming mails and displaying them on http://localhost:1080/:

gem install mailcatcher

Once installed start the mailcatcher server with the command:

mailcatcher

A toy SMTP server will be running on port 1025 catching emails and displaing them on HTTP port 1080.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors