[Rails] Action Mailerのdelivery_methodを動的に切り替える方法

SendGridのAPI経由のEメール送信がテンプレートを使えて非常に良いので切り替えたいが、現在smtpで送っているメールをすべて一回で切り替えるのは不可能なので、同時運用したい。 下記のように、delivery_methoddelivery_method_optionsをmailメソッドに入れるとwrapしてくれるので、smtpを使わなくなる。

  def send_test_mail
    mail(
      to: 'myemail@change.this.com', 
      subject: 'email subject', 
      body: 'not used',
      template_id: "xxxx",
      delivery_method: :sendgrid_actionmailer, 
      delivery_method_options: {
        api_key: ENV['SENDGRID_API_KEY'] 
      }
    )
  end

Related Posts