从JSON POST触发ActionMailer

时间:2017-07-11 11:58:58

标签: ruby-on-rails actionmailer

编辑:这是我来自Thomas'的工作代码。回答,还有其他错误需要修复。

我正在使用Rails API服务器,该服务器设置为为用户创建共享链接。我设置它,以便通过POST传递一些参数,我想发送邮件,然后保存记录。我能够保存记录,但我似乎无法触发邮件。

以下是控制器上的创建操作

def create
  @recipients = (share_params[:shared_with])
  @recipients.each do |recipient|
    ShareMailer
      .share_dealsheet(recipient)
      .deliver_now
    end
  @share = current_user.shares.create!(share_params)
  json_response(@share, :created)
end

ShareMailer类

class ShareMailer < ApplicationMailer

  default from: 'no-reply@dealsheetboard.com'

  def share_dealsheet(recipient)
    @recipient = recipient
    mail(to: @recipient, subject: "Test")
  end
end

还有一个样板模板保存为share_dealsheet.rb

如果我在Postman中POST以下内容,则会保存记录,但不会触发任何邮件程序(因此我假设其他邮件程序我在创建时使用控制台中的输出)。

{
    "token": "Sbdvmvt2VnzO0n3_thGSfZc1xkjs",
    "shared_with":{
        "1": "someemail@example.com",
        "2": "anotheremail@example.com"
    },
    "expiry": "30 June 2017"
}

1 个答案:

答案 0 :(得分:0)

您必须发送邮件,请参阅API以获取更多详细信息http://api.rubyonrails.org/v5.1/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Sending+mail

ShareMailer.share_dealsheet(share_params).deliver_now