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

mailer attachement #12

Closed
gagalago opened this issue Jul 16, 2013 · 22 comments
Closed

mailer attachement #12

gagalago opened this issue Jul 16, 2013 · 22 comments

Comments

@gagalago
Copy link

How can i use axls_rails within a mailer to have a attachement ?

in the mailer :

class SocialSecretariatMailer < ActionMailer::Base
  def export
   attachments["Nouvelles entrées.xlsx"] = render xlsx: "/administration/contracts/social_secretariat", disposition: "attachment", filename: "nouvelles entrées - CESAM Nature - #{Date.today.to_s}.xlsx"
...

the error

Missing template social_secretariat_mailer/export with {:locale=>[:fr], :formats=[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xlsx], :handlers=>[:erb, :builder, :axlsx, :coffee, :haml, :prawn, :prawn_dsl]}. 
Searched in: (ActionView::MissingTemplate) * "/home/sigo/workspace/rcesam/app/views"

I feel that it is an error from the default rendered and not from the axls renderer.
Is it impossible to achieve this or I've made ​​a mistake ?

@straydogstudio
Copy link
Collaborator

Stupidly enough, I've never tried it. I may have to make some changes. I'll test it within a few days.

In the meantime, you can use the axlsx gem directly inside the mailer, rendering a pdf. That, however, removes the advantage of a template. Or, abstract that into some class method you use both in the template and the mailer.

@gagalago
Copy link
Author

ok, thanks

For the moment, I create the file with a controller and I write myself the mail.

@gagalago
Copy link
Author

how can i help you to implement/test/... this new fonctionnality ?

@straydogstudio
Copy link
Collaborator

I should get to it this week. But there is one thing you can do: Try it with an abs path that is only two deep. E.g. "/administration/social_secretariat" instead of "/administration/contracts/social_secretariat". I have some partially tested code for absolute paths, and I vaguely remember thinking that problems could come in certain circumstances. It it works with the short path, I know where to go. If it doesn't, then I'll have other digging to do.

@gagalago
Copy link
Author

for all test, with an abs path only two deep (with an without changing the location of the view) , I have this error:

Missing template social_secretariat_mailer/export with {:locale=>[:fr], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xlsx], :handlers=>[:erb, :builder, :axlsx, :coffee, :haml, :prawn, :prawn_dsl]}. 
Searched in:  * "/home/sigo/workspace/rcesam/app/views"

if i use the standart form of rendering (path 2 or 3 deep)
attachments["Nouvelles entrées.xlsx"] = render "administration/employees/social_secretariat.xlsx", :format=>:xlsx
I have an error that a method from a helper is undefined.I havn't this error if i use the render from the controller with the form of axlsx.

I hope that this information will help you. I am always available to do other tests.

@straydogstudio
Copy link
Collaborator

Thanks. I'll use this and let you know if I can use any more testing.

@straydogstudio
Copy link
Collaborator

@gagalago Ok I think I have this figured out. It isn't really a bug. We just can't call render inside a mailer that way. It is trying to find out the template based on the current method/class; since your mailer is SocialSecretariatMailer.export, it looks for a social_secretariat_mailer/export template. This is Rails magic; my code isn't even being touched. (Edit: Looks like it was a typo, using :xlsx instead of :axlsx.)

However, you can use render_to_string and specify the template directly. Try something like this:

def export
  xlsx = render_to_string handlers: [:xlsx], template: "/administration/contracts/social_secretariat"
  attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
  ...
end

Let me know if that works for you. If it does I'll close the issue.

Note that you don't have to specify handlers if there is only one "administration/contracts/social_secretariat" file with the xlsx.axlsx extension. If you also have a social_secretariat.pdf.prawn file, for example, it will pick whichever one it understands first.

I've added tests and will be updating the docs sometime today or tomorrow. Thanks for submitting the issue.

@gagalago
Copy link
Author

With your instruction, I have this error :
undefined method render_to_string' for main:Object``

@straydogstudio
Copy link
Collaborator

What rails are you using? I'm still fixing the tests for Rails 4.

@straydogstudio
Copy link
Collaborator

@gagalago Send me your whole mailer code if you can. I'm not finding any way to reproduce your error.

@straydogstudio
Copy link
Collaborator

@gagalago You might try something like:

def export
  ac = ActionController::Base.new()
  xlsx = ac.render_to_string handlers: [:xlsx], template: "/administration/contracts/social_secretariat"
  attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
  ...
end

@gagalago
Copy link
Author

I use rails 3.2.11 and my mailer code:

# coding: utf-8
class SocialSecretariatMailer < ActionMailer::Base
  default :from => "Didier Leser - CESAM Nature <didier.leser@cesam-nature.com>"

  def export
    to=["info@cesam-nature.com", ""]
    content_type = "multipart/alternative"
    attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: new_entries_file}

    subject="xlsx: CESAM Nature"
    mail(:to=>to,:subject=>subject) do |format|
      format.text { render :text => "xlsx CESAM Nature" }
    end
  end

  private
  def new_entries_file
    save_xlsx(Employee.not_deleted.confirmed, "employés nouvelles entrées")
  end

  def save_xlsx(employees, title)
    if employees.size>0
      @employees = employees
      ac = ActionController::Base.new()
      xlsx = ac.render_to_string handlers: [:xlsx], template: "/administration/employees/social_secretariat"

      xlsx
    end
  end
