rails 3 actionmailer无法发送电子邮件

时间:2011-02-26 07:18:25

标签: ruby-on-rails actionmailer

我关注Ryan Bates's tutorial on 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 console中测试过: 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 个答案:

答案 0 :(得分: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"
}

答案 1 :(得分: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
  }