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?

Reply

Is this an ActiveStorage::Blob?

Reply

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.

Reply

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.

Reply
Join the discussion

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.