Rails:ContatosController中的Net :: OpenTimeout #create

时间:2014-07-09 17:29:01

标签: ruby-on-rails ruby ruby-on-rails-4

我不知道为什么出现此错误 Net :: OpenTimeout在ContatosController #create 中。我在app / controllers / contatos_controller.rb中有以下代码:

  def create
    @contato = Contato.new(contato_params)

    respond_to do |format|
      if @contato.save
        ContatoMailer.mensagem(@contato).deliver
        format.html { redirect_to contatos_path, notice: 'Obrigado por enviar sua mensagem! Em breve, responderemos.' }
        format.json { render :show, status: :created, location: @contato }
      else
        format.html { render :new }
        format.json { render json: @contato.errors, status: :unprocessable_entity }
      end
    end
  end

在app / mailers / contato_mailer.rb中:

class ContatoMailer < ActionMailer::Base
    default from: "myemail@gmail.com"

    def mensagem(contato)
        mail(:to => "myemail@gmail.com", :subject => "Subject")            
    end
end

在config / initializers / setup_mail.rb中:

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

当然,app / views / contato_mailer / mensagem.html.erb中的模板。

当我从表单提交新邮件时,Rails返回“执行过期”,重点关注“ContatoMailer.mensagem(@ contato).deliver”

拜托,我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试更改domain:键以指向'gmail',因此它应显示为:

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

我认为它已超时,因为它不知道domain.com与您的设置相对应。