Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: Net::ReadTimeout when calling ActionMailer

When I run my mailer in development mode, I get the following error:

Net::ReadTimeout in SchoolApplicationsController#create

Here is the controller method that is getting the timeout

  def create
    @school_application = SchoolApplication.new(school_application_params)
     @school_application.program_cost =    @school_application.calculate_cost_to_charge(params[:school_application][:program], params[:school_application][:duration])
    if @school_application.save

      NotificationsMailer.send_application(@school_application).deliver
      redirect_to application_path(@school_application.id)
    else  
      Rails.logger.debug(@school_application.errors.full_messages)
          @school_application.errors.full_messages.each do |msg|
        flash.now[:error] = msg
      end
      render action: "new"
    end
  end

I am positive the error is being caused by the NotificationsMailer call because when I comment it out, I no longer get the error.

Here is my mailer, and the settings:

class NotificationsMailer < ActionMailer::Base

  default :from => "[email protected]"
  default :to => "[email protected]"

  def send_application(application)
    @application = application 
    mail(:subject => "New Application")
  end
end

Here is my environments/development.rb smtp settings:

Fls::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.

  # 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
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:              'secure3309.hostgator.com',
  port:                 465,
  domain:               'slf.net',
  ssl: true,
  user_name:            ENV['slf_username'],
  password:             ENV['slf_password'],
  authentication:       'plain',
  enable_starttls_auto: true  }
end

When I write ENV['slf_username'] in the Rails console I get the right value. Same with the password. The username is in the format [email protected]. Is that correct or is the right format just "user" and the domain is implied from domain parameter?

like image 710
Thalatta Avatar asked Oct 02 '14 17:10

Thalatta


2 Answers

After reading this post I took another look at my smtp settings and added

tls: true 

changed port: 465 to port: '465' as I noticed that most people write it as a string. Also Similarly changed the string "plain" to the symbol :plain

like image 142
Thalatta Avatar answered Nov 07 '22 14:11

Thalatta


I faced the similar issue when i connect the smtp mail to qq mail(business mail). I updated my settings by following the post as like below:

config.action_mailer.smtp_settings = {
address:              'smtp.exmail.qq.com',
port:                 '465',
domain:               'groobusiness.com',
user_name:            ENV['GMAIL_USER_NAME'],
password:             ENV['GMAIL_PASSWORD'],
authentication:       :plain,
enable_starttls_auto: true,
openssl_verify_mode:  'none',
ssl:                   true,
tls:                   true
}

And the issue got solved. Hope it might be helpful for someone who is facing this issue.

like image 44
maniempire Avatar answered Nov 07 '22 14:11

maniempire