【问题标题】:Loop Through all users to send email Node JS循环通过所有用户发送电子邮件 Node JS
【发布时间】:2018-03-07 21:13:33
【问题描述】:

我试图找到答案,但找不到。我是 Node Js 的新手,并且有一个包含用户电子邮件的用户模式。我想要一个包含“主题”和“消息”字段的表单,以及一个提交按钮,然后该按钮将通过电子邮件发送表单的内容。我目前正在使用 Mailgun,标准电子邮件如下所示:

 var data = {
            to: user.email,
            from: '"Lighthouse Studios" <noreply@reception.lighthousespace.co.uk>',
            subject: 'Your password has been changed',
            text: 'Hello,\n\n' +
              'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n'
          };
          mailgun.messages().send(data, function (error, body) {
      console.log(body);
    });

我的用户架构如下所示:

var UserSchema = new mongoose.Schema({
    username: {type: String, unique: true, required: true},
    name: {type: String, required: true},
    email: {type: String, unique: true, required: true},
    password: String,
    studio: {type: String, required: true},
    resetPasswordToken: String,
    resetPasswordExpires: Date,

    comments: [
      {
            quantity: String,
            received: { type: Date, default: Date.now },
            collected: { type: String, default: "At Reception" }
      }
   ],

    isAdmin: {type: Boolean, default: false}
});

这是表单代码:

<form action="/" method="POST">
            <div class="form-group">
                <input class="form-control" type="text" name="subject" placeholder="Subject">
            </div>
            <div class="form-group">
               <textarea class="form-control" rows="5" id="comment" name="message" placeholder="Message"></textarea>
            </div>
            <div class="form-group">
                <input class="btn btn-lg btn-primary btn-block" type="submit" value="Send">
            </div>

        </form>

总而言之,我想在单击“提交”按钮时使用表单向所有用户发送电子邮件。感谢您抽出宝贵时间阅读本文。

【问题讨论】:

    标签: javascript node.js mongodb express mailgun


    【解决方案1】:
     UserModel.find({})
             .select('email')
             .lean()
             .exec((err,users)=>{
                if(users){
                  let userEmails = "";
                  let recp = users.reduce((a,u)=>{ a[u.email]={}; a[u.email].email = u.email; userEmails+=u.email; return a; },{})
                  let data = {
                            to: userEmails,
                            from: '"Lighthouse Studios" <noreply@reception.lighthousespace.co.uk>',
                            subject: 'Your password has been changed',
                             text: 'Hello,\n\n' +'This is a confirmation that the password for your account ' + %recipient.email% + ' has just been changed.\n',
                            'recipient-variables': JSON.stringify(recp)
                    };
    
                    mailgun.messages().send(data, function (error, body) {
                    console.log(body);
                    });
               }   
            })
    

    希望这个能成功

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 2022-01-13
      • 2014-08-17
      • 2018-10-27
      • 2015-12-07
      • 2018-12-19
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多