ActionMailer problem - proper pdf attachment syntax

What is the correct syntax for sending emails using actionmailer that includes some PDF file attachments? I use Gmail for SMTP, with a TLS plugin. Here is what I have so far (and also tried the options):

**lead_mailer.rb:**

  def auto_response(lead)
    recipients  lead.email
    subject     "Information"
    body        :recipient => lead.first_name
    from        "me@mydomain.com"
    attachment  "application/pdf" do |a|
                a.body = File.read(RAILS_ROOT + "/public/files/Datasheet.pdf")
    end
    attachment  "application/pdf" do |a|
                a.body = File.read(RAILS_ROOT + "/public/files/OtherSheet.pdf")                
    end
  end

**lead_observer.rb:**

class LeadObserver < ActiveRecord::Observer
  def after_save(lead)
    mail = LeadMailer.create_auto_response(lead)
    LeadMailer.deliver(mail)
  end
end

The problem is that it sends attachments, but they are displayed as “no name”, although when they are opened, they are displayed correctly. However, the body of the letter does not appear at all. I am sure that I am doing something simple, wrong.

+3
source share
1 answer

Well, I stepped back for a moment, returned and looked again for a walk and got an answer!

From the API:

, . , /.

. . /, , , .

part        :body => render_message('auto_response', :recipient => lead.first_name) 

, :

attachment  "application/pdf" do |a|
                a.body = File.read(RAILS_ROOT + "/public/files/OtherSheet.pdf") 
                a.filename = "Othersheet.pdf"  
end
+3

Source: https://habr.com/ru/post/1702939/


All Articles