GCE + Rails + SendGrid でメール送信


  • GCE = Google Compute Engine

SendGridで

Setup guide > SMTP Relay と進んで、API keyを生成します(このキーがパスワードになります)。

Railsで

enviroment.rb または 各環境の設定ファイルにsmtpの設定を追記します。default_url_optionsの設定も忘れずに。ここでコツなのはportに「2525(465や587ではなく)」を指定するところです。ドキュメントにあるとおり、GCEでは主要なメール送信用のポートが閉じられています。

config/enviroments/staging.rb
  config.action_mailer.default_url_options = {
    :host => 'yourdomain.com', :protocol => 'https'}
  ActionMailer::Base.smtp_settings = {
    :user_name => 'apikey',
    :password => 'yourpassword',
    :domain => 'yourdomain.com',
    :address => 'smtp.sendgrid.net',
    :port => 2525,
    :authentication => :plain,
    :enable_starttls_auto => true
  }

参考URL

https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html
https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid