我试图在Rails应用程序的客户预订时使用Sendgrid发送事务性电子邮件。
我确实根据需要创建了一个动态模板。当我使用正确的主体和我的Sendgrid密钥向https://api.sendgrid.com/v3/mail/send发出POST请求时,我确实收到了确认邮件。
但是,我是Rails新手,我真的不知道如何处理文档中提供的代码:
require 'sendgrid-ruby'
include SendGrid
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'], impersonate_subuser: The subuser's username. This header generates the API call as if the subuser account was making the call.)
data = JSON.parse('{
"name": "example_name",
"generation": "dynamic"
}')
response = sg.client.templates.post(request_body: data)
puts response.status_code
puts response.headers
puts response.body提前感谢您的帮助!
发布于 2022-02-05 08:08:23
Twilio SendGrid开发人员在这里传道。
查看这里的文档显示了您可以用Ruby库发送电子邮件的所有选项 (原谅使用include)。
基于这些文档,下面是一个应该满足您的需求的快速片段:
require 'sendgrid-ruby'
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
mail = SendGrid::Mail.new
mail.from = SendGrid::Email.new(email: "YOUR_FROM_ADDRESS")
mail.subject = "Your fancy, exciting subject line"
mail.template_id = "YOUR_TEMPLATE_ID"
personalization = SendGrid::Personalization.new
personalization.add_to(SendGridEmail.new(email: 'YOUR_CUSTOMER_EMAIL'))
personalization.add_dynamic_template_data({
"name" => "example_name",
"generation" => "dynamic"
})
mail.add_personalization(personalization)
sg.client.mail._("send").post(request_body: mail.to_json)https://stackoverflow.com/questions/70991366
复制相似问题