连接拒绝 - 连接(2)Ruby on Rails邮件设置 [英] Connection refused - connect(2) Ruby on Rails Mail Setup

查看:157
本文介绍了连接拒绝 - 连接(2)Ruby on Rails邮件设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 config / environments / production.rb development.rb 中设置了smtp设置在`config / initializers / setup_mail.rb中添加设置

  config.action_mailer.default_url_options = {:host => 'ipaddress'} 
config.action_mailer.delivery_method =:smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'EMAIL_ADDRESS@gmail.com',
:password => 'pass',
:authentication => :plain,
:enable_starttls_auto => true,
:openssl_verify_mode => 'none'
}

config / initializers / setup_mail.rb

  ActionMailer :: Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'EMAIL_ADDRESS@gmail.com',
:password => 'pass',
:authentication => :plain,
:enable_starttls_auto => true,
:openssl_verify_mode => 'none'
}

ActionMailer :: Base.default_url_options [:host] =ipaddress

我收到错误连接拒绝 - 连接(2)



while我在本地工作在以下配置我没有得到任何错误,邮件已发送。



配置/ initializers / setup_mail.rb(localhost)

  ActionMailer :: Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'localhost',
:user_name => 'EMAIL_ADDRESS@gmail.com',
:password => 'pass',
:authentication => 'plain',
:enable_starttls_auto => true
}

ActionMailer :: Base.default_url_options [:host] =localhost:3000

在控制台中运行时出现错误,

  Errno :: ECONNREFUSED:连接被拒绝 - connect(2)
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in`initialize'
从/home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in`open'
from / home / attuser / .rvm / rubies / ruby​​-1.9.3-p545 / lib / ruby​​ / 1.9.1 / net / smtp.rb:541:在`tcp_socket'
from /home/attuser/.rvm/rubies/ruby- 1.9.3-p545 / lib / ruby​​ / 1.9.1 / net / smtp.rb:550:在`block in do_start'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545 /lib/ruby/1.9.1/timeout.rb:69:in`timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/ timeout.rb:100:在`timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `/ do / start / $ 5 / lib / ruby​​ / 1.9.1 / net / smtp.rb:520:在`start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4 .4 / lib / mail / network / delivery_methods / smtp.rb:144:在`deliver!'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4 .4 / lib / mail / message.rb:2034:在`do_delivery'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/ mail / message.rb:229:在`block in delivery'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base .rb:415:在`block in deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb: 123:在`block in instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20 :在`instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in`instrument' / b / b /gems/actionmailer-3.2.9/lib/action_mailer/base.rb:413:in`deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail- 2.4.4 / lib / mail / message.rb:229:来自(irb)的`deliver'
:来自/home/attuser/.rvm/gems/ruby-1.9.3-p545的$ 28
/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in`start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/ railties-3.2.9 / lib / rails / commands / console.rb:8:在`start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2 .9 / lib / rails / commands.rb:41:在`< top(required)>'
from script / rails:6:in`require'

控制器

  def sendResume 
@ name = params [:name]
@email_id = params [:email_id]
@mob_no = params [:ph_no]

attachments = params [:resume]
如果simple_captcha_valid?

如果params [:resume]
filename = attachments.original_filename

extname = File.extname(filename)[1 ..- 1]
mime_type = Mime :: Type.lookup_by_extension(extname)
content_type = mime_type.to_s除非mime_type.nil?


如果content_type!=application / pdf
flash [:error] =只允许使用pdf文件
redirect_to:action =>职业
else
File.open(Rails.root.join('tmp','uploads',attachments.original_filename),'w')do | file |
re = attachments.read
file.write(re.force_encoding(utf-8))
@attached_pa​​th = file.path
end


begin
ResumeMailer.sendResume(@name,@email_id,@mob_no,@attached_pa​​th,attachments.original_filename).deliver
flash [:notice] =您的简历已提交成功
redirect_to:action =>careers
rescue Exception => e
放e.message

logger.warn发送邮件错误
flash [:error] =提交简历时出错
redirect_to:action => ;职业
end

end
else
flash [:error] =请上传您的简历
redirect_to:action =>职业生涯
end
else

flash [:error] =验证码不正确
redirect_to:action =>职业
end

end

安装后缀错误在控制台模式中消失,电子邮件从控制台发送,但在图形模式(浏览器)中,我收到错误连接拒绝 - 连接(2)



什么是错误,



提前感谢

解决方案

我看到您在两台服务器上都尝试过相同的设置,但只在您的生产/非本地主机环境中出现问题。



这表示环境本身的网络配置存在问题。



在与应用程序相同的服务器上的命令行中,尝试以下命令



  telnet smtp.gmail.com 587 

您应该看到以下内容

  telnet smtp.gmail.com 587 
尝试173.194.79.108 ...
连接到gmail-smtp-msa.l.google.com。
转义字符为'^]。
220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtp

如果您没有看到这很可能会导致连接错误。这意味着您的计算机无法访问gmail服务器。可能的问题是a)一般出站网络连接,b)防火墙专门阻止所有出站连接c)防火墙阻止/允许连接到特定端口或主机



如果这不工作,也尝试以下端口代替587

  telnet smtp.gmail.com 465 
telnet smtp.gmail。 com 25

如果其中一个更成功,请更改您的邮件服务器设置以进行相应的使用。 p>

编辑:使用Gmail,然后使用我们的自定义域,我们遇到了一些麻烦。有一件事可以帮助您从配置文件中删除:域行,尝试没有它。



为了参考,这里是我的Gmail配置在prod使用我们的自定义域由Gmail托管:

  config.action_mailer.default_url_options = {:host => my.website.com} 
config.action_mailer.delivery_method =:smtp
config.action_mailer.smtp_settings = {
地址:'smtp.gmail.com',
端口:587,
domain:'website.com',
user_name:'user@website.com',
密码:'密码',
身份验证:'plain',
enable_starttls_auto:true
}

此外,如果您有双重身份验证启用您的Gmail帐户,您可能需要禁用它,并再次尝试,只是为了确认这并不会使问题复杂化。


I have set smtp settings in both config/environments/production.rb and development.rb also i have added the settings in `config/initializers/setup_mail.rb

