Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vivekapex opened this issue Jun 13, 2019 · 9 comments
Open

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

vivekapex opened this issue Jun 13, 2019 · 9 comments

Comments

@vivekapex
Copy link

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
Copy link
Collaborator

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

@vivekapex
Copy link
Author

Yes.

@vivekapex
Copy link
Author

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
Copy link
Collaborator

Try renaming the attachment template to something else.

@straydogstudio
Copy link
Collaborator

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
Copy link
Author

vivekapex commented Jun 14, 2019

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
Copy link
Collaborator

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

@vivekapex
Copy link
Author

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants