I already have figured out how to email users on a single basis (someone enters their email address and a mailer model sends that person an email using friend.email (friend being the model and email being the attribute).
Now I am trying to send an email to several users. I started writing a process in my User model like below:
class User < ActiveRecord::Base
acts_as_authentic
def mail_out
weekday = Date.today.strftime(%A).downcase
@users = find(:conditions => "#{weekday}sub = t")
@users.each { |u| UserMailer.deliver_mail_out(u)}
end
end
I know there is probably a much better way to do this but each User in my database can have t or f for mondaysub, tuesdaysub, etc. So I want to throw all of the users with the attribute mondaysub set to "t" into the @monday variable (the same for each day of the week). Now I want to check the day of the week and if it's a monday, send an email to each of the users in the @monday variable. I have used whenever as follows:
every 1.day, :at => '5:30 am' do
runner "User.mail_out"
end
This will invoke the mail_out process in my model once a day (I think).
Now I have my mailer setup as follows:
class UserMailer < ActionMailer::Base
def mail_out(users)
开发者_StackOverflow中文版 @recipients = { }
users.each do |user|
@recipients[user.email] = { :name => user.name }
end
from "no-reply@dailytrailer.net"
subject "Check out the trailer of the day!"
body :user => user
end
end
My question is, how do I connect these together, I hope I am on the right track... I want it so every morning at 5:00, if its a monday, then every user with mondaysub set to "t" is mailed a specific usermailer template. I am starting to really confuse myself, any help would be GREATLY appreciative. As you can tell I am new at this but I have been reading through tons of books/tutorials.
Thank you!
For this sort of mass emailing you really ought to consider outsourcing the problem. Take a look at PostageApp which makes it trivial to email multiple recipients. It's DHH-approved!
Check this railscast episode about sending e-mails. You just need to create a template file for your e-mail on views/usermailer/mail_out.erb.html.
精彩评论