0

I have 3 main files of my project which link together - index.html, server.js and mail.js

A portion of mail.js is below

const sendMail = (message, cb) => { 
    var dict = { 
        'hourly': '* * 0-23 * * ', 
        'daily': ' * * 1-31 * ', 
        'weekly': ' * * * * ', 
        'monthly': ' * * * 1-12 *'
};
const mailOptions = {
    from: 'blah',
    to: 'blah',
    subject: 'Emails',
    text: message
};


transporter.sendMail(mailOptions, function(err, data){
    if (err){
        cb(err, null);
    } else {
        cb(null, data);
    }
});

Portion of server.js below

app.post('/email', (req, res) => { const {message} = req.body; console.log('Data: ', req.body); sendMail(message, function(err, data) { if (err) { res.status(500).json({say: 'Internal Error' }); } else { res.json({say: 'Email sent'}); } }); res.json({message: 'Message received'}); });

portion of index.html below

<body>


<form>

    <input type = "text" id = "message" placeholder = "Message"> <br>
    <select id = "frequency" placeholder = "frequency">
        <option value="hourly">Hourly</option>
        <option value="daily">Daily</option>
        <option value="weekly">Weekly</option>
        <option value="monthly">Monthly</option>
      </select> <br>
    <input type = "submit" value ="Submit"> <br>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
    $('form').on('submit', (e) => {
        e.preventDefault();

        const frequency = $('#frequency').val().trim();
        const message = $('#message').val().trim();

        const data = {
            frequency,
            message

        };
        $.post('/email', data, function(){
            console.log('Data is received by server')
        });
    })
</script>

What the project is supposed to do is to select a frequency option from the form in index js, write a message and from that it send an email to a certain email address (which will be in mail.js) with the frequency of the option until the program is terminated. I'm having issues with the frequency bit using nodecron in mail.js. How do I include the frequency with this? I have currently held the values in a dictionary in mail.js but I want to know how to use this to my advantage. Please help.

I had tried to use them in a dictionary with nodecron methods but it only sent 1 email.

0 Answers0