1

我正在尝试向多个用户发送电子邮件。我有一个发送@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
4

2 回答 2

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

于 2010-02-08T08:02:22.907 回答
1
recipients    ['mail1@example.info', 'mail2@example.com']
于 2012-05-21T11:54:55.167 回答