caxlsx / caxlsx_rails

A Rails plugin to provide templates for the axlsx gem
MIT License
744 stars 84 forks source link

Mail body showing as attachment along with generated xlsx file. #120

Open vivekapex opened 5 years ago

vivekapex commented 5 years ago

Declaring layout: false renders the mail body as another attachment and on not declaring the same it shows Missing template layouts/mailer with {:locale=>[:en], :formats=>[:xlsx], :variants=>[], :handlers=>[:axlsx]}

Is there a way to have the correct attachment file along with the correct mail body and styling? Thanks for any help in advance.

straydogstudio commented 5 years ago

Is your mail template name the same as the email action name?

vivekapex commented 5 years ago

Yes.

vivekapex commented 5 years ago

Just for your reference.

class ReportMailer < ApplicationMailer
  default from: Rails.application.config.settings.mail.from

  def send_report reporting_task_id
    reporting_task = ReportingTask.find_by_id(reporting_task_id)
    scheduled_task = reporting_task.try(:get_scheduled_task)
    report         = scheduled_task.try(:schedulable)

    return if reporting_task.blank? || scheduled_task.blank? || report.blank?

    emails = scheduled_task.user_group.emails rescue nil
    return if emails.blank?

    scheduled_time   = scheduled_task.scheduled_time.in_time_zone( ENV['TIMEZONE'] || 'UTC' ).strftime('%I:%M %p')
    report_name_time = "#{report.name} as on #{reporting_task.output[:report_date]} #{scheduled_time}"
    subject          = "#{scheduled_task.name} ( #{report_name_time} )"

    data_file = render_to_string(
      layout: false, handlers: [:axlsx], formats: [:xlsx],
      template: 'reporting_tasks/scheduled_report_download',
      locals: { internal_name: report.internal_name, output: reporting_task.output }
    )

    attachments["#{report_name_time}.xlsx"] = {mime_type: Mime[:xlsx], content: Base64.encode64(data_file), encoding: 'base64'}
    mail( to: emails, subject: subject )
  end
end

Note that send_report is the email action name and scheduled_report_download is the axlsx ttemplate file name i'm generating.

straydogstudio commented 5 years ago

Try renaming the attachment template to something else.

straydogstudio commented 5 years ago

FYI, the template lookup mechanism appears to cache values and get confused. This is where the Rails "magic" breaks down. If the lookup process appears to match the action it will use what it has found. Renaming the attachment template has helped in other cases. If you would let us know your results.

vivekapex commented 5 years ago

Sorry but no luck with the above solution. I've tried with both ways a) Keeping layout: false. In this case it generates the report but the mail body is still coming as an attachment. b) On removing the layout: false option, it throws the same error Missing template layouts/mailer with {:locale=>[:en], :formats=>[:xlsx], :variants=>[], :handlers=>[:axlsx]}. Searched in:\n ...

    data_file = render_to_string(
      handlers: [:axlsx], formats: [:xlsx],
      template: 'reporting_tasks/test_template',
      locals: { internal_name: report.internal_name, output: reporting_task.output }
    )

Note the renamed template test_template above.

straydogstudio commented 4 years ago

@vivekapex Did you every have any success or did you move on?

vivekapex commented 4 years ago

Didn't have any success with this. The file attachment is working like charm but the mail content is still showing as another attachment. Working on different projects and had to move on from this. Thanks.

huzaifa-malik commented 4 years ago

I was having the same problem, My workaround was to simply

ac = ActionController::Base.new()
xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "path/to/template", locals: { locals: locals }
attachments["some reports.xlsx"] = { mime_type: Mime[:xlsx], content: xlsx}

Can't say if it's elegant or not.