简体   繁体   中英

Rails 4 Mailer, Amazon S3, and inline images

I have a Rails 4 application hosted on Heroku that serves assets from an S3 bucket. I am trying to customize my mailer (in this case, a customization of the Devise mailer) so that I can embed inline images into my emails.

Per the Rails documentation, the mailer should include code such as the following:

def confirmation_instructions(record, token, opts={})
  # Prepare image for embedding
  attachments.inline['logo'] = File.read("#{Rails.root}/app/assets/images/logo.jpg")

  # Allow Devise to do its thing
  super 
end

And the view should contain the following:

<%= image_tag attachments['logo'].url, :style => "my styling here" %>

On Heroku, this fails with the following log:

ActionView::Template::Error (undefined method `url' for nil:NilClass):

"my styling here" %>

In other words, it looks like attachments.inline['logo'] is returning nil, and the view is then calling .url on nil.

I've tried numerous fixes and at this point am pretty exasperated. I know it must be something simple I'm overlooking and I hope somebody out there can point out where I'm going wrong.

Thanks in advance.

Try this:

File.read(Rails.root.join("app/assets/images/logo.jpg")

That's how I got it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM