Skip to main content

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

Rails • Asked by Daniel Weaver

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.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.