Emails can be sent from you Rails application through many services like mandril, sendgrid, amazon SES. In this article, we would be explaining how we can send email using a Gmail credentials. Even though we can’t use it in production scenario due to the 500 email per day limitation, it would help us in prototyping an application quickly and also for cases where you want to send actual email in your development environment. Action Mailer is the ruby library in rails that will help us to do this.
Action Mailer allows you to send emails from your application using mailer classes and views. Mailers work very similarly to controllers. They inherit from ActionMailer::Base and live in app/mailers, and they have associated views that appear in app/views.To setup action mailer, must do the following:
1. Configuring the mailer in your environment file 2. Generating the mailer 3. Defining mailer action 4. Generating the mailer view 5. Delivering the email
Action Mailer Configuration
To configure action mailer add the following to your appropriate config/environments/$RAILS_ENV.rb file:
Eg:
config.action_mailer.delivery_method = :smtp config.action_mailer.raise_delivery_errors = true config.action_mailer.default_options = {from: '[email protected]'} config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, user_name: '<username>', password: '<password>', authentication: 'plain', enable_starttls_auto: true }
Generating the mailer
As you generate a controller for your application, you can also generate the mailer, which in turn will create some files and folder for you. The mailer class is exactly same as a controller, where each action has its associated view and all instance variable in an action is available in the corresponding views also. The only difference is that you do not write routes for the mailer action.
Eg:
rails generate mailer UserMailer
Defining mailer action
Mailers are very similar to Rails controllers. They also have methods called “actions” and use views to structure the content. Where a controller generates content like HTML to send back to the client, a Mailer creates a message to be delivered via email.
Eg:
class UserMailer < ApplicationMailer default from: '[email protected]' def welcome_email(user) @user = user @url = 'http://example.com/login' mail(to: @user.email, subject: 'Welcome to My Awesome Site') end end
Generating the mailer view
In the view, we will define how our email will look to the user. Create a erb file with the same name as our mailer action in app/views/user_mailer . So our, mailer view will be welcome_email.erb. The content of this file is simple HTML as shown below
Eg:
Welcome to sample.com You have successfully signed up to example.com Thanks for joining and have a great day!
Delivering the email
The common pattern for interfacing with ActionJob in Rails is to set up a Job with a perform() method that gets called asynchronously via perform_now or perform_later.
In the case of Mailers, you can directly call deliver_now or deliver_later since Action Job is well integrated with Action Mailer.
deliver_later
Basically, deliver_later is asynchronous. When you use this method, the email is not sent at the moment, but rather is pushed in a job’s queue. If the job is not running, the email will not be sent.
Eg:
Notifier.welcome(User.first).deliver_later Notifier.welcome(User.first).deliver_later(wait: 1.hour) Notifier.welcome(User.first).deliver_later(wait_until: 10.hours.from_now)
Options:
:wait – Enqueue the email to be delivered with a delay.
:wait_until – Enqueue the email to be delivered at (after) a specific date / time.
:queue – Enqueue the email on the specified queue.
deliver_later!
Enqueues the email to be delivered through Active Job. When the job runs it will send the email using deliver_now!. That means that the message will be sent bypassing checking perform_deliveries and raise_delivery_errors, so use with caution.
Eg:
Notifier.welcome(User.first).deliver_later! Notifier.welcome(User.first).deliver_later!(wait: 1.hour) Notifier.welcome(User.first).deliver_later!(wait_until: 10.hours.from_now)
deliver_now()
Delivers an email:
Eg:
Notifier.welcome(User.first).deliver_now
deliver_now!()
Delivers an email without checking perform_deliveries and raise_delivery_errors, so use with caution.
Eg:
Notifier.welcome(User.first).deliver_now!
References
]]>