Rails Mailer Error "Errno::ECONNREFUSED (Connection refused...)" After Installed & Uninstalled Postfix - ruby-on-rails

My app on Ruby on Rails has suddenly got an error, shown as below, when it tries to send an email with a Mailer using Gmail.
This problem started after I installed & uninstalled "postfix" on the server (Apache/CentOS) since the installation was too difficult for me. Before that, the Mailer was properly working.
log/production.log
NoticeMailer#newtask_email: processed outbound mail in 2.5ms
I, [2016-08-19T10:55:13.429666 #3782] INFO -- :
Sent mail to email#example.com (4.2ms)
D, [2016-08-19T10:55:13.429750 #3782] DEBUG -- : Date: Fri, 19 Aug 2016 10:55:13 +0900
From: info#domain.com
To: email#example.com
Message-ID: <defaultdomain.mail>
Subject: test
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
(Message)
test
I, [2016-08-19T10:55:13.430023 #3782] INFO -- : Completed 500 Internal Server Error in 17ms (ActiveRecord: 2.0ms)
F, [2016-08-19T10:55:13.431110 #3782] FATAL -- :
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
app/controllers/tasks_controller.rb:41:in `create'
Related coding is shown as below:
config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost:3000' }
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',
authentication: :plain,
user_name: 'xxxxx#gmail.com',
password: 'xxxxx'
}
The coding on should be fine. So, I guess some server settings have been changed due to the installation of postfix.
I installed postfix following the procedure written on the webiste:
http://tipszone.jp/20120521_mail_server/
And, I uninstalled it by "yum erase postfix", "yum remove dovecot", "rm -rf /etc/postfix" and "rm -rf /etc/dovecot". After that I did "reboot" and "service httpd restart".
"/etc/sysconfig/iptables" is also same as before.
I have no idea about how to fix this, so could you help me out please...

This problem has been solved by writing same "config.action_mailer.smtp_settings" coding on production.rb as environment.rb.
It seemed like it had been fine without the coding on production.rb because of default setting before I installed postfix.

Related

AactionMailer configuration without authentication

I have to send e-mails from a Rails application, and am trying to do so with ActionMailer. The mail administrator created a new account for the purpose that requires no authentication; I am able to send messages with this new account with msmtp using a configuration like (in ~/.msmtprc)
account myapp
auth off
host mailserver.domain
port 25
from myapp#domain
user myapp#domain
password
In the development.rb file I introduced a similar configuration:
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mailserver.domain',
from: 'myapp#domain',
port: 25,
domain: 'domain',
authentication: 'off',
user_name: 'myapp#domain',
password: '',
enable_starttls_auto: false }
config.action_mailer.perform_deliveries = true
But whenever a mailing method is invoked I get the following exception:
(0.7ms) ROLLBACK
Completed 500 Internal Server Error in 387ms
Errno::ECONNREFUSED (Connection refused - connect(2)):
app/models/user.rb:28:in `send_admin_mail'
app/controllers/registrations_controller.rb:15:in `create'
I imagine something is wrong or missing in the development.rb file, but I have no idea what it may be. Any help is appreciated.
Update: Following eugen's suggestion below I found that at execution time ActionMailer::Base.smtp_settings does not contain the correct settings; in particular, address is pointing to localhost instead of pointing to the mail server:
=> "{:address=>\"localhost\",
:port=>25,
:domain=>\"localhost.localdomain\",
:user_name=>nil,
:password=>nil,
:authentication=>nil,
:enable_starttls_auto=>true}"
How can I correct this?
Actually, your configuration has some problems. Just like the the 'from:' key, it should not be placed in smtp_settings. As for other possible problems, please refer RubyonRails.guides
Hope to do some help.

Net::OpenTimeout (execution expired) exception with Rails mailer

I have problem with delivering emails on production server.
When mailer processed new message, calls exception Net::OpenTimeout (execution expired)
My smtp settings:
#settings.yml
production:
smtp:
address: smtp.gmail.com
port: 587
domain: mydomain.net
user_name: username#gmail.com
password: password
authentication: plain
enable_starttls_auto: true
My environment settings:
#production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = Settings.smtp.symbolize_keys
From logs:
Sent mail to username#gmail.com (30010.1ms)
I, [2014-10-15T12:59:22.371563 #19779] INFO -- : Completed 500 Internal Server Error in 30051ms
F, [2014-10-15T12:59:22.373984 #19779] FATAL -- :.
Net::OpenTimeout (execution expired):
app/controllers/subscribers_controller.rb:9:in `create'
My VPS provider (DigitalOcean) blocked SMTP on IPv6 by default :(
I had to disable IPv6 on server with next config:
# /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Apply settings:
$ sysctl -p
Uncomment the next line in /etc/gai.conf to prefer IPv4:
precedence ::ffff:0:0/96 100
And remove IPv6 DNS from /etc/resolv.conf:
nameserver 2001:4860:4860::8844 # remove lines like this
nameserver 8.8.8.8 #don't remove
Then restart application and Nginx (optional)
I had the same issue, in my case it was because Google Cloud Compute platform blocks outgoing connections to SMTP servers outside of Google's domain on ports 25, 465, and 587.
To fix this problem we had to connect to a non-standard SMTP port (most API-based mailer platforms support 2525 as well for this reason).

Sending mail using Mandrill smtp on rails app

I'm hosting a rails app on my own server using Unicorn and Nginx. I have an issue when i try to send mails using mandrill, i get the following error :
Net::SMTPServerBusy: 401 Syntax: HELO hostname
And this is my configuration on production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default charset: 'utf-8'
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
authentication: "plain",
domain: localhost,
enable_starttls_auto: true,
user_name: ENV['MANDRILL_USERNAME'],
password: ENV['MANDRILL_API_KEY']
}
what i get from the error is that maybe mandrill reject the domain because is not "safe/registered"??
I already tried changing the domain to 'timewarp.us' (yet not registered), and i'm having the same problem.
If i change madrill and use the gmail smtp it works as expected.
This is the first time i'm doing a deploy to a server and i'm kinda lost.
Any help would be great. Thanks!
Try adding your actual domain instead of localhost. My guess is that Mandrill won't accept localhost as a hostname.

ActionMailer Timing out

I'm using Rails 3.2.17 and ActionMailer to send out emails. In the development environment I used my google account to send email and it worked fine. On production, with the production email settings it's timing out. To verify I copied over the production settings to dev.
Currently it's failing with very little info:
Rendered notifications_mailer/access_granted.html.haml (2.8ms)
Rendered notifications_mailer/access_granted.text.haml (1.5ms)
E, [2014-03-27T12:15:16.066641 #36665] ERROR -- : worker=1 PID:36673 timeout (16s > 15s), killing
E, [2014-03-27T12:15:16.079432 #36665] ERROR -- : reaped #<Process::Status: pid 36673 SIGKILL (signal 9)> worker=1
I, [2014-03-27T12:15:16.096443 #36684] INFO -- : worker=1 ready
SMTP Settings
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "secure.emailsrvr.com",
port: 465,
domain: "MYDOMAIN.net",
authentication: :plain,
enable_starttls_auto: true,
user_name: "no-reply#MYDOMAIN.net",
password: "MYPASSWORD"
I've used telnet to confirm the host and port, however when I try to send mail according to this tutorial it disconnects me.
Telnet Console
$ telnet secure.emailsrvr.com 465
Trying 166.78.79.129...
Connected to secure.emailsrvr.com.
Escape character is '^]'.
hello me
Mail from: no-reply#MYDOMAIN.net
Connection closed by foreign host.
Additionally, I've setup my email app (Mac Mail) with this information and AM able to send email with these settings.
Any advice on how to approach troubleshooting this would be greatly appreciated.
you should increase pool time in your database yml file.
It will eliminate time out problem.

reset password on production with use the devise, unicorn

If I try to reset password I see: We're sorry, but something went wrong.
Then I see to my logs:
poruction.log
Sent mail to achempion#gmail.com (238ms)
Completed 500 Internal Server Error in 610ms
OpenSSL::SSL::SSLError (hostname does not match the server certificate):
In my production.rb I set the config.action_mailer.default_url_options = { :host => '178.79.153.194' }
and openssl version is OpenSSL 1.0.0g 18 Jan 2012
I reset password via devise gem
It was my mistake. I forgot to configure ActionMailer.
link tutorial

Resources