Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Emails with attachments don't work as documented

#1936

Trying to send an email which has both a plain text body and a file attachment, as shown in the documentation for ActionMailer, but the local assigns are nil when the body view is rendered.

Have been speaking with Koz about this elsewhere for a little while and we decided to add it here as a ticket instead.

I've attached a full sample app, but here's the mailer:


class Mail < ActionMailer::Base
  
  def daily_report
    recipients      "me@example.com"
    subject         "Daily Report"
    from            "me@example.com"
    content_type    "multipart/alternative"
    
    part :content_type => "text/plain", 
         :body => render_message("daily_report", :date => Time.now, :recipient => "Bob")

    attachment :content_type => "text/csv", :filename => "hello", :body => "my,cool,csv"
    
  end

end

And the view:


<%= @recipient %>,

Please see the attached report, created for <%= @date.strftime("%Y %m %d") %>.

An error is raised on nil.strftime, which I think means the local assigns are not working as advertised.

Reported by Justin French · February 11th, 2009 @ 04:11 AM

State: resolved
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.

    I can reproduce the breakage for mail.zip here as well.

    However the attached patch shows that the tests pass when we test that functionality. So something is very strange.

    February 11th, 2009 @ 04:14 AM

  2. Justin French
  3. Michael Koziarski
    Michael Koziarski
    • Assigned user changed from Michael Koziarski to josh

    I can confirm this error, but everything on the AM side seems 'right'. Any ideas josh? Can you reproduce the error with the zip file?

    February 11th, 2009 @ 04:18 AM

  4. josh
    josh
    • State changed from new to open
    • Assigned user changed from josh to Michael Koziarski

    I can confirm the error in the sample app.

    I'm not sure how to fix it, but it looks like the error is around this line.

    
            # Then, if there were such templates, we check to see if we ought to
            # also render a "normal" template (without the content type). If a
            # normal template exists (or if there were no implicit parts) we render
            # it.
            template_exists = @parts.empty?
            template_exists ||= template_root["#{mailer_name}/#{@template}"]
            @body = render_message(@template, @body) if template_exists
    

    The template renders fine the first time with the explicit render_message. Because "daily_report.erb" exists, it is setting template_exists to true and trying to render the template a second time.

    February 16th, 2009 @ 08:07 PM

  5. Michael Koziarski
    Michael Koziarski

    Yeah, it seems it's missing a performed? style check. But I'm not sure that we can change that too easily without potentially breaking people who are reying on the implicit rendering of the body?

    February 17th, 2009 @ 01:41 AM

  6. Michael Koziarski
    Michael Koziarski
    • Milestone set to 2.x

    It's had this problem all along, so I'll punt on this for now.

    February 22nd, 2009 @ 02:32 AM

  7. rdavila
    rdavila

    It works fine for me in 2.3.5.

    April 21st, 2010 @ 09:11 PM

  8. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to resolved

    April 24th, 2010 @ 10:09 PM