我试图在我的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"
我做错了什么?我不明白为什么我需要一个模板。
问题是ActionMailer
默认在Rails中使用模板。您应该查看rails的mailer指南,它有比我在这里描述的更多的细节。
要解决这个问题,您应该在app/views/notifier/signup_email.html.erb
中创建一个包含一些内容的模板。
顺便说一句,如果您希望在不使用模板的情况下发送电子邮件,则需要在创建邮件发送器后立即调用deliver
函数。有关详细信息,请参阅此堆栈溢出问题:
如何在没有模板的情况下使用rails发送邮件?
def signup_email(user)邮件(:to => user)。电子邮件,:subject => "感谢您的报名")
结束由替换
def signup_email(用户)邮件(:to => user)。电子邮件,:subject => "感谢您的报名",:body => "hello")结束