Could someone please help me understand how to send an email to all the users of my website? I know how to create an email if the user signs up or submits a contact form or performs some action. But, I can't figure out how to send an email without some action being performed.
In my config/environments/production.rb I have:
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"
}
In my app/mailers/questions_mailer.rb I have:
class QuestionsMailer < ActionMailer::Base
default from: "lauralee@majorfinder.com"
def interest_question(user)
@user = user
mail(to: @user.email, subject: "My Subject")
end
I have the txt and html version of my email created and placed in app/views/questions_mailer/email.html.erb / email.txt.erb.
What I'm missing is what do I put in my controller to make the email send. Is it something like:
QuestionsMailer.interest_question(@user).deliver
At the time of sending this I have around 300 users. Should I break this up into more than one batch (something like I found here: Sending emails based on intervals using Ruby on Rails)?
I don't know if this is important but I'm hosting on heroku and my database is postgresql.
Thank you so much for any help you can give me!
UPDATE: I followed @jefflunt's suggestion and create a rake task. Here's what mine looked like:
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
And then I ran rake interests
locally to make sure everything worked fine. Once I pushed all the changes up to heroku I ran heroku run rake interests
and an email was sent to all the users.