大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 使用SendGrid模板的Rails Mailer,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
user_controller.rb
def create @user = User.new(user_params) respond_to do |format| if @user.save format.html { redirect_to @user,notice: 'User was successfully created.' } format.json { render :show,status: :created,LOCATIOn: @user } header = Smtpapi::Header.new header.add_to(@user.email) header.add_substitution('user',[@user.name]) header.add_substitution('body',"You've registered! This is from the controller.") header.add_filter('templates','enable',1) header.add_filter('templates','template_id','xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx') header.to_json UserNotifier.welcome(header).deliver else format.html { render :new } format.json { render json: @user.errors,status: :unprocessable_entity } end end end
邮寄/ user_notifier.rb
class UserNotifier < ApplicationMailer default from: "test@test.com" def welcome(header) headers['X-SMTPAPI'] = hdr.to_json mail(subject: "Welcome to the site!") end end
视图/ user_notifier / welcome.html.erb
<html> <body> Hi -user-<br /> Thanks so much for joining us! <p>-body-</p> Thanks,<br /> The Microblog Team </body> </html>
我在SendGrid活动日志中没有看到任何内容,所以它甚至没有被发送到那里,至少我的猜测是这样.
我究竟做错了什么?
您应该将标头元数据直接包含在UserNotifier中:
headers "X-SMTPAPI" => { "sub": { "%name%" => [user.name] },"filters": { "templates": { "setTings": { "enable": 1,"template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' } } } }.to_json # the 'to' email can be overridden per action mail( from: 'test@test.com',to: 'recipient@test.com',subject: "Hello World" )
如果UserNotifier.welcome用于应用的其他部分,您也可以传递内容:
UserNotifier.welcome(user: @user,subject: "Welcome!").deliver_now # user_notifier.rb class UserNotifier < ApplicationMailer default from: "test@test.com" def welcome(user:,subject:,template: "default" ) # template's default view is "default" headers "X-SMTPAPI" => { "sub": { "%name%" => [user.name] },"filters": { "templates": { "setTings": { "enable": 1,"template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' } } } }.to_json mail( from: 'test@test.com',to: user.email,subject: subject,template_path: 'path/to/view',template_name: template ) # this would try to render the view: `path/to/view/default.erb` end end
在模板中,您可以通过添加标记的名称来包含替换标记:
<h1>Hello %name%!</h1>
More information about substitution tags
查看using their template system上的Sendgrid文档
以上是大佬教程为你收集整理的ruby-on-rails – 使用SendGrid模板的Rails Mailer全部内容,希望文章能够帮你解决ruby-on-rails – 使用SendGrid模板的Rails Mailer所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。