如何在 Nodejs 中创建电子邮件提醒?

发布于 2024-11-30 19:52:43 字数 203 浏览 1 评论 0 原文

我有一个包含 4 个字段的简单表单,即:

  1. 姓名
  2. 出生日期
  3. 电子邮件 地址
  4. 消息

我将此数据保存到 mongodb。生日那天,我需要发送电子邮件提醒。我使用node_mailer来发送邮件。但是如何设置在特定日期发送邮件的提醒呢?我正在运行nodejs服务器。

谢谢

I have a simple form containing 4 fields viz:

  1. Name
  2. DateOfBirth
  3. email Address
  4. Message

I save this data to mongodb. On birthday, i need to send a email reminder. I use node_mailer for sending mails. But how to set up the reminders to send mails on specific date? I am running nodejs server.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身软味 2024-12-07 19:52:43

不要使用节点来跟踪这样的日期。不要重新发明轮子。

您的平台,无论是 Mac、Linux 还是 Windows,都有一个调度程序。传统的称为“cron”。使用它来启动一个简单的 node_mailer 包装器,该包装器将扫描数据库中的“今天的生日”,然后发送电子邮件。

Don't use node to track dates like that. Don't re-invent wheels.

Your platform, being it Mac, Linux, or Windows, has a scheduler on it. The traditional one is called 'cron'. Use that to start a simple wrapper to node_mailer that will scan the database for "today's birthdays" that will send the emails instead.

垂暮老矣 2024-12-07 19:52:43

您可以使用 node-cron 来实现。

You can use node-cron for that.

咋地 2024-12-07 19:52:43

我发现 agendajs 高度可靠,并且更好地使用 GUI 补充agendash

入门资源:NodeJS:使用 Agenda.js 调度任务

I found agendajs highly reliable and better with the GUI complement agendash

Resource for Getting Started: NodeJS: scheduling tasks with Agenda.js

终止放荡 2024-12-07 19:52:43

这只是需要的基本循环,

您每天在特定时间循环访问用户,然后检查日期和月份是否匹配并发送邮件

**这里是下面的示例代码**干杯

const cron = require('node-cron');
const mailer = require('nodemailer');

//T0 Get the Current Year, Month And Day
var dateYear = new Date().getFullYear();
var dateMonth = new Date().getMonth(); // start counting from 0
var dateDay = new Date().getDate();// start counting from 1

/* The Schema Which The Database Follow 
    {
        'id' : number,
        'name' : string,
        'dob' : string (day - month),
        'email' : string 
    },
 * You can Use Any type of schema (This is the method I preferred)
*/

/// database goes here 
var users = [
    {
        'id' : 000,
        'name' : 'user1',
        'dob' : '14-6-1994',
        'email' : '[email protected]'
    },
    {
        'id' : 001,
        'name' : 'user2',
        'dob' : '15-6-2003',
        'email' : '[email protected]'
    },
    {
        'id' : 002,
        'name' : 'user3',
        'dob' : '17-4-2004',
        'email' : '[email protected]'
    },
    {
        'id' : 003,
        'name' : 'user4',
        'dob' : '6-0-1999',
        'email' : '[email protected]'
    }
]

//// credentials for your Mail
var transporter = mailer.createTransport({
    host: 'YOUR STMP SERVER',
    port: 465,
    secure: true,
    auth: {
        user: 'YOUR EMAIL',
        pass: 'YOUR PASSWORD'
    }
});
//Cron Job to run around 7am Server Time 
cron.schedule('* * 07 * * *', () => {
    ///The Main Function 
    const sendWishes =  
    // looping through the users
    users.forEach(element => {
        // Spliting the Date of Birth (DOB) 
        // to get the Month And Day
        let d = element.dob.split('-')
        let dM = +d[1]  // For the month
        let dD = +d[0] // for the day 
        let age = dateYear - +d[2]
        console.log( typeof dM) //return number
        // Sending the Mail
        if(dateDay == dD && dateMonth == dM ){
            const mailOptions = {
                from: 'YOUR EMAIL',
                to: element.email,
                subject: `Happy Birthday `,
                html: `Wishing You a <b>Happy birthday ${element.name}</b> On Your ${age}, Enjoy your day \n <small>this is auto generated</small>`                       
            };
            return transporter.sendMail(mailOptions, (error, data) => {
                if (error) {
                    console.log(error)
                    return
                }
            });
        } 
    });
});

It's Just Basic Looping That Is Required

You Loop through the user everyday at a particular time then check if the day and month matches and shoot your mail

**Here A Sample code Below ** cheers

const cron = require('node-cron');
const mailer = require('nodemailer');

//T0 Get the Current Year, Month And Day
var dateYear = new Date().getFullYear();
var dateMonth = new Date().getMonth(); // start counting from 0
var dateDay = new Date().getDate();// start counting from 1

/* The Schema Which The Database Follow 
    {
        'id' : number,
        'name' : string,
        'dob' : string (day - month),
        'email' : string 
    },
 * You can Use Any type of schema (This is the method I preferred)
*/

/// database goes here 
var users = [
    {
        'id' : 000,
        'name' : 'user1',
        'dob' : '14-6-1994',
        'email' : '[email protected]'
    },
    {
        'id' : 001,
        'name' : 'user2',
        'dob' : '15-6-2003',
        'email' : '[email protected]'
    },
    {
        'id' : 002,
        'name' : 'user3',
        'dob' : '17-4-2004',
        'email' : '[email protected]'
    },
    {
        'id' : 003,
        'name' : 'user4',
        'dob' : '6-0-1999',
        'email' : '[email protected]'
    }
]

//// credentials for your Mail
var transporter = mailer.createTransport({
    host: 'YOUR STMP SERVER',
    port: 465,
    secure: true,
    auth: {
        user: 'YOUR EMAIL',
        pass: 'YOUR PASSWORD'
    }
});
//Cron Job to run around 7am Server Time 
cron.schedule('* * 07 * * *', () => {
    ///The Main Function 
    const sendWishes =  
    // looping through the users
    users.forEach(element => {
        // Spliting the Date of Birth (DOB) 
        // to get the Month And Day
        let d = element.dob.split('-')
        let dM = +d[1]  // For the month
        let dD = +d[0] // for the day 
        let age = dateYear - +d[2]
        console.log( typeof dM) //return number
        // Sending the Mail
        if(dateDay == dD && dateMonth == dM ){
            const mailOptions = {
                from: 'YOUR EMAIL',
                to: element.email,
                subject: `Happy Birthday `,
                html: `Wishing You a <b>Happy birthday ${element.name}</b> On Your ${age}, Enjoy your day \n <small>this is auto generated</small>`                       
            };
            return transporter.sendMail(mailOptions, (error, data) => {
                if (error) {
                    console.log(error)
                    return
                }
            });
        } 
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文