Skip to content

fatmahussein/actionmailer-devise-auth

Repository files navigation

Action Mailer in Devise Authentication

📗 Table of Contents

📖 Action Mailer in Devise Auth

This project is all about authentication using devise and using action mailer to send outbond emails from your Rails application eg, forgot password, email confirmation

🛠 Built With

Tech Stack

Key Features

Key features of the application.

  • Devise installation
  • Creating a devise User
  • Adding action mailer in development.rb file
  • Generating app name and password
  • Encrypting email and password in Rails application credential
  • Accessing the email and password in the mailer configuration

(back to top)

💻 Getting Started

To get a local copy up and running, follow these steps.

Prerequisites

To run this project you need:

  • Code Editor.
  • Ruby On Rails.

Setup

Clone this repository to your desired folder:

I am using Ruby -v 3.3.4 and Rails -v 7.2.1

  cd my-folder
  gh repo clone https://github.com/fatmahussein/rails-auth-skeleton.git

Install

In your gmail account -> manage account -> security -> search 'app password', create an app name, and copy the generated password, you will use it.

Setting up devise authentication

Add gem 'devise', '~> 4.9.2'

bundle install

Run the following code:

$ rails generate devise:install, follow instructions provided

$ rails generate devise User

uncomment

    t.string   :confirmation_token`
    t.datetime :confirmed_at
    t.datetime :confirmation_sent_at
    t.string   :unconfirmed_email

in the devise_create_user migration file

Add :confirmable to the User model

$ rails db:migrate

(back to top)

Adding action mailer to development.rb

In your development.rb file, add the following settings:

config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:         'smtp.gmail.com',
  port:            587,
  domain:          'example.com',
  user_name:       'your email',
  password:        'your password',
  authentication:  'plain',
  enable_starttls: true,
  open_timeout:    5,
  read_timeout:    5 }

Please change this to be true in your file

config.action_mailer.raise_delivery_errors = true

Your Rails application should now be able to send outbound emails.

Securing your email and password

Run $env:EDITOR="code --wait"; rails credentials:edit in your code editor, an editor will pop up.

Add

email:
  username: 'your email'
  password: 'your password'

to the editor and save. We will use Rails.application.credentials.email[:username] to access the encrypted data.

Your action mailer config should now look like this:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:         'smtp.gmail.com',
  port:            587,
  domain:          'example.com',
  user_name:       Rails.application.credentials.email[:username],
  password:        Rails.application.credentials.email[:password],
  authentication:  'plain',
  enable_starttls: true,
  open_timeout:    5,
  read_timeout:    5 }

Your data is now encrypted and rails is sending emails successfully :)

Finally, in case it's still not working,

run Rails.application.credentials.email[:username] in your rails console to confirm if it's displaying Nil or your email address.

All the best.

👥 Author

👤 Fatuma Hussein

🔭 Future Features

  • Skeleton to kickstart any RoR project

(back to top)

🤝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the Issues.

(back to top)

⭐️ Show your support

If you like this project, show your support by giving the project a ⭐️.

(back to top)

🙏 Acknowledgments

I would like to thank Microverse.

(back to top)

📝 License

This project is MIT licensed.

(back to top)