【问题标题】:rails 3 actionmailer cannot send emailrails 3 actionmailer 无法发送电子邮件
【发布时间】:2011-03-29 09:15:36
【问题描述】:

我关注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 控制台中测试过: 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 文件有问题..

【问题讨论】:

    标签: ruby-on-rails actionmailer


    【解决方案1】:

    如果这是您的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
        }
      

      【讨论】:

        猜你喜欢
        • 2011-08-11
        • 2014-12-31
        • 2013-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多