[英]How do not send email if it empty with rails ActionMailer
How to not send emails if them empty. 如果它们为空,如何不发送电子邮件。
Here is my mailer: 这是我的邮件:
class UsersMailer < ActionMailer::Base
default from: "\"Example\"<reports@example.com>"
def report(emails)
@users = User.where(created_at: (1.days.ago)..(Time.now))
mail to: emails
end
end
End here is a rake task for: 结束于此是一项耙任务:
task :users_browser_mail => [:environment] do
emails = %w{me@example.com}
UsersBrowserMailer.report(emails).deliver
end
So if @users
is nil
the emails is still sending how i can avoid this? 因此,如果
@users
为nil
,电子邮件仍在发送,我如何避免这种情况?
Should i do something in my rake task like?: 我应该在耙任务中做些什么吗?
if @users
UsersBrowserMailer.report(emails).deliver
end
Move the query into the rake task. 将查询移至rake任务。
@users
won't exist in the rake task, as it's an instance variable within the mailer. @users
在rake任务中将不存在,因为它是邮件程序中的一个实例变量。
task :users_browser_mail => [:environment] do
emails = %w{me@example.com}
users = User.where(created_at: (1.days.ago)..(Time.now))
UsersBrowserMailer.report(emails, users).deliver unless users.blank?
end
class UsersMailer < ActionMailer::Base
default from: "\"Example\"<reports@example.com>"
def report(emails, users)
@users = users
mail to: emails
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.