Save 36% for Black Friday! Learn more

All threads / How to attach a remote S3 file to an outgoing email? [SOLVED]

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to attach a remote S3 file to an outgoing email? [SOLVED]

Daniel Weaver asked in Rails

I'm using a regular mailer to send text emails and now I want to attach a file stored on S3 to the email. I have the URL of the file and was naively hoping something like this would work:

def video_email
  @user = params[:user]
  @video_url = params[:video_url]
  attachments['video.mp4'] = File.read "https://me.s3.com/video.mp4"
  mail(to: @user.email, subject: 'Your video is attached!')
end

But I get an error:

Errno::ENOENT: No such file or directory @ rb_sysopen - "https://me.s3.com/video.mp4"

What's the best way for me to attach a remote file to an outgoing email?

Is this an ActiveStorage::Blob?

No, this is a video file that I'm generating in a function outside of Rails.

Eventually I'll be pulling the function into Rails so the S3 file will be easier to handle but for now it's a totally separate file.

Got this working by parsing and opening the URL with URI.parse:

def video_email
  @user = params[:user]
  @video_url = params[:video_url]
  attachments['video.mp4'] = File.read(URI.parse(@video_url).open)
  mail(to: @user.email, subject: 'Your video is attached!')
end

Not sure why it wasn't working with a regular string. The OpenURI docs seem to suggest a string will work.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.