Rails 4: Net::ReadTimeout when calling ActionMailer - ruby-on-rails

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 => "from#slf.net"
default :to => "nayr#slf.net"
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 user#slf.net. Is that correct or is the right format just "user" and the domain is implied from domain parameter?

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

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.

add following code in intitialize
require 'net/smtp'
module Net
class SMTP
def tls?
true
end
end
end

Related

Require support in ruby on rails smtp delivery method

Im trying to send a notification email to my account in gmail by using smtp delivery method when i click the place order button but i am getting all sorts of connection and authentication errors
like these:An existing connection was forcibly closed by the remote host. - SSL_connect
Net::ReadTimeout when calling ActionMailer
rails email error - 530-5.5.1 Authentication Required.
SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server session ticket A
i have searched everywhere and everytime i try to change the port or do anything to my developments.rb file either of these errors shows up
this is my development.rb file
Rails.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.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = 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
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
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 => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => ENV["GMAIL_USERNAME"] ,#your gmail id
:password => ENV["GMAIL_PASSWORD"], #your gmail password
:authentication => :plain,
:enable_starttls_auto => true,
tls: true
}
end
i dont know what the hell is going on i need some help in this and currently i am getting the first error that existing connection one

Session ID changes when I make a POST request in Rails but only during tests

I'm having a rather odd issue in that POST requests triggered by link_to with method: :post. In the test environment the session_id seems to change. This causes issues such as the current_user object doesn't exist within the action I'm posting too. I've logged out the request and the session info and I can see that the session has changed for the POST action and when I try to use current_user the test fails.
I have other POST requests through forms around the app. And they work fine. It seems to be some Rails magic around the link_to with method: :post and passing the CSRF token.
I'm able to get around this by changing my test.rb to be the same as my development.rb. But I'm sure this isn't a good solution. It's possible it's related to some configuration but it seems like this is the default behavior.
Controller
class RecruitersController < ApplicationController
before_action -> { STDOUT.puts "Request: #{request.method} #{request.fullpath}" }
before_action -> { STDOUT.puts "Session: #{session[:session_id]}" }
...
end
Button that triggers the POST Request
= link_to "<3", recruiter_request_url(id: recruiter.id), method: :post, remote: true
Output in Tests
Request: GET /recruiters/dashboard
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: GET /recruiters/premium
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: POST /recruiters/request_premium_trial/1
Session: 314c6eef0156aa36a469a4f9ea7513a8
Output in Development
Request: GET /recruiters/dashboard
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: GET /recruiters/premium
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: POST /recruiters/request_premium_trial/1
Session: cdb333efb5d62e6ddbb5914c8edd7a92
The Spec
Simple spec user signs in goes to the dashboard, goes to the premium page and then clicks on the link that makes the POST request.
scenario 'Should be able to make request', js:true do
rsign_in # Function that simulates sign in
click_on 'Premium'
click_on '<3'
assert_text 'Request made' # Fails as we're redirected to sign in page when we try to authenticate the user
end
Test.rb
Rails.application.configure do
# Set log level
config.log_level = :debug
# This means that all URLs need to have 5 parts to them. This is for http://recruiter.127.0.0.1.xip.io:3000
config.action_dispatch.tld_length = 5
# Settings specified here will take precedence over those in config/application.rb.
cache_store = :file_store, "tmp/cache"
# Use a different job queue
config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = true
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_controller.default_url_options = { host: 'localhost:5000' }
config.action_mailer.default_url_options = { host: 'localhost:5000' }
config.action_mailer.delivery_method = :test
# config.action_mailer.delivery_method = :smtp
# config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
config.active_record.raise_in_transactional_callbacks = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Development.rb
Rails.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 = true
config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"
# 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
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
config.cache_store = :dalli_store
# Use a different job queue
config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?
# 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
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.after_initialize do
Bullet.enable = false
Bullet.alert = true
Bullet.console = true
Bullet.rails_logger = true
end
end
Seems like the error was coming from this line in my test.rb, was just trying to set the default host on my links to not be example.com. Not sure how this resulted in the bug I was experiencing. But after going through the config and trying to figure out why development.rb was working. This is what I got.
config.action_controller.default_url_options = { host: 'localhost:5000' }
Thanks to everybody who helped me.

Action mailer settings not working in rails 4

