我正在尝试创建一种方法,当被调用时向所有用户发送电子邮件。
I am trying to create a method that, when called send a email to all users.
我的最终目标是通过调度程序调用此方法(我已经使调度程序正常工作),如果满足一些先决条件,该方法将考虑所有用户并将电子邮件发送给某些用户。
My ultimate goal is to call this method via a scheduler (i already got the scheduler working) And the method will go thought all users and send emails to some of then if some pre-requisites are met.
现在我只想学习我如何制作最简单的东西,即向表中的每个用户发送自定义电子邮件。
Right now i just want to learn how i make a simplest stuff that is to send a custom email to every user in the table.
我的第一个问题是:
def send_digest @users = User.all @users.each do |user| @time = Time.now mail(to: user.email, subject: user.name) end end此方法(位于app / mailer / user_mailer.rb内)仅向表中ID最大的人发送一封电子邮件。为什么?
This method (is inside app/mailer/user_mailer.rb) only is sending one e-mail to the guy with biggest ID in the table. Why that?
此外,我需要怎么做才能访问电子邮件中的变量 user.name?
Also, what i need to do to access the variable "user.name" inside the email?
编辑:有比使用@user = user更好的方法来访问邮件正文中的用户变量?
There a better way for accessing user variable inside the mail body than doing @user = user?
def send_digest(user) @time = Time.now @user = user mail(to: user.email, subject: 'mail message') end推荐答案
对于每次调用邮寄方式的邮件,都会发送一封电子邮件
For each call to the mailer method one email is sent
在计划的工作人员中
def calling_method @users.each do |user| send_digest(user.email, user.name) end end在用户邮件中
def send_digest(user_email, user_name) mail(to: user_email, subject: user_name) end更多推荐
向所有用户发送电子邮件
发布评论