Techie October 2022
Introduction
When ActionMailer and Gmail settings are properly configured, we can easily send emails from controller actions by generating and setting up mailers.
The set up is really easy. Let’s have a look.
Part A: Setting Up Gmail App Passwords
ActionMailer will use your gmail account to send mail, but you can not use your normal gmail password with external applications. Gmail has a nifty feature called App passwords that lets you sign in to your Google Account from apps on devices that don’t support 2-Step Verification. Click here to set up App passwords. On the page, select ‘Mail’ under the ‘select app’ tab, then enter a custom name under the ‘select device’ tab.
Click on the ‘Generate’ button to generate the password. Just like your normal password, this app password grants complete access to your Google Account. Copy 16-character password shown, you will need it for the next step.
Part B: Setting Up Environment Variables For the Email Credentials
1 . In your rails project, create a ruby hidden file in /config directory that will contain the ENV variables. Prepend a dot . in the file name to make it hidden.
e.g .my_secret.rb, which will contain this:
NB: press Ctrl + Hto view hidden files on linux.
2 . You want the rails app to load .my_secret.rb file as soon as the server fires up. To do that, you will use the File and load methods inside the /config/environment.rb file in the rails project. Make sure to place this code just above the line: Rails.application.initialize!
3 . Ensure the .my_secret.rb file is not submitted to github whenever you commit your project to a github repository. To do that, open the .gitignore file and add the path to the file as shown below:
With the these steps you are done setting up the environment variables for your database. Now if you open the rails console and enter this ENV[‘EMAIL_USER_ID’], it should output the content of that variable:
Part C: Configure ActionMailer
1 . Configure ActionMailer for the development
environment in config/environments/development.rb.
That’s all!
Thanks for reading, see you in the next one!