コンテンツにスキップ

Rails

config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "acme.com", protocol: "https" }
config.action_mailer.smtp_settings = {
address: "smtp.spacepost.dev",
port: 25976,
user_name: "spacepost",
password: ENV.fetch("SPACEPOST_API_KEY"),
authentication: :plain,
enable_starttls_auto: true
}

enable_starttls_auto: true は、ログインの前に STARTTLS で接続をアップグレードします。ssl: true や tls: true は設定しないでください。暗黙的 TLS(ポート 465)はまだ提供していません。

app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "Acme <hello@acme.com>"
layout "mailer"
end

メールごとにタグやスケジュールを追加します。

class UserMailer < ApplicationMailer
def welcome(user)
headers["X-SpacePost-Tags"] = "category=welcome"
mail(to: user.email, subject: "Welcome to Acme")
end
end

遅い SMTP 呼び出しが Web リクエストをブロックしないよう、Active Job(deliver_later)を使ってください。

ターミナルウィンドウ
bin/rails runner 'ActionMailer::Base.mail(from: "hello@acme.com", to: "delivered@test.spacepost.dev", subject: "Test", body: "It works").deliver_now'