我對 Ruby on Rails 和 SendGrid 還很陌生。
我不明白為什么下面的代碼示例不起作用?它確實使用正確的模板發送電子郵件,但沒有填寫動態資料。感謝任何輸入/見解。
require 'sendgrid-ruby'
include SendGrid
mail = SendGrid::Mail.new
mail.template_id = 'template id'
mail.from = Email.new(
email: '[email protected]',
name: 'ABC')
personalization = SendGrid::Personalization.new
personalization.add_to(Email.new(email: '[email protected]', name: 'ABC'))
personalization.add_dynamic_template_data(
'variable' => [
{ 'first_name' => 'John' },
{ 'last_name' => 'Doe' },
{ 'email' => '[email protected]' },
{ 'message' => 'The message is as follows' }
]
)
mail.add_personalization(personalization)
sgp = SendGrid::API.new(api_key: ENV['sendgrid_api_key'])
response = sgp.client.mail._('send').post(request_body: mail.to_json)
模板代碼如下所示。
Hello from {{first_name}} {{last_name}}.
{{message}}
結果最終沒有填充變數值。
Hello from .
uj5u.com熱心網友回復:
Twilio SendGrid 開發人員布道者在這里。
您添加的動態資料比模板更復雜。您可以只發送一個散列,而不是將散列陣列作為動態資料發送。試試這個:
personalization.add_dynamic_template_data({
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected]',
'message' => 'The message is as follows'
})
請參閱SendGrid Ruby 庫檔案中的此示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/314950.html
上一篇:元編程和參考
下一篇:json模式驗證不強制型別
