我正在尝试向多个用户发送电子邮件。我有一个发送@users的模型,其中包含我将要发送邮件的所有用户...现在在User_mailer中我无法确定如何告诉mail_out进程发送给每个用户(set每个收件人都是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)
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
答案 0 :(得分:1)
#mail_out方法中的'body'参数是一个值的哈希值,它将被插入到电子邮件模板中,而不是收件人的哈希值。它应该是这样的:
def mail_out(users)
recipients users.collect(&:email)
from "somewhere.net"
subject "Check it out"
body {:var => 'value to interpolate into email'}
end
这里有一个很好的备忘单: http://dizzy.co.uk:80/ruby_on_rails/cheatsheets/action-mailer
答案 1 :(得分:1)
recipients ['mail1@example.info', 'mail2@example.com']