config.action_mailer.default_url_options = { :host => 'ipaddress' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
}

config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
} 

ActionMailer::Base.default_url_options[:host] = "ipaddress" 

i am getting the error Connection refused - connect(2)

while i have working in localhost with below configuration I haven’t get any error and also mail has been sent.

config/initializers/setup_mail.rb (localhost)

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'localhost',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => 'plain',
  :enable_starttls_auto => true
} 

ActionMailer::Base.default_url_options[:host] = "localhost:3000" 

Errors are show below while running in console,

Errno::ECONNREFUSED: Connection refused - connect(2)
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `initialize'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `open'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `tcp_socket'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `block in do_start'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:69:in `timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `do_start'
from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:520:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:415:in `block in deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `block in instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `instrument'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:413:in `deliver_mail'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
from (irb):28
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

controller

def sendResume
@name =params[:name]
@email_id = params[:email_id]
@mob_no = params[:ph_no]

attachments = params[:resume]
if simple_captcha_valid?

 if params[:resume]
     filename=attachments.original_filename   

    extname = File.extname(filename)[1..-1]
    mime_type = Mime::Type.lookup_by_extension(extname)
    content_type = mime_type.to_s unless mime_type.nil?


        if content_type !="application/pdf"
           flash[:error]= "Only pdf files are allowed"
           redirect_to :action=>"careers"
        else
         File.open(Rails.root.join('tmp', 'uploads', attachments.original_filename), 'w') do |file|
          re = attachments.read
          file.write(re.force_encoding("utf-8"))
          @attached_path = file.path
        end


        begin
          ResumeMailer.sendResume(@name, @email_id, @mob_no, @attached_path, attachments.original_filename).deliver
          flash[:notice] = "Your resume has been submitted successfully"
          redirect_to :action=>"careers"
        rescue Exception => e
          puts e.message

          logger.warn "error sending mail"
          flash[:error]= "Error in submitting resume"
          redirect_to :action=>"careers"
        end

     end
    else
      flash[:error]= "Please upload your resume"
      redirect_to :action=>"careers"
    end
else

  flash[:error]= "Incorrect captcha"
   redirect_to :action=>"careers"
end

end

After installing the postfix the error goes away in console mode and the mails send from console but in graphical mode (in browser) i am getting the error Connection refused - connect(2).

what is wrong,

thanks in advance.

解决方案

I see you have tried identical settings on both servers but are having problems only on your production / non-local host environment.

This points to an issue with the network configuration of the environment itself.

In the command line on your same server as your application, try the following command

telnet smtp.gmail.com 587

You should see something like the following

telnet smtp.gmail.com 587
Trying 173.194.79.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtp

If you do not see this, you will most likely get a connection error. This means your machine does not have access to the gmail server. Likely problems are a) general outbound network connectivity, b) firewalls specifically blocking all outbound connections c) firewalls blocking/allowing connections to particular ports or hosts

If this does not work, also try the following ports in place of 587

telnet smtp.gmail.com 465
telnet smtp.gmail.com 25

If one of these is more successful, change your mail server settings to use accordingly.

Edit: We had quite a bit of trouble using Gmail and then Gmail with our custom domain. One thing that can help is to remove the :domain line from your configuration file, try without it.

For reference, here is my Gmail config in prod using our custom domain hosted by Gmail:

config.action_mailer.default_url_options = { :host => "my.website.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'website.com',
    user_name:            'user@website.com',
    password:             'password',
    authentication:       'plain',
    enable_starttls_auto: true
}

Also, if you do have 2-factor authentication enabled on your Gmail account, you might want to disable it and try this again just to confirm it is not complicating the issue.

这篇关于连接拒绝 - 连接(2)Ruby on Rails邮件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