gpt4 book ai didi

ruby-on-rails - 连接被拒绝 - connect(2) Ruby on Rails 邮件设置

转载 作者:行者123 更新时间:2023-12-04 14:13:05 25 4
gpt4 key购买 nike

我在 config/environments/production.rb 中都设置了 smtp 设置和 development.rb我还在`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'
}

配置/初始化程序/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"

我收到错误 Connection refused - connect(2)
当我使用以下配置在 localhost 中工作时,我没有收到任何错误,并且邮件也已发送。

配置/初始化程序/setup_mail.rb (本地主机)
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: 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

安装后 postfix错误在控制台模式下消失,邮件从控制台发送,但在图形模式(在浏览器中)我收到错误 Connection refused - connect(2) .

怎么了,

提前致谢。

最佳答案

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

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

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

telnet smtp.gmail.com 587

您应该会看到如下所示的内容
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

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

如果这不起作用,请尝试以下端口代替 587
telnet smtp.gmail.com 465
telnet smtp.gmail.com 25

如果其中之一更成功,请相应地更改邮件服务器设置以使用。

编辑:我们在使用 Gmail 和使用自定义域的 Gmail 时遇到了很多麻烦。可以提供帮助的一件事是从配置文件中删除 :domain 行,尝试不使用它。

作为引用,这是我在 prod 中使用我们由 Gmail 托管的自定义域的 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
}

此外,如果您在 Gmail 帐户上启用了 2 因素身份验证,您可能需要禁用它并再次尝试以确认它不会使问题复杂化。

关于ruby-on-rails - 连接被拒绝 - connect(2) Ruby on Rails 邮件设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23494136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com