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

am46iovg  于 2023-04-22  发布在  Ruby
关注(0)|答案(2)|浏览(167)

我的sendgrid电子邮件正在发送确定,但模板从来没有加载。我已经尝试了多次与许多模板,但他们从来没有被调用,所以一定是有什么问题,我的调用。
这是来自控制器的调用:

AdminMailer.with( email: "xxxxxxxxx@gmail.com" , 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=>"xxxxxxxxx@gmail.com", :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 6336175785e4_366932af97af0d39c27826@miguel.mail (7142.5ms)
[ActiveJob] [ActionMailer::MailDeliveryJob] [d8927648-36ca-4e60-afd3-7fe6ea1b936f] Date: Thu, 29 Sep 2022 17:08:23 -0500
From: xxxxxxxxx@gmail.com
To: xxxxxxxxx@gmail.com
Message-ID: <6336175785e4_366932af97af0d39c27826@miguel.mail>
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

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

gpnt7bae

gpnt7bae1#

问题是,您为这个函数调用提供了body属性,它告诉SDK您想要使用此body发送电子邮件。如果您想要使用模板,则需要省略此属性。
至少,这就是它在node.js SDK中的工作方式,根据此示例代码,我假设Ruby的工作方式类似。

disbfnqx

disbfnqx2#

您可以使用此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

希望有帮助!

相关问题