gcarey
gcarey

Reputation: 231

Rails 4 Action Mailer - Attach non-local inline image (with URL)

I'm aware that in order to attach an inline image using Action Mailer you include the following line in your controller action:

attachments.inline['image.jpg'] = File.read('path/to/image.jpg')

I've gotten that to work fine for images that are stored locally, but I'm now trying to attach an image that is a Paperclip attachment and is stored using Amazon S3. How do I attach inline images that are not stored on the same domain as the site? I've tried:

attachments.inline['image.jpg'] = @resource.image.url(:full)

and I know that "@resource.image.url(:full)" does successfully point to the image that I'm trying to attach, but it appears in the email as a broken image. And if I try:

attachments.inline['image.jpg'] = File.read(@resource.image.url(:full))

then it just fails entirely. Anyone know how to do this?

Upvotes: 2

Views: 736

Answers (1)

Dana Najjar
Dana Najjar

Reputation: 41

You're calling File. Read on a URL when it's expecting a local file path. Have you tried:

attachments.inline['image.jpg'] = open("https://s3-us-(region).amazonaws.com/(bucketname)/#{@image.current_path}").read

Example:

attachments.inline['image.jpg'] = open("https://s3-us-west-2.amazonaws.com/mycucket/#{@image.current_path}").read

It works for carrier wave as the image uploader, it may work with paperclip as well.

Upvotes: 4

Related Questions