3

私のウェブサイトのすべてのユーザーに電子メールを送信する方法を理解するのを手伝ってくれませんか? ユーザーがサインアップしたり、連絡フォームを送信したり、何らかのアクションを実行したりした場合に、メールを作成する方法を知っています. しかし、何らかのアクションを実行せずにメールを送信する方法がわかりません。

私のconfig/environments/production.rbには次のものがあります:

config.action_mailer.smtp_settings = {
  :address              => "smtp.mandrillapp.com",
  :port                 => 587,
  :domain               => "majorfinder.com",
  :user_name            => ENV["MANDRILL_USERNAME"],
  :password             => ENV["MANDRILL_PASSWORD"],
  :authentication       => :plain,
  :enable_starttls_auto => true
}

config.action_mailer.default_url_options = {
  :host => "majorfinder.com"
}

私の app/mailers/questions_mailer.rb には次のものがあります:

class QuestionsMailer < ActionMailer::Base
  default from: "lauralee@majorfinder.com"
  def interest_question(user)
    @user = user
mail(to: @user.email, subject: "My Subject")
  end

メールの txt および html バージョンを作成し、app/views/questions_mailer/email.html.erb / email.txt.erb に配置しました。

私が見逃しているのは、電子メールを送信するためにコントローラーに何を入れるかです。それは次のようなものですか:

QuestionsMailer.interest_question(@user).deliver

これを送信した時点で、約 300 人のユーザーがいます。これを複数のバッチに分割する必要があります (ここで見つけたもの: Ruby on Rails を使用して間隔に基づいてメールを送信する)。

これが重要かどうかはわかりませんが、heroku でホストしており、データベースは postgresql です。

あなたが私に与えることができるどんな助けにも感謝します!

更新: @jefflunt の提案に従い、rake タスクを作成しました。これが私のものです:

desc "Email to ask users what they're most interested in"
task :interests => :environment do
  User.all.each do |u|
    QuestionsMailer.interest_question(u).deliver
  end
end

そして、rake interestsローカルで実行して、すべてが正常に機能することを確認しました。すべての変更を heroku にプッシュするheroku run rake interestsと、すべてのユーザーにメールが送信されました。

4

1 に答える 1