Open OpenCoderX opened 9 years ago
Do you have any text in the body of your message? E.g. do you have a template for daily_coding_status
? Perhaps it is confused at having no content.
What rails are you using?
Rails 3.2. That was indeed the solution. Once I added html and text templates and a proper do format block in my call to mail(), it worked.
Thanks
@chrisgogreen ran into the same issue and i did have an html template for my mailer. I used this workaround:
xlsx = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "releases/order_report"
attachments["DailyOrderReport.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
self.instance_variable_set(:@_lookup_context, nil)
mail(to: recipients, subject: "Daily Order Report")
@ph0t0n Which Rails version?
@ph0t0n I think you need to pass a block to mail()like this:
mail(:subject => "#{@date} Status Report") do |format| format.html { render 'template_name' } format.text { render :text => 'template_name.txt.erb' } end
Why don't you need a template? You could use an empty template, instance_variable_set smells fishy.
@ph0t0n @chrisgogreen According to the ActionMailer documentation the block isn't required. So I'd like to isolate what is going on either way.
I agree, I don't like having to reset the lookup context. It shouldn't be necessary. Recently Rails has started caching certain aspects of the rendering process to make things more efficient. I would not be surprised if it is related.
@ph0t0n What Rails are you running?
My complete version number is 3.2.19. Let me know what else I can check for you.
Same thing happening on 3.2.18. Adding self.instance_variable_set(:@_lookup_context, nil) fixes the problem.
Same on ruby 2.1.7
, rails 3.2.12
.
Adding self.instance_variable_set(:@_lookup_context, nil)
fixes the problem.
Another solution?
Instead self.instance_variable_set(:@_lookup_context, nil)
I could do use render_to_string
again, this time just to force the html format, adding formats: [:html]
, notice I don't actually use the return value though its result will be (somehow?) used as the mail content.
render_to_string(formats: [:html], :action => "path/to/action", :layout => false)
mail(subject: "Test", to: email)
The mail content will be taken from the render_to_string
result,
For empty email:
render_to_string(formats: [:html], text: nil, :layout => false)
Or if you want the default action path, you can remove the action option:
render_to_string(formats: [:html], :layout => false)
I use this, without even the layout option (since I didn't define layout?)
render_to_string(formats: [:html])
@Roko131 Thanks for this result. That makes sense now why the first call to render to string is showing up in the message if your second render to string is producing the message. I'll have to dig into the lookup context to see what is going on sometime.
Mail is going but in attachment, a sheet is not opening.
My rails version: 4.2 Ruby Version: 2.2.4
My mailer action: def send_timesheet_summary_report(project_employee, projects_summary, employee_summary) data_file = render_to_string( layout: false, handlers: [:axlsx], formats: [:xlsx], template: 'time_sheets/export_project_report', locals: { project_employee: project_employee, projects_summary: projects_summary, employee_summary: employee_summary } ) attachment = Base64.encode64(data_file) attachments["Timesheet_summary_report.xlsx"] = {mime_type: Mime[:xlsx], content: attachment, encoding: 'base64'} self.instance_variable_set(:@_lookup_context, nil) mail( subject: "Timesheet summary report", to: "vaibhav@example.com") end
and my action name and template name is different
This is the email body:
This is my mailer:
This is the template 'status.xlsx.axlsx':
I can't see what I'm doing incorrectly.