I am trying to send an email to mu开发者_运维技巧ltiple 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
The 'body' parameter in your #mail_out method is meant to be a hash of values that will be interpolated into the email template, not a hash of recipients. it should look like this:
def mail_out(users)
recipients users.collect(&:email)
from "somewhere.net"
subject "Check it out"
body {:var => 'value to interpolate into email'}
end
Theres a nice cheatsheet here: http://dizzy.co.uk:80/ruby_on_rails/cheatsheets/action-mailer
recipients ['mail1@example.info', 'mail2@example.com']
精彩评论