Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending Emails in Rails using Gmail

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 859
    Comment on it

    Sending Emails in Rails using Gmail Account

    For sending emails in rails,ActionMailer Class is used. Emails can be sent using any service providers like Gmail, Sendgrid, Mandrill or any other. Here we will see how we can send the emails using our gmail account. The steps are given below:
    Step 1: Create a mailer in your rails app.

    	rails g mailer notification_mailer
    	## => Output
    	create  app/mailers/notification_mailer.rb
    	create  app/mailers/application_mailer.rb
    	invoke  erb
    	create    app/views/notification_mailer
    	create    app/views/layouts/mailer.text.erb
    	create    app/views/layouts/mailer.html.erb
    	invoke  test_unit
    	create    test/mailers/notification_mailer_test.rb
    	create    test/mailers/previews/notification_mailer_preview.rb
    

    Step 2: Open your app/mailers/notification_mailer.rb and set the default from and also create a method for sending emails

    	class NotificationMailer < ApplicationMailer
    		default from: "something@example.com"
    
    		def notification_email(receiver_email)
    			mail(to: receiver_email, subject: 'Sample Email')
    		end
    	end
    

    Step 3: Create a file app/views/notification_mailer/notification_email.html.erb. This file will be served as a body template in the mail. In case you want to use some dynamic content in this template, you can declare them as an instance variable in the notification_email action and use them in the view page

    	##Inside app/views/notification_mailer/notification_email.html.erb
    	Hi dear,
    
    	This is a test email
    

    Step 4: Configure the mailer with gmail username, password and smtp inside environment files config/environment/development.rb and config/environment/production.rb

    	config.action_mailer.delivery_method = :smtp
    	# SMTP settings for gmail
    	config.action_mailer.smtp_settings = {
    	:address              => "smtp.gmail.com",
    	:port                 => 587,
    	:user_name            => 'gmail_username',
    	:password             => 'gmail_password',
    	:authentication       => "plain",
    	:enable_starttls_auto => true
    	}
    

    Step 5: Now for testing the emails, just call this mailer action like this.

    	NotificationMailer.notification_email("any-email-address").deliver_now
    

    If during sending emails, you get any error like Net::SMTPAuthenticationError, you need to enable less secure apps for sending emails in your gmail setting.
    I have also created a sample app for sending emails, clone the repository and run the app locally to test it. Don't forget to place your gmail username and password at config/environment/development.rb . The github url for the app is https://github.com/raghvendra1501/sendemail-using-gmail-rails

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: