Rails 3的ActionMailer无法发送电子邮件。

3
我是在跟随 Ryan Bates的Rails 3 ActionMailer教程。我在终端中生成了邮件程序,然后在config/initializers下建立了一个setup_mail.rb文件。我输入了以下代码:
ActionMailer::Base.smtp_settings={
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domail               => "gmail.com",
  :user_name            => "my_account_at_gmail",
  :password             => "my_password",
  :authentication       => "plain"  ,
  :enable_starttls_auto => true
}

我的user_mailer.rb文件如下:

class UserMailer < ActionMailer::Base
  default :from => "my_account_at_gmail@gmail.com"

  def registration_confirmation(user)
    mail(:to => user.email,:subject => "registered")
  end
end

我在Rails控制台中进行了测试: u=User.first UserMailer.registration_confirmation(u).deliver 它显示:
 #<Mail::Message:2194479560, Multipart: false, Headers: <Date: Sat, 26 Feb 2011 14:42:06 +0800>, <From: my_account_at_gmail@gmail.com>, <To: some_account@gmail.com>, <Message-ID: <some_number@My-MacBook-Pro.local.mail>>, <Subject: registered>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>

但是我在这里从未收到过邮件...为什么?我该如何解决?我猜测这是send_mail.rb文件上的某个问题。
2个回答

2
如果这是你的 send_mail.rb 的复制/粘贴,那么在 :domain 中有一个拼写错误(你写成了 :domail),这可能会导致问题。
如果这样还不行,请尝试以下操作:
ActionMailer::Base.delivery_method = :smtp # be sure to choose SMTP delivery
ActionMailer::Base.smtp_settings = {
  :tls => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "gmail.com",
  :authentication => :plain,
  :user_name => "my_account_at_gmail@gmail.com", # use full email address here
  :password => "password"
}

2

另外,在Action Mailer Rails Edge Guide中建议将电子邮件配置放在config/environments目录下的相应.rb文件中。对于我来说,我将以下内容放在config/environments/development.rb中,以便使用gmail的SMTP服务器发送电子邮件:

config.action_mailer.raise_delivery_errors = true #useful to have to debug
config.action_mailer.perform_deliveries = true #default value
config.action_mailer.delivery_method = :smtp #default value

config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "yourdomain.com",
    :user_name => "username@yourdomain.com",
    :password => "yourpassword",
    :authentication => :login, #or can use "plain"
    :enable_starttls_auto => true
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接