在没有模板的情况下发送 Sendgrid 电子邮件

问题描述 投票:0回答:2

我的 sendgrid 电子邮件发送正常,但模板从未加载。我用很多模板尝试了很多次,但从来没有调用过它们,所以我的调用一定有问题。

这是控制器的调用:

AdminMailer.with( email: "[email protected]" , id:item.id, name: item.name, room: item.room.name).send_stock.deliver_later

这是我的admin_mailer.rb

class AdminMailer < ApplicationMailer
  def send_stock
    mail(to: params[:email],
      subject: 'Foto sin inventario',
      body: 'email body',
      delivery_method_options: {
        api_key: ENV["SENDGRID_API"]
      },
      template_id: 'd-2d99d960ed47490497f1a99af32cd706', 
      dynamic_template_data:{
        'id': params[:id],
        'name': params[:name],
        'room': params[:room]
      }
    )
  end 

这是 rails 的输出:

[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] Performing ActionMailer::MailDeliveryJob (Job ID: d8927648-36ca-4e60-afd3-7fe6ea1b936f) from Async(mailers) enqueued at 2022-09-29T22:08:22Z with arguments: "AdminMailer", "send_stock", "deliver_now", {:params=>{:email=>"[email protected]", :id=>4, :name=>"foto 3 exclusive", :room=>"Sala 1"}, :args=>[]}
[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] AdminMailer#send_stock: processed outbound mail in 1.0ms
[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] Delivered mail [email protected] (7142.5ms)
[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] Date: Thu, 29 Sep 2022 17:08:23 -0500
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Foto sin inventario
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
template-id: d-2d99d960ed47490497f1a99af32cd706
dynamic-template-data: {:id=>4, :name=>"foto 3 exclusive", :room=>"Sala 1"}

email body
[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] Performed ActionMailer::MailDeliveryJob (Job ID: d8927648-36ca-4e60-afd3-7fe6ea1b936f) from Async(mailers) in 7150.18ms

找不到很多关于模板问题的文档。

ruby-on-rails sendgrid actionmailer
2个回答
0
投票

问题是你为这个函数调用提供了 body 属性,它告诉 SDK 你想用这个正文发送一封电子邮件。如果你想使用模板,你需要省略这个属性。

至少,这就是它在 node.js SDK 中的工作方式,根据这个示例代码,我假设 Ruby 的工作方式类似。


0
投票

你可以使用这个 gem https://github.com/eddiezane/sendgrid-actionmailer

添加到您的 gemfile:

gem 'sendgrid-actionmailer'

然后运行

bundle
.

然后在

application.rb

中添加以下内容
config.action_mailer.delivery_method = :sendgrid_actionmailer
config.action_mailer.sendgrid_actionmailer_settings = {
  api_key: ENV['SENDGRID_API_KEY'],
  raise_delivery_errors: true
}

这就是您的邮件程序的样子:

class AdminMailer < ApplicationMailer
  def send_stock
    mail(to: params[:email],
    body: '',
    subject: 'Foto sin inventario',
    template_id: 'd-2d99d960ed47490497f1a99af32cd706',
    dynamic_template_data: {
      'id': params[:id],
      'name': params[:name],
      'room': params[:room]
    })
  end 

您将使用通常的 ActiveMailer API 调用它:

AdminMailer.send_stock.deliver_later # or deliver_now

希望对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.