user3800418
user3800418

Reputation: 29

Email is not sent in RoR application

I want to send mail from my ruby on rails application and I follow the below steps:

First I create mailer using below command:

rails generate mailer UserMailer

I am doing below in user_mailer.rb page:

class UserMailer < ActionMailer::Base
  default from: "[email protected]"

  def schedule
    @mail_to = '[email protected]' #params[:Schedule][:Appointments]
    mail :to => @mail_to, :subject => "Hi"
  end
end

And schedule is my view page schedule.html.erb:

<%= form_for(:Schedule,:html => {:id => "Schedule"}) do |f| %>
    <table class="table" width="100%">
        <th><font size="+1">Interview Schedule</font></th>
    </table>
    <table>
        <tr>
            <td>
                <label style="font-size:small; font-weight:bold;">Send Appointments to:</label>
            </td>
            <td>
                <%= f.text_field :mail_to_address, :style => "font-size:small;" %>
            </td>
        </tr>
        <tr>
            <td style="vertical-align:top;">
                <label style="font-size:small; font-weight:bold;">Message:</label>
            </td>
            <td>
                <%= f.text_area :message, :style => "font-size:small; width:300px; height:100px;" %>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <%= f.submit "Send", { :style => "font-size:small" } %>
            </td>
        </tr>
    </table>
<% end %>

In the above code mail_to_address text_field I put email address where I want to send email and put some text in the message text_field and click on Send button, but the mail is not sending. why?

And, below is the environment.rb page code:

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
VideoResume::Application.initialize!
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :authentication => :login,
    :charset => 'utf-8',
    :user_name => "[email protected]",
    :password => "password"
}

and In my App/config/environments/development.rb I have:

VideoResume::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

Kindly suggest me what I should do, waiting for reply. Thanks

Upvotes: 1

Views: 96

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

Delivery

Bottom line is when you create a mailer in ROR, you'll have to deliver it somehow

As you can see here, you'll basically have to call the mailer & the .deliver method to get the thing sent. This is in parallel to your declaration of the mailer itself:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
   def action
      @mailer = UserMailer.schedule
      @mailer.deliver
   end
end

--

Settings

This, obviously, needs to have a series of settings in order to get it to work properly. We use gmail's SMTP settings for testing in development:

#config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "testdomain.com",
      :user_name            => "[email protected]",
      :password             => Rails.application.secrets.gmail,
      :authentication       => :plain,
      :enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
      :host => "localhost"
}

This will allow you to send the messages through gmail's servers - the above is working code

Upvotes: 0

Mandeep
Mandeep

Reputation: 9173

From at the comments above i think the problem is that you are not calling your mailer method. As @mixian945 and @Sachin pointed out you need to call your mailer.

Lets suppose you want to send a mail from create_schedule method in your controller then you can send it by

def create_schedule
  # your method logic
  UserMailer.schedule.deliver #this will call your schedule method in UserMailer class and hence send your mail
end

def schedule
  mail to: "[email protected]", subject: "Hi"
end

Also if you want to send users id or something to your deliver method then you can call your schedule method like

def create_schedule
  @user = User.find(params[:id]) #your logic to find your user
  # your method logic
  UserMailer.schedule.deliver(@user.email)
end

this will allow you to have your method like

def schedule (email)
  mail :to => email, :subject => "Hi"
end

For details refer to Action Mailer Basics

Upvotes: 3

Related Questions