我正在尝试向多个用户发送电子邮件。我有一个发送@users 的模型,它包含我要邮寄的所有用户......现在在 User_mailer 中我无法弄清楚如何告诉 mail_out 进程发送给每个用户(设置每个收件人 user.email)。总而言之,我想设置一个 cron 作业来每天早上运行 User.mail_out 进程,让它通过电子邮件将 @users 变量中的每个用户发送给 User_mailer 模型。有人可以建议一种方法吗?使用我在下面写的内容时,我目前收到以下错误:
/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)
用户.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