向所有用户发送电子邮件

编程入门 行业动态 更新时间:2024-10-25 06:31:57
本文介绍了向所有用户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一种方法,当被调用时向所有用户发送电子邮件。

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

更多推荐

向所有用户发送电子邮件

本文发布于:2023-10-24 17:22:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!