I am trying to send an email to multiple users. I have a model that sends @users, which has all of the users I am going to be mailing... now when in the User_mailer I am having trouble figuring out how to tell the mail_out process to send to each of the users (set each recipient the user.email). To summarize, I want to set a cron job to run the User.mail_out process each morning, have it email each user in the @users variable being passed to the User_mailer model. Can someone please suggest a way of doing this? I am currently getting the following error when using what I have written below:
/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: /usr/lib/ruby/1.8/net/smtp.rb:680:in `check_response': 501 5.1.3 Bad recipient address syntax (Net::SMTPSyntaxError)
User.rb
class User < ActiveRecord::Base
acts_as_authentic
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_presence_of :birthday => "cannot be left blank"
def self.mail_out
weekday = Date.today.strftime('%A').downcase
@users = find(:all, :conditions => {"#{weekday}sub".to_sym => 't'})
UserMailer.deliver_mail_out(@users)
end
end
User_Mailer.rb
class UserMailer < ActionMailer::Base
def mail_out(users)
@recipients = { }
users.each do |user|
@recipients[user.email]
end
from "somewhere.net"
subject "Check it out"
body :user => @recipients
end
def subscribe(user)
recipients user.email
from "somewhere.net"
subject "Welcome!"
body :user => user
end
end