Rails 使用 SendGrid 模板的邮件程序
Rails Mailer using SendGrid Template
如何使用 smtpapi-ruby gem 将我的 rails 邮件程序绑定到 Sendgrid?我关注了他们的 limited documentation,但我的电子邮件没有通过,我已经验证我的 SendGrid 实现在发送普通电子邮件时工作正常,所以不是这样。这是我的:
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
mailers/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
views/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 activity 日志中没有看到任何内容,所以它甚至没有被发送到那里,至少这是我的猜测。
我做错了什么?
您必须将邮件程序中的代码更改为:
class UserNotifier < ApplicationMailer
default from: "test@test.com"
def welcome(hdr)
headers['X-SMTPAPI'] = hdr.asJSON()
mail(subject: "Welcome to the site!")
end
end
示例:https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html
我认为您混淆了变量。您调用 hdr.to_json
并且参数名称是 header
也已经转换为 json.
您应该将 header 元数据直接包含到 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>
上的 Sendgrid 文档
如何使用 smtpapi-ruby gem 将我的 rails 邮件程序绑定到 Sendgrid?我关注了他们的 limited documentation,但我的电子邮件没有通过,我已经验证我的 SendGrid 实现在发送普通电子邮件时工作正常,所以不是这样。这是我的:
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
mailers/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
views/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 activity 日志中没有看到任何内容,所以它甚至没有被发送到那里,至少这是我的猜测。
我做错了什么?
您必须将邮件程序中的代码更改为:
class UserNotifier < ApplicationMailer
default from: "test@test.com"
def welcome(hdr)
headers['X-SMTPAPI'] = hdr.asJSON()
mail(subject: "Welcome to the site!")
end
end
示例:https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html
我认为您混淆了变量。您调用 hdr.to_json
并且参数名称是 header
也已经转换为 json.
您应该将 header 元数据直接包含到 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>
上的 Sendgrid 文档