end

with this new syntax, I have this error:

Missing template administration/employees/social_secretariat with {:locale=>[:fr], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xlsx], :handlers=>[:xlsx]}. 
Searched in: (ActionView::MissingTemplate)

If i use a mix of controller and your mailer syntax:

ac = ActionController::Base.new()
xlsx = ac.render_to_string :xlsx => "/administration/contracts/social_secretariat"
attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}

I get an error that says that a helper function is unknown

undefined method `id_for_social_secretariat' for #<#<Class:0x00000006b44050>:0x00000006b44f50> (ActionView::Template::Error)

I hope that this new information will help us to solve these problems.

ps: I'm still here tomorrow and then I leave for two weeks holiday

@straydogstudio
Copy link
Collaborator

Ugh. After all this, I think I sent you a typo at the first. I hope this is it, (and I'll be embarrassed too.) Try this:

  def export
    to=["info@cesam-nature.com", ""]
    content_type = "multipart/alternative"
    xlsx = render handlers: [:axlsx], template: "/administration/contracts/social_secretariat"
    attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
    subject="xlsx: CESAM Nature"
    mail(:to=>to,:subject=>subject) do |format|
      format.text { render :text => "xlsx CESAM Nature" }
    end
  end

Notice handers specifies :axlsx not :xlsx. Let me know how it goes!

I still don't understand why render_to_string isn't available for you, and all my web searching has not been fruitful on that point.

@straydogstudio
Copy link
Collaborator

Using ActionController like that is a hack to me. That didn't work because I was using Rails 2 syntax. If, perchance, you need to generate a view outside of a controller, this is how you would do it for Rails 3:

      av = ActionView::Base.new()
      av.view_paths = ActionController::Base.view_paths
      av.extend ApplicationHelper #Here you would include the id_for_social_secretariat helper
      av.assign employees: employees
      xlsx = av.render handlers: [:axlsx], template: "/administration/contracts/social_secretariat"

Note in one of your posts you use "/administration/employees/social_secretariat" and in all others "/administration/contracts/social_secretariat". Make sure you've got the right path!

@straydogstudio
Copy link
Collaborator

@gagalago: I haven't heard from you in a while. Do you have any more information? Is it working for you?

@gagalago
Copy link
Author

thanks. All works !

And my code :

# coding: utf-8
class SocialSecretariatMailer < ActionMailer::Base
  default :from => "Thibault Poncelet - CESAM Nature <thibault.poncelet@cesam-nature.com>"

  def export
    #to=["info@cesam-nature.com", "gagalago@gmail.com"]
    to = ["gagalago@gmail.com"]
    content_type = "multipart/alternative"
    attachments["Nouvelles entrées.xlsx"] = {mime_type: Mime::XLSX, content: new_entries_file}

    subject="xlsx: CESAM Nature"
    mail(:to=>to,:subject=>subject) do |format|
      format.text { render :text => "xlsx CESAM Nature" }
    end
  end

  private
  def new_entries_file
    save_xlsx(Employee.not_deleted.confirmed.new_export, "employés nouvelles entrées")
  end

  def save_xlsx(employees, title)
    if employees.size>0
      av = ActionView::Base.new()
      av.view_paths = ActionController::Base.view_paths
      av.extend SocialSecretariatHelper
      av.assign employees: employees
      av.render handlers: [:axlsx], template: "/administration/employees/social_secretariat"
    end
  end
end

Don't forget to update your documentation ;-)

sorry for answering so late and thank you again.

@OpenCoderX
Copy link
Member

When I use this method I get the content of the file inline in email.

@straydogstudio
Copy link
Collaborator

@chrisgogreen Create a new issue, and post your mailer code in it.

@mejiaro
Copy link

mejiaro commented May 6, 2016

In case someone else is still having this problem with the syntax provided in the README, I got my template working using the following lines in the mailer:

xlsx = render_to_string formats: [:xlsx], template: "client/default/report", locals: {money: @money}
    attachments['report.xlsx'] = {mime_type: Mime::XLSX, content: xlsx}

The template file is named report.xlsx.axlsx

@straydogstudio
Copy link
Collaborator

@mejiaro Just to be clear, you dropped the handlers specifier? Which version of Rails, Axlsx, axlsx_rails?

@mejiaro
Copy link

mejiaro commented May 6, 2016

Yup, just removed the handlers. rails (4.2.5.1), axlsx (2.0.1), axlsx_rails (0.4.0)

@straydogstudio
Copy link
Collaborator

@mejiaro Thanks. I will update the docs at least.

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

4 participants