我使用Action Mailer發送每日電子郵件,但沒有Rails。我也看了看這裏的其他問題:Action Mailer 3 without Rails,ActionMailer and RamazeRuby中沒有Rails的Action Mailer - 超時錯誤
這裏是我的代碼:
require 'action_mailer'
class Mailer < ActionMailer::Base
def daily_names_email(names,subject="test daily mail",to = "[email protected]")
@names = "test names"
mail(
:to => to,
:from => "[email protected]",
: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 => "[email protected]",
: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: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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: <[email protected]>
/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>''
任何幫助,將不勝感激!提前致謝!
'的telnet smtp.gmail.com 485'似乎我的系統上超時。你對你的主機名和端口號有信心嗎? – sarnold 2012-07-19 01:21:03
@sarnold,你說得對,它應該是465或587,基於此鏈接(http://support.google.com/a/bin/answer.py?hl=zh-CN&answer=176600)在谷歌支持頁面 – ringular 2012-07-19 02:27:12
使用端口465,我得到了上述錯誤(更新) – ringular 2012-07-19 02:28:58