没有Rails的Ruby中的Action Mailer - 超时错误

时间:2012-07-19 01:08:35

标签: ruby smtp actionmailer google-apps

我使用Action Mailer发送每日电子邮件,但没有Rails。我还在这里查看了其他问题:Action Mailer 3 without RailsActionMailer and Ramaze

这是我的代码:

require 'action_mailer'

class Mailer < ActionMailer::Base
  def daily_names_email(names,subject="test daily mail",to = "test@domain.com")
  @names = "test names"
  mail(
      :to      => to,
      :from    => "me@domain.com",
      :subject => subject
    ) do |format|
    format.text
    format.html
    end
  end
end

Mailer.raise_delivery_errors = true
Mailer.delivery_method = :smtp
Mailer.smtp_settings = {
 :address   => "smtp.gmail.com",
 :port      => 465,
 :domain    => "google",
 :authentication => :plain,
 :user_name      => "me@domain.com",
 :password       => "*****",
 :enable_starttls_auto => true
}
Mailer.view_paths = File.dirname(__FILE__)
Mailer.logger = Logger.new(STDOUT)

email = Mailer.daily_names_email('hello')

puts email
email.deliver

这里是错误输出:

Date: Thu, 19 Jul 2012 08:46:18 +0800
From: me@domain.com
To: me@domain.com

Message-ID: <500758da4dbda_151082d87c10766@ubuntu.mail>
Subject: test daily mail
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_500758da4a289_151082d87c104fb";
 charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_500758da4a289_151082d87c104fb
Date: Thu, 19 Jul 2012 08:46:18 +0800
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <500758da4c2f6_151082d87c10515@ubuntu.mail>

/usr/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /usr/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from /usr/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from /usr/lib/ruby/1.9.1/net/smtp.rb:929:in `recv_response'
from /usr/lib/ruby/1.9.1/net/smtp.rb:552:in `block in do_start'
from /usr/lib/ruby/1.9.1/net/smtp.rb:939:in `critical'
from /usr/lib/ruby/1.9.1/net/smtp.rb:552:in `do_start'
from /usr/lib/ruby/1.9.1/net/smtp.rb:519:in `start'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
from /var/lib/gems/1.9.1/gems/actionmailer-3.2.6/lib/action_mailer/base.rb:415:in `block in deliver_mail'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /var/lib/gems/1.9.1/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
from /var/lib/gems/1.9.1/gems/actionmailer-3.2.6/lib/action_mailer/base.rb:413:in `deliver_mail'
from /var/lib/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
from mailer.rb:34:in `<main>''

任何帮助将不胜感激!提前谢谢!

1 个答案:

答案 0 :(得分:1)

除非是拼写错误,否则“域”参数是错误的。

:domain => 'google'应为:domain => 'yourdomain.com'

下面给出的代码,正确的域名可以正常使用

require 'action_mailer'

class Mailer < ActionMailer::Base
  def daily_names_email(names,subject="test daily mail",to = "recipient@domain.com")
  mail(
      :to      => to,
      :from    => "you@yourdomain.com",
      :subject => subject
    ) do |format|
    format.text
    format.html
    end
  end
end

Mailer.raise_delivery_errors = true
Mailer.delivery_method = :smtp
Mailer.smtp_settings = {
 :address   => "smtp.gmail.com",
 :port      => 587,
 :domain    => "yourdomain.com",
 :authentication => :plain,
 :user_name      => "you@yourdomain.com",
 :password       => "*****",
 :enable_starttls_auto => true
}
Mailer.view_paths = File.dirname(__FILE__)
Mailer.logger = Logger.new(STDOUT)

email = Mailer.daily_names_email('hello').deliver

puts email