Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending Email through SMTP mailer in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 585
    Comment on it

    Sending Email through SMTP mailer in Rails

    As we know these days in almost every application we have the need to send mails to its users then be it for password confirmations, subscriptions etc.

    So these mails can be sent through your rails application with the help of SMTP that is Simple Mail Transfer Protocol. We will be sending mails via gmail. For the purpose of sending emails, the action mailer class is used.

    First of all you need to enable the less secure apps in your gmail settings which will allow less secure apps to access your gmail account. You can do the same through this link https://www.google.com/settings/security/lesssecureapps

    • Creating a mailer in our rails application.

    Our rails application already have a mailer folder in which our mailer filers are present. So first we will create a mailer through our console like this

    $ rails g mailer notification_mailer
    

    after this, some files will be created as follows 

    Running via Spring preloader in process 5666
          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
    • Now go to the folder app/mailers and create a file notification_mailer.rb. This file will contain our default email ID through which the mails have to be sent and a method in which we will define where the mail has to be sent like this
    class NotificationMailer < ApplicationMailer
    
      default from: "abc@gmail.com"
    
      def notification_email(receiver_email)
        @recipient = receiver_email
        mail(to: receiver_email, subject: 'This is a dummy email for testing!')
      end
    end
    • After this we will be creating the content or the body part of the mail in our views. For that go to the folder  app/views/notification_mailer and create a file notification_email.html.erb.
    <h1>Hi <%= @recipient%></h1>
    <p>
      This is a sample mail generated for testing purposes.
    </p>
    <h3>Regards</h3>
    <h2>Admin</h2>
    • Now next thing is to configure our mailers in the environment files development.rb and production.rb in the config/environment/ folder like this
      config.action_mailer.perform_deliveries = true #try to force sending in development
      config.action_mailer.raise_delivery_errors = true #to raise any errors while sending mail
    
      config.action_mailer.smtp_settings = {
        :address              => "smtp.gmail.com",
        :port                 =>  587,
        :user_name            => 'abc@gmail.com',
        :password             => your_password,
        :authentication       => "plain",
        :enable_starttls_auto => true
      }
    • Now last thing is to call the mailer method from the controller method from where you want to initiate the mail sending process. You can call the function like this
    NotificationMailer.notification_email("def@gmail.com").deliver_now

    And here you go. Now as soon as the method will be called, the mail will be sent to the user successfully.

 0 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: