Skip to content

Instantly share code, notes, and snippets.

@jeremysmithco
Created May 20, 2021 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremysmithco/52db4d5d1e0b15e4969c4bb8a0087480 to your computer and use it in GitHub Desktop.
Save jeremysmithco/52db4d5d1e0b15e4969c4bb8a0087480 to your computer and use it in GitHub Desktop.
Rails bin script to listen for emails delivered to filesystem and open with Mail (on a Mac)
#!/usr/bin/env ruby
require 'listen'
APP_ROOT = File.expand_path('..', __dir__)
listener = Listen.to("#{APP_ROOT}/tmp/mails") do |_modified, added, _removed|
added.each do |added_file|
if added_file.end_with?(".eml")
system("open -a Mail #{added_file}")
else
File.rename(added_file, "#{added_file}-#{Time.now.to_i}.eml")
end
end
end
listener.start
sleep
@jeremysmithco
Copy link
Author

jeremysmithco commented May 20, 2021

ActionMailer relies on the mail gem to deliver emails to the filesystem, and mail's FileDelivery#deliver! method names those files the destination email address. So this script first renames the file appending a timestamp and .eml extension, then opens the renamed file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment