RailsでSMTPへ送信したメールを実際には送信せずにローカルに保存する方法
MailTrapを使うと超簡単にSMTPサーバをローカルに構築できます。
しかも実際にはメールを送信せずにローカルに保存してくれるのでTailコマンドで流しっぱなしにしておくといちいち受信しなくて便利です。
インストールはgemで一発
$ sudo gem install mailtrap
SMTPサーバの構築は以下のコマンド一発
$ mailtrap start
Railsへの設定は{RAILSROOT}/config/environment.rb に以下を記述します。
# Be sure to restart your server when you modify this file # Uncomment below to force Rails into production mode when # you don't control web/app server and can't set it the proper way # ENV['RAILS_ENV'] ||= 'production' # Specifies gem version of Rails to use when vendor/rails is not present RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION # Bootstrap the Rails environment, frameworks, and default configuration require File.join(File.dirname(__FILE__), 'boot') Rails::Initializer.run do |config| #ここから config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => 'localhost', :port => 2525, :domain => 'mydomain.net', } #ここまで追記 (中略) end
これで/var/tmp/mailtrap.logにSMTPで受けたメールが随時書き込まれるのでtail -f コマンドで流しっぱにしておくことをおすすめします。