无法通过Gmail SMTP服务器发送邮件(在讨论中)

问题描述:

我正在尝试设置 discourse ,这是一个rails3 Web应用程序,但是在使用gmail配置smtp时遇到了一些问题smtp服务器.

I'm trying to setup discourse, which is a rails3 webapp, but have some problems configuring smtp with gmail smtp server.

我昨天注册了一个新的Gmail帐户,并且可以登录浏览器和电子邮件客户端软件.

I have registered a new gmail account yesterday, and I can logged in browser and email-client software.

然后我在文件config/environments/production.rb中配置语篇:

Then I configure discourse, in the file config/environments/production.rb:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address   => "smtp.gmail.com",
  :port      => "587",
  :user_name => "smtp4shuzu@gmail.com",
  :password  => "12345678",
  :authentication => :plain,
  :domain => "shuzhu.org",
  :enable_starttls_auto => true
}

启动用于在后台发送邮件的sidekiq:

Start the sidekiq which is used to sending the mails in the background:

nohup bundle exec sidekiq > log/sidekiq.log 2>&1 &

然后以生产模式开始演讲:

Then start discourse in production mode:

rails server -e production -d

但是它不起作用.我可以在sidekiq.log中看到一些错误:

But it doesn't work. I can see some errors in sidekiq.log:

2013-03-01T03:06:02Z 30687 TID-qib28警告:{重试" => true,队列" =>默认",类" =>作业:: UserEmail","args" => [{"type" =>"signup","user_id" => 42,"email_token" =>"b40a21ece2b14586e346abfd96685975","current_site_id" =>默认"}}],"jid" =>"558bb6bd5aa36cfc8d3d1e91","error_message"=>"连接被拒绝-connect(2)," error_class"=>" Errno :: ECONNREFUSED," failed_at"=> 2013-03-01 03:06:02 UTC," retry_count"=> 0} 2013-03-01T03:06:02Z 30687 TID-qib28警告:连接被拒绝-connect(2) 2013-03-01T03:06:02Z 30687 TID-qib28警告:/home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in initialize' /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in打开' /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:在`tcp_socket'中

2013-03-01T03:06:02Z 30687 TID-qib28 WARN: {"retry"=>true, "queue"=>"default", "class"=>"Jobs::UserEmail", "args"=>[{"type"=>"signup", "user_id"=>42, "email_token"=>"b40a21ece2b14586e346abfd96685975", "current_site_id"=>"default"}], "jid"=>"558bb6bd5aa36cfc8d3d1e91", "error_message"=>"Connection refused - connect(2)", "error_class"=>"Errno::ECONNREFUSED", "failed_at"=>2013-03-01 03:06:02 UTC, "retry_count"=>0} 2013-03-01T03:06:02Z 30687 TID-qib28 WARN: Connection refused - connect(2) 2013-03-01T03:06:02Z 30687 TID-qib28 WARN: /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in initialize' /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:inopen' /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in `tcp_socket'

我尝试了各种smtp设置,但没有一个起作用.

I have tried all kinds of smtp settings, but none of them works.

更新:

每个@Basil的答案,我都尝试过:

Per @Basil's answer, I just tried:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address   => "smtp.gmail.com",
  :port      => 587,
  :user_name => "smtp4shuzu",
  :password  => "12345678",
  :authentication => "plain",
  :enable_starttls_auto => true
}

但是它有相同的错误.域shuzu.org是我的站点的域,我当时想将其传递给smtp.现在,我将其删除,但仍无法正常工作.

But it has the same error. The domain shuzu.org is the domain of my site, I was thinking I should passing it to smtp. Now I removed it, but still not working.

最后,我发现了(愚蠢的)原因.

At last, I found the (stupid) reason.

我应该在生产模式下启动sidekiq:

I should start sidekiq in production mode:

nohup bundle exec sidekiq -e production > log/sidekiq.log 2>&1 &