What Will I Learn?
- You will learn how to send emails in Ruby on Rails Project
- You will learn how to make forgot password using devise
- You will learn how we setup mail clients with SMTP connection
- You will learn the configuration needed for sending the emails in Ruby on Rails Project
Requirements
- OS: Ubuntu/Mac OS
- A text editor like sublime
- Basic understanding about Ruby on Rails
- Email account of Gmail
- And for better understanding must read my part 1 of this tutorial (link added in curriculum)
Difficulty
- Intermediate
Tutorial Contents
Hello and welcome to the 2nd part of the tutorial. In this tutorial, we are going to make configurations in our app to send emails from app and we also make forgot password functionality using devise. So let's start the tutorial.
- First of all run rails server in the project we make in part 1 of this tutorial.
- we know how to generate the devise views as we discussed in the previuos tutorial.
- Devise by defaut gave us the forgot password page but we can overwrite it, for that go to your app in the editor > views > users > password > new
- You will get this form:
<h2>Forgot your password?</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="actions">
<%= f.submit "Send me reset password instructions" %>
</div>
<% end %>
- Now open your localhost:3000/user/password/new in your url to check forgot password is working or not. You will get a error like the below image because by default hostname is set to blank in devise.
So lets setup our host in rails app to make forgot password functionality working in our app.
Step-1
- Open gmail account
- Click Settings button which is present at the tip right corner
- Click the Forwarding and POP/IMAP tab
- In the "IMAP Access" section, select Enable IMAP
- Click Save Changes
Step-2
- Add a file under mailer and name it application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'your email address'
layout 'mailer'
end
- Save this file
Step-3
- Open rails application in the editor (sublime), if you have setup the app please follow the previous tutorial.
- Then go to our environment folder then development.rb file because we are on the local system thats why we configure only in the developement file, when we will going live then production.rb will be changed till then do not change anything in this file
- Next is copy the previous code under the Rails.application.configure do
# Email
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = {host: 'localhost', port:3000}
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
enable_starttls_auto: true,
user_name: ‘your gmail address here,
password: ‘your gmail password here,
authentication: :plain,
domain: ‘domain’
}
Domain: The domain address could be anything, its your company domain or nay personal domain, if you haven’t any then remove this line.
Step-4
- Now change the devise mailer sender address with the below code at the devise.rb file:
config.mailer_sender = ‘abc@gmail.com'
- Now save this configuration and start your server
rails s
- Now our app is ready to send the email to the users
- The default mailing address will be your email address
- Now go the below url
http://localhost:3000//users/password/new
Entered your registered email and submit the form, as shown below
Now the email will be send. You can check the logs in the terminal whether email is sent or not. Or you can also check your sent mails
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors