Sendcorex
チュートリアル

Ruby on RailsでSMTP経由でメールを送る

Action Mailerをinitializerで一度設定すれば、アプリのどこからでも送信できます。

Sendcorexチーム·更新日 2026年9月21日·7分で読めます

Action Mailerを設定する

config/environments/production.rb(またはinitializer)で:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.sendcorex.com',
  port: 587,
  user_name: ENV['SENDCOREX_API_KEY'],
  password: ENV['SENDCOREX_API_KEY'],
  authentication: 'plain',
  enable_starttls_auto: true,
}
config.action_mailer.default_url_options = { host: 'yourdomain.com' }

メーラーを生成して送信する

rails generate mailer Welcome
class WelcomeMailer < ApplicationMailer
  default from: 'Your App <hello@yourdomain.com>'

  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome!')
  end
end

# from anywhere in your app
WelcomeMailer.welcome_email(user).deliver_later

deliver_nowではなく、キューアダプター(Sidekiq、Solid Queue)と一緒にdeliver_laterを使いましょう。遅いSMTP接続がウェブリクエストをブロックしなくなります。

よくあるエラー

症状対処
Net::SMTPAuthenticationError環境変数が読み込まれ、user_nameとpasswordの両方に使われているか確認してください。
メールがキューから出ないキューアダプター(例:Sidekiq)がその環境で実際に動いているか確認してください。

数分で最初のメールを送信

無料アカウントを作成し、ドメインを認証して、上のコードを貼り付けましょう。

クレジットカード不要