I am trying to send notification mail using Action mailer in rails 4 but it is not working properly. After solving many error I am able to send the mail and no error shows in the console but the mail is not received. This has always worked for me before but I dont know how it is not happening now. Here is my output in the console.
AdminMailer#subscription_added: processed outbound mail in 202.7ms
Sent mail to receiver#gmail.com (9.8ms)
Date: Sat, 28 Nov 2015 21:25:52 +0530
To: receiver#gmail.com
Message-ID: <5659ce8829e54_2b0563310f2520dc#ubuntu.mail>
Subject: Subscription addded
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!--XRAY START 8 /home/yogesh/Desktop/new/munam/app/views/layouts/mailer.html.haml-->
<hmtl>
<body>
<!--XRAY START 7 /home/yogesh/Desktop/new/munam/app/views/admin_mailer/subscription_added.html.haml-->
name of receiver
Email receiver#gmail.com
<!--XRAY END 7-->
</body>
</hmtl>
<!--XRAY END 8-->
Completed 302 Found in 320ms (ActiveRecord: 70.3ms)
Here is my code for it
In my development.rb
Rails.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
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { :host => "localhost:3000" }
# Automatically inject JavaScript needed for LiveReload
# config.middleware.insert_after(ActionDispatch::Static, Rack::LiveReload)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'localhost:3000',
user_name: 'mymail#gmail.com',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
end
My code that triggers the mail
AdminMailer.subscription_added(#subscription).deliver_now
My Admin mailer
class AdminMailer < ApplicationMailer
def subscription_added(subscription)
#subscription = subscription
begin
mail(:to => "sender#gmail.com", :subject => "Subscription addded")
rescue Exception => e
end
end
end
I will really thankful if someone tells me where I am going wrong or what extra do I need to do. Thanks a lot in advance.
You're hiding any errors that may occur when trying to send the email, with this line:
config.action_mailer.raise_delivery_errors = false
Change it to true, restart your server, and you will get an error if you try to send mail and it fails.
In development, I'd recommend against using a real live mail server, though - I'd recommend using something like Mailcatcher (http://mailcatcher.me/)
Try to raise run-time errors using the following code in development.rb file.
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

learn-ruby-on-rails tutorial, mailchimp not sending any emails

I've been following the tutorial and have already reach deployment on heroku.
The contact forms work perfectly and no errors messages are displaying.
When I use the sign up button the success message appears with no error messages. Signing in at mailchimp reveals the email has indeed been added to the list.
However, I am not receiving any confirmation emails(waited 24 hours and signing up with 2 separate emails), either to owner email or the signup email. I have checked the "settings/list name and default campaigns/send a final welcome email" box as well as the "send a final welcome email" box at the create forms section.
I believe the issue is with my mailchimp settings somewhere but I'm not sure.
I'm also not sure if we're meant to be using an "opt in email" or how to activate this. I'm new to mailchimp and the whole signup forms system is confusing to me. Please help.
EDIT: file included as requested:
config/environments/development.rb
Rails.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
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: Rails.application.secrets.domain_name,
authentications: "plain",
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = 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
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Well I solved the problem changing status from "subscribed" to "pending", apparently mailchimp doesn't send final welcome emails when it uses single opt in. This sends out an email asking for confirmation and once confirmed then it sends the final welcome email and a notification to the owner. I don't think this was the approach in the book and if anyone solved this another way please let me know.
def subscribe
mailchimp = Gibbon::Request.new(api_key: Rails.application.secrets.mailchimp_api_key)
list_id = Rails.application.secrets.mailchimp_list_id
result = mailchimp.lists(list_id).members.create(
body: {
email_address: self.email,
status: 'pending'
})
Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end

I don't get confirm mail

I have a problem with devise mail confirm.
I adding mailcatcher, that work correct:
Thu, 24 Sep 2015 05:49:50 +0300
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: ...#gmail.com
Message-ID: <560364ce3f3d_34372357331e9383d#gerdon-MS-7346.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome ...#gmail.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>Confirm my account</p>
but i not getting this at my gmail.
Here is my code on rails:
/development.rb
Rails.application.configure do
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
end
/production.rb
Rails.application.configure do
config.log_level = :debug
config.i18n.fallbacks = true
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
config.action_mailer.default_url_options = {:host => 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "127.0.0.1"
:port => 25,
:domain => 'localhost:3000'
}
end
i use this comment like a documentation in my try to set mail confirm:
How do I set up email confirmation with Devise?
To enable email sending, you need to do two things.
Configure your email for in config/environments/development.rb (not sure if you've already done that.
Configure your email in Devise by editing config/initializers/devise.rb. You've definitely not done that, given the "from" address visible in your log.
Step 1. Here are settings if you are sending from a gmail account. Pay attention to all the email settings, including allowing email sending in development mode and default url option. You have to change the smtp settings to reflect your account.
Rails.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
# Send emails in test mode
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "domain.of.sender.net",
authentication: "plain",
user_name: "your_user_name",
password: "your_password",
enable_starttls_auto: true
}
Step 2. Configure config/initializers/devise.rb to include the email that you just configured for sending.
# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
# The secret key used by Devise. Devise uses this key to generate
# random tokens. Changing this key will render invalid all existing
# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` on Rails 4+ applications as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'ewe44lwemwle66wmew4lewwew'
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'YOUR_EMAIL_HERE'
This may sound like a silly question, but are you running an smtp server on localhost??

Resources