require 'csv'

module MailsFromTemplate
  extend ActiveSupport::Concern

  def build_and_send_mails(substitution_file)
    CSV.parse(substitution_file.read, headers: :first_row) do |row|
      template = Liquid::Template.parse(@mail_template.body)
      body = template.render(row.to_hash)

      ActionMailer::Base.mail(from: "placements@sparkpostbox.com",
                              to: row['email'],
                              subject: @mail_template.subject,
                              body: body
                             ).deliver_later
    end
  end
end

require 'csv'

module MailsFromTemplate
  extend ActiveSupport::Concern

  def build_and_send_mails(substitution_file)
    CSV.parse(substitution_file.read, headers: :first_row) do |row|
      ActionMailer::Base.mail(from: "placements@sparkpostbox.com",
                              to: row['email'],
                              subject: @mail_template.subject,
                              body: @mail_template.body
                             ).deliver_later
    end
  end
end

Remove recipient-specific customization of body using Liquid
Remove recipient-specific customization of body using Liquid

SparkPost offers data substitution features that make it unnecessary
to handle this login in-app.

The enduser syntax is eerily similar to Liquid's. The main advantage
of using this method is that instead of sending many emails out
individually from the application, just one request is made to the
SparkPost API with a body and a JSON object with the substitution
data.

This especially makes a difference when sending mails with attachments,
but also helps to manage sent emails better.
