rails,动作邮件不发送

时间:2013-05-25 21:48:24

标签: ruby-on-rails email actionmailer

我是Rails的初学者,我在尝试使用动作制作者注册后发送电子邮件。

我的日志说电子邮件正在发送,但gmail永远不会收到它

配置/初始化/为setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "asciicasts.com",
  :user_name            => "asciicasts",
  :password             => "secret",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

寄件人/ user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "eifion@asciicasts.com"

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

控制器/ users_controller.rb

...
def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
...

谢谢!

3 个答案:

答案 0 :(得分:17)

确保在config/environments/development.rb

中设置了此选项

config.action_mailer.delivery_method = :smtp

此外,在ActionMailer::Base.smtp_settings中,您需要指定有效的Gmail帐户。复制粘贴(asciicasts)不会在这里削减它。

请参阅此问题以供参考:Sending mail with Rails 3 in development environment

答案 1 :(得分:4)

您可以使用'sendmail'

代替'smtp'
ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
     :port => "587", :domain => "gmail.com", :user_name => "xxx@gmail.com", 
    :password => "yyy", :authentication => "plain", :enable_starttls_auto => true }

答案 2 :(得分:0)

我为我设置的新邮件遇到了同样的问题。我无法弄清楚为什么这个新的邮件程序无法发送电子邮件,甚至在我介绍邮件时在邮件程序中找到方法。

解决方案

最后,如果您将deliver_nowdeliver*代码放在邮件程序中,它就不会发送电子邮件。

示例破碎

  def email_message()
    message = mail(to: User.first, subject: 'test', body: "body text for mail")
    message.deliver_now
  end

校正的

  #Different class; in my case a service
  def caller
    message = MyMailer.email_message
    message.deliver_now
  end

  def email_message()
    mail(to: User.first, subject: 'test', body: "body text for mail")
  end

这解决了我的问题,希望能为别人解决。