Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach active storage file to mailer

I have tried everything and still can't get this to work.

I have two types of application in my system that are pre-qualified and sent to lenders,

1) one generates a pdf 2) second should use active storage attachments and attach them to an ActionMailer

First one is working the second is giving me the following error:

[ActionMailer::DeliveryJob] [905177a5-b0e9-46f4-ba9a-fc4630e873f9] Error performing ActionMailer::DeliveryJob (Job ID: 905177a5-b0e9-46f4-ba9a-fc4630e873f9) from Async(mailers) in 140.14ms: Errno::ENOENT (No such file or directory @ rb_sysopen - https://funderhunt.co/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZ1lIIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fa91a15681c23d47d767169c7821601aa15ed2b3/Statuses.pages?disposition=attachment):

The link is correct tho:

My mailer code for this part looks like this:

      q = 0
      statement.files.each do |file|
        q += 1
        bank_statement = File.read(rails_blob_url(file, disposition: "attachment"))
        attachments["statement_#{q}.pdf"] = { :mime_type => 'application/pdf', :content => bank_statement }
      end

What is wrong? Can you please help. Thanks in advance.

like image 460
Ermek Rysbek Uulu Avatar asked Dec 17 '22 19:12

Ermek Rysbek Uulu


2 Answers

If somebody lands here looking for a general solution:

modelname.attachments.each do |file|
  attachments[file.blob.filename.to_s] = {
    mime_type: file.blob.content_type,
    content: file.blob.download
  }
end
like image 101
Markus Andreas Avatar answered Dec 29 '22 08:12

Markus Andreas


You should be able to do something like,

statement.files.each_with_index do |file, q|
  attachments["statement_#{q + 1}.pdf"] = { mime_type: 'application/pdf', content: file.blob.download }
end

file.blob.download will return the content of the file, similar to File.read.

like image 21
Josh Brody Avatar answered Dec 29 '22 07:12

Josh Brody