How to attach a remote S3 file to an outgoing email? [SOLVED]
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?
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.