【问题标题】:SendGrid and Rails ErrorSendGrid 和 Rails 错误
【发布时间】:2013-04-23 01:24:45
【问题描述】:

我正在尝试在我的 Rails 应用程序上设置 SendGrid,我一直在关注这个:http://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html

到目前为止,我已经设置了我的config/environment.rb

ActionMailer::Base.smtp_settings = {
  :user_name => *********, 
  :password => *********, 
  :domain => *********, 
  :address => "smtp.sendgrid.net",
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

创建了一个名为 app/models/notifier.rb 的新文件,其中包含

class Notifier < ActionMailer::Base
  include SendGrid
  default :from => "*****@******.com"

  # send a signup email to the user, pass in the user object that contains the user's email address
  def signup_email(user)
    mail( :to => user.email, :subject => "Thanks for signing up" )
  end
end

在 Rails 控制台中,会发生这种情况。

>> @new_user = User.new(:email => 'blah@blah.com')
 => #<User id: nil, email: "blah@blah.com", created_at: nil, updated_at: nil> 
>> @new_user.save
   (0.2ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "users" ("created_at", "email", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Tue, 23 Apr 2013 01:21:17 UTC +00:00], ["email", "blah@blah.com"], ["updated_at", Tue, 23 Apr 2013 01:21:17 UTC +00:00]]
   (1.8ms)  COMMIT
 => true 
>> Notifier.signup_email @new_user
ActionView::MissingTemplate: Missing template notifier/signup_email with "mailer". Searched in:
* "notifier"

我做错了什么?我不明白为什么我需要一个模板。我是 Rails 新手,如果这很明显,请原谅我。

【问题讨论】:

    标签: ruby-on-rails sendgrid


    【解决方案1】:

    问题是ActionMailer 默认使用Rails 中的模板。您应该查看the mailer guide for rails,它的详细信息比我在这里要描述的要多。

    要解决此问题,您应该在 app/views/notifier/signup_email.html.erb 中创建一个包含一些内容的模板。


    顺便说一句,如果您想在不使用模板的情况下发送电子邮件,则需要在创建邮件程序后立即调用deliver 函数。有关详细信息,请参阅此堆栈溢出问题:

    How can I send mail with rails without a template?

    【讨论】:

    • 感谢您的帮助!值得注意的是,在我将:body =&gt; "blah blah" 包含在我的邮件呼叫中之前,我一直收到模板错误。否则,它会寻找一个模板来填充正文。
    【解决方案2】:

    def 注册电子邮件(用户) mail( :to => user.email, :subject => "感谢您的注册" ) 结束

    替换为

    def 注册电子邮件(用户) mail( :to => user.email, :subject => "感谢您的注册", :body => "hello" ) 结束

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      相关资源
      最近更新 更多