secador de pelo
secador de pelo

Reputation: 709

Rails actionmailer: attachments from database

I'm using actionmailer to send mails in rails. I want to attach multiple attachments:

def prepare_attachments(languages)
  attachments = {}
  languages.each do |language|
    next unless language.document
    attachments[language.document.filename] = language.document.read
  end

  return attachments
end

def distribution_email(recipient, languages)
  attachments = self.prepare_attachments(languages)

  mail(
      :to => recipient,
      :subject => 'Test'
  )
end

The delivered mail doesn't contain any attachment. This is working:

def distribution_email(recipient, languages)
  attachments['test.pdf'] = File.read("/tmp/test.pdf")

  mail(
      :to => recipient,
      :subject => 'Welcome to My Awesome Site'
  )
end

What am i doing wrong?

Upvotes: 0

Views: 287

Answers (1)

secador de pelo
secador de pelo

Reputation: 709

I fount the solution, one must not override attachment:

def prepare_attachments(languages)
  attachments = {}
  languages.each do |language|
    next unless language.document
    attachments[language.document.filename] = language.document.read
  end

  return attachments
end

def distribution_email(recipient, languages)
  self.prepare_attachments(languages).each do |filename, content|
    attachments[filename] = content
  end

  mail(
      :to => recipient,
      :subject => 'Test'
  )
end

Upvotes: 1

Related Questions