Rectangle 27 9

Here is my model Report. I'm using Paperclip.

class Report < ActiveRecord::Base
  has_attached_file :pdf_file
  ...
end
class ReportMailer < ActionMailer::Base
  def monthly_report_email(emails, report)
    attachments[report.pdf_file_file_name] = File.read(report.pdf_file.path)
    mail(:to => emails, :subject => 'monthly report')
  end
end

ruby on rails - paperclip + ActionMailer - Adding an attachment? - Sta...

ruby-on-rails ruby-on-rails-3 paperclip actionmailer
Rectangle 27 4

From the Ruby on Rails guides (only reachable through Bing):

All that is left is to download the attachment (if in S3) to a file object or access it it is stored locally. Try using open-uri.

thanks, but it's the opening of a paperclip file I can't figure out

So I need to download the S3 file and then read it?? I can't just access it and create the attachment?

Yes. You must have the file downloaded to attach it.

thanks. I'm trying that but no luck yet. I udpdated with the code any ideas?

ruby on rails - paperclip + ActionMailer - Adding an attachment? - Sta...

ruby-on-rails ruby-on-rails-3 paperclip actionmailer
Rectangle 27 18

After messing around some more, was able to get it working. Below is what worked for me - hopefully it helps someone. The key was using open uri since the quiz files are on S3.

class AdminMailer < ActionMailer::Base

require 'open-uri'

default from: "sales@mydomain.com"

def admin_upload_notification_email(quiz, current_user)
  @url  = "http://mydomain.com"
  @quiz = quiz
  @user = current_user
  attachments["#{quiz.quiz_path_file_name}"] = open("#{quiz.quiz_path.expiring_url(60)}").read

  mail(to: "admin@mydomain.com", :subject => "New Upload From #{@user.email}")
end


end

ruby on rails - Trouble adding a file stored on S3 through Paperclip a...

ruby-on-rails paperclip actionmailer
Rectangle 27 18

After messing around some more, was able to get it working. Below is what worked for me - hopefully it helps someone. The key was using open uri since the quiz files are on S3.

class AdminMailer < ActionMailer::Base

require 'open-uri'

default from: "sales@mydomain.com"

def admin_upload_notification_email(quiz, current_user)
  @url  = "http://mydomain.com"
  @quiz = quiz
  @user = current_user
  attachments["#{quiz.quiz_path_file_name}"] = open("#{quiz.quiz_path.expiring_url(60)}").read

  mail(to: "admin@mydomain.com", :subject => "New Upload From #{@user.email}")
end


end

ruby on rails - Trouble adding a file stored on S3 through Paperclip a...

ruby-on-rails paperclip actionmailer
Rectangle 27 1

You can use the Asset pipeline to attached the actual file path. Have not tested. "#{Rails.root}/#{YourAppName::Application.assets.find_asset('avatar.jpg').pathname}" You can find the answer here share|improve this answer edited May 23 at 12:06 Community♦ 11 answered Apr 19 '14 at 21:55 cevaris 3,62412926

The problem with this is I would need to know the actual filename. I don't know the filenames for people that upload their avatars. I am only saving the attachment as avatar.jpg. Not finding a file avatar.jpg

I had though paperclip renames the file? Or at least persists the path/location of it? May be wrong.

localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965
user.avatar.path

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 1

You can use the Asset pipeline to attached the actual file path. Have not tested. "#{Rails.root}/#{YourAppName::Application.assets.find_asset('avatar.jpg').pathname}" You can find the answer here share|improve this answer edited May 23 at 12:06 Community♦ 11 answered Apr 19 '14 at 21:55 cevaris 3,67412926

The problem with this is I would need to know the actual filename. I don't know the filenames for people that upload their avatars. I am only saving the attachment as avatar.jpg. Not finding a file avatar.jpg

I had though paperclip renames the file? Or at least persists the path/location of it? May be wrong.

localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965
user.avatar.path

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 1

class Petition < ApplicationRecord
  has_attached_file :contract
end

In mailer, I attached this contract file like this. Hope this helps.

def send_invitation(petition)
  attachments[petition.contract_file_name] = File.read(petition.contract.path)
  mail(
    to:       @petition.email,
    subject: t('mailer.send_invitation')) do |format|
    format.html { render 'send_invitation' }
end

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 1

class Petition < ApplicationRecord
  has_attached_file :contract
end

In mailer, I attached this contract file like this. Hope this helps.

def send_invitation(petition)
  attachments[petition.contract_file_name] = File.read(petition.contract.path)
  mail(
    to:       @petition.email,
    subject: t('mailer.send_invitation')) do |format|
    format.html { render 'send_invitation' }
end

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 0

After messing around some more, was able to get it working. Below is what worked for me - hopefully it helps someone. The key was using open uri since the quiz files are on S3.

class AdminMailer < ActionMailer::Base

require 'open-uri'

default from: "sales@mydomain.com"

def admin_upload_notification_email(quiz, current_user)
  @url  = "http://mydomain.com"
  @quiz = quiz
  @user = current_user
  attachments["#{quiz.quiz_path_file_name}"] = open("#{quiz.quiz_path.expiring_url(60)}").read

  mail(to: "admin@mydomain.com", :subject => "New Upload From #{@user.email}")
end


end

ruby on rails - Trouble adding a file stored on S3 through Paperclip a...

ruby-on-rails paperclip actionmailer
Rectangle 27 0

From the Ruby on Rails guides (only reachable through Bing):

All that is left is to download the attachment (if in S3) to a file object or access it it is stored locally. Try using open-uri.

thanks, but it's the opening of a paperclip file I can't figure out

So I need to download the S3 file and then read it?? I can't just access it and create the attachment?

Yes. You must have the file downloaded to attach it.

thanks. I'm trying that but no luck yet. I udpdated with the code any ideas?

ruby on rails - paperclip + ActionMailer - Adding an attachment? - Sta...

ruby-on-rails ruby-on-rails-3 paperclip actionmailer
Rectangle 27 0

Having similar issues: I've verified on the production server that I can get the file with open-uri like so:

irb(main):011:0>  myfile = open('https://secret-app.s3-us-west-2.amazonaws.com/bucketname/filename.pdf?gobbldeygook')
=> #<File:/tmp/open-uri20121214-2-wa908g>

irb(main):012:0>  file.readlines
=> ["%PDF-1.4\n", "1 0 obj\n", "<<\n", "/Title (\xFE\xFF\x00F\x00a\x00i\x00r\x00L\x00o\x00a\x00n)\n", "/Producer (wkhtmltopdf)\n", "/CreationDate (D:20121214051439)\n", ..... etc.
pdf = open(the_url)
attachments['my_attachment.pdf'] = pdf.read

mail(
  to: @customer.email,
  subject: "Your Documents",
  :charset => "UTF-8"
)

Yet when Sidekiq runs the worker, the attachment is called "noname" and completely blank except for saying:

This is a multi-part message in MIME format...

----

This works for regular file system files as attachments but not on the production server, where stuff is stored on S3. I'm out of ideas.

ruby on rails - paperclip + ActionMailer - Adding an attachment? - Sta...

ruby-on-rails ruby-on-rails-3 paperclip actionmailer
Rectangle 27 0

attachments.inline['avatar.jpg'] = @user.avatar.data

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 0

In my case, the problem was specifying content_type: "text/html" in the mail() call. Removing it fixed the issue for me on Rails 4.

require 'open-uri' # Allows us to read the S3 content into the mail attachment.

class InvoiceMailer < ActionMailer::Base
  def send_invoice(user, invoice)
    @user = user
    @invoice = invoice
    attachments["invoice.pdf"] = open(@invoice.s3_url).read
    mail(to: @user.email, subject: "This Month's Invoice")
  end
end

This rendered the PDF document attachment properly on both Outlook and Gmail, but should work everywhere.

ruby on rails - Trouble adding a file stored on S3 through Paperclip a...

ruby-on-rails paperclip actionmailer
Rectangle 27 0

attachments.inline['avatar.jpg'] = @user.avatar.data

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 0

You can use the Asset pipeline to attached the actual file path. Have not tested.

"#{Rails.root}/#{<YourAppName>::Application.assets.find_asset('avatar.jpg').pathname}"

The problem with this is I would need to know the actual filename. I don't know the filenames for people that upload their avatars. I am only saving the attachment as avatar.jpg. Not finding a file avatar.jpg

I had though paperclip renames the file? Or at least persists the path/location of it? May be wrong.

localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965
user.avatar.path

ruby on rails - Paperclip files not being attached to actionmailer. No...

ruby-on-rails ruby paperclip actionmailer
Rectangle 27 0

Here is my model Report. I'm using Paperclip.

class Report < ActiveRecord::Base
  has_attached_file :pdf_file
  ...
end
class ReportMailer < ActionMailer::Base
  def monthly_report_email(emails, report)
    attachments[report.pdf_file_file_name] = File.read(report.pdf_file.path)
    mail(:to => emails, :subject => 'monthly report')
  end
end

ruby on rails - paperclip + ActionMailer - Adding an attachment? - Sta...

ruby-on-rails ruby-on-rails-3 paperclip actionmailer
Rectangle 27 0

In my case, the problem was specifying content_type: "text/html" in the mail() call. Removing it fixed the issue for me on Rails 4.

require 'open-uri' # Allows us to read the S3 content into the mail attachment.

class InvoiceMailer < ActionMailer::Base
  def send_invoice(user, invoice)
    @user = user
    @invoice = invoice
    attachments["invoice.pdf"] = open(@invoice.s3_url).read
    mail(to: @user.email, subject: "This Month's Invoice")
  end
end

This rendered the PDF document attachment properly on both Outlook and Gmail, but should work everywhere.

ruby on rails - Trouble adding a file stored on S3 through Paperclip a...

ruby-on-rails paperclip actionmailer