Reputation: 2308
What is the correct syntax for sending an email with actionmailer that includes some PDF file attachments? I am using Gmail for SMTP, with the TLS plugin. Here is what I have so far (and have tried variations on this too):
**lead_mailer.rb:**
def auto_response(lead)
recipients lead.email
subject "Information"
body :recipient => lead.first_name
from "[email protected]"
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 the attachments, but they show up as "no name", even though upon opening them they appear correctly. However, the body of the email does not appear at all. I am sure I am doing something simple, wrong.
Upvotes: 3
Views: 1658
Reputation: 2308
Okay I stepped away for a moment and came back and googled a little more and got the answer!
From the API:
Implicit template rendering is not performed if any attachments or parts have been added to the email. This means that you‘ll have to manually add each part to the email and set the content type of the email to multipart/alternative.
For the main mailer method I switched out body and added this explicit render. Note- I skipped the multipart/alternative and it worked, most likely because I am sending a plain text email.
part :body => render_message('auto_response', :recipient => lead.first_name)
For the attachment naming issue, this is what I did:
attachment "application/pdf" do |a|
a.body = File.read(RAILS_ROOT + "/public/files/OtherSheet.pdf")
a.filename = "Othersheet.pdf"
end
Upvotes: 3