Rails 4.2.3
設定
設定は標準だと config/enviromnents/{env}.rb
の中に記述する。新規作成の時に自動で幾つか書いてある。
環境ごとにしかわけないなら十分だけど、例えば「ステージングがAlphaサーバーとBetaサーバーに分かれてる」とかそういうのがある。だから ActionMailer の設定も yml に出したい。
unless MyApp::Application.config.action_mailer.nil?
options = YAML.load_file(Rails.root.join('config', 'mailer.yml'))[Rails.env]
MyApp::Application.config.action_mailer.merge! options.deep_symbolize_keys! unless options.nil?
end
これで config/mailer.yml
をロードしてくれる。マージなので、config/environments/{env}.rb
に書いてあるやつが消えるわけじゃない感じ。
config.action_mailer
は(ていうか config
以下全部)シンボルのキーしか読まない感じでハッシュを取り扱ってるけど、yml からロードしたのはキーが文字列なので、deep_symbolize_keys!
してる。再帰的に symbolize_keys
する奴だと思う。
Return a new hash with all keys converted to symbols, as long as they respond to to_sym. This includes the keys from the root hash and from all nested hashes.
だそうです。
defaults: &defaults
staging:
delivery_method: :smtp
default_url_options:
host: "staging.example.com"
smtp_settings:
address: "localhost"
production:
delivery_method: :smtp
default_url_options:
host: "example.com"
smtp_settings:
address: "localhost"
development:
smtp_settings:
host: "127.0.0.1"
port: "1025"
default_url_options:
host: "0.0.0.0"
port: "3000"
まあなんかこんな感じ。
に ActionMailer の設定について書いてある。普通のメール周りの知識でだいたいなんとかなると思う。
MailCatcher
MailCatcher ってのが便利。ローカルで動く仮想 SMTP サーバーで、受け取ったメール配信リクエストを Virtual 受信ボックスで表示してくれる。Plane と HTML の表示切り替えとかもできるので、両方のフォーマットに対応したメールをチェックする時とかも便利。
gem install mailcatcher
mailcatcher
以上。古い人なので rvm つかってるので、gemset で mailcatcher のやつ作ってインストールしてる。
ここに MailCatcher について書いてる。こんな雑記読むより公式のテキスト読んだほうが良い。
http://localhost:1080
が Virtual 受信トレイ。
Postfix
普通にインストールしたら動いたので特に書くこと無い。
yum install -y postfix
systemctl enable postfix
systemctl start postfix
CentOS 7 だと不要な感じがするけど、postfix の設定はちゃんとしとかないと困る。ガチSAPMられる。
終わり
他になんかメモったほうがいいことあるかな……。Gmail周りは(正直そう複雑なことはないけど)細かく調べて書いておくと便利そうな気はするけど、すでに調べてる人たくさんいる案件だしわざわざ書き直すほどのことかと言われるとかなり疑問。
ていうか送信だけなら普通自前のSMTPサーバー使うだろうし、Gmail 使うのって開発時だけな気がする。で、開発時は MailCatcher の方が便利なので Gmail 不要だよね。