2012-07-19 60 views
2

我使用Action Mailer發送每日電子郵件,但沒有Rails。我也看了看這裏的其他問題:Action Mailer 3 without RailsActionMailer 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>'' 

任何幫助,將不勝感激!提前致謝!

+0

'的telnet smtp.gmail.com 485'似乎我的系統上超時。你對你的主機名和端口號有信心嗎? – sarnold 2012-07-19 01:21:03

+0

@sarnold,你說得對,它應該是465或587,基於此鏈接(http://support.google.com/a/bin/answer.py?hl=zh-CN&answer=176600)在谷歌支持頁面 – ringular 2012-07-19 02:27:12

+0

使用端口465,我得到了上述錯誤(更新) – ringular 2012-07-19 02:28:58

回答

1

除非它有錯字,「域」參數是錯誤的。

:domain => 'google'應該:domain => 'yourdomain.com'

你下面給出的代碼,用正確的域正常工作

require 'action_mailer' 

class Mailer < ActionMailer::Base 
    def daily_names_email(names,subject="test daily mail",to = "[email protected]") 
    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  => 587, 
:domain => "yourdomain.com", 
: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').deliver 

puts email 
+0

嘿@anand。謝謝你的幫助。我已經試過以上&得到如下答覆 對於這些設置: ':端口=> 465, :域名=> 「domain.com」, :認證=>:登錄,' 或 ':端口=> 465, :domain =>「domain.com」, :authentication =>:plain,' Get this Response: '/usr/lib/ruby/1.9.1/net/protocol.rb: 146:在'rescue in rbuf_fill'中:Timeout :: Error(Timeout :: Error)from/usr/lib/ruby​​/1.9.1/net/protocol.rb:140:'rbuf_fill' – ringular 2012-07-19 05:21:10

+0

使用此設置: ':port => 587, :domain =>「domain.com」, :authentication =>:login, :port => 465, :domain =>「domain.com」, :authentication =>:plain,' 我得到: '/usr/lib/ruby/1.9.1/net/smtp.rb:960:in'check_auth_response ':535-5.7.1用戶名和密碼不被接受。通過/usr/lib/ruby/1.9.1/net/smtp從/usr/lib/ruby/1.9.1/net/smtp.rb:737:in'auth_plain' 瞭解更多信息(Net :: SMTPAuthenticationError) .rb:729:從/usr/lib/ruby/1.9.1/net/smtp.rb:564:in'authenticate' from /usr/lib/ruby/1.9.1/net/smtp .rb:519:in'start'' – ringular 2012-07-19 05:24:36

+0

要添加,我意識到端口號應該是587(IMAP),就像您指定&not 465(POP)。但是,當我使用端口587時,出現身份驗證錯誤。我不知道爲什麼 – ringular 2012-07-19 06:37:37

相關問題