One, the introduction

Recently the family added a child, at home with a baby to accompany some days, deeply understand the wife to take children’s hard.

When I couldn’t stay at home with her at work, I thought about writing a program to send her some love words at a fixed time every day, so that she could feel her husband’s love when she was taking care of her baby and help her relieve her restless mood when taking care of her baby.

Considering that QQ and wechat have many restrictions (accidentally blocked), first to a mailbox version of it!

Stack Overflow co-founder Jeff Atwood has said that any application that can be written in JavaScript will eventually be implemented in JavaScript.

Atwood’s Law was proposed by Jeff Atwood in 2007: “Any application that can be written in JavaScript, will eventually be written in JavaScript.

Big guy’s law all came out, can only say JS cow X!

Our technology model is NodeJS, first you have to install nodeJS, and then we mainly use a module Nodemailer.

Download links and screenshots

2. Nodemailer

Nodemailer is a mail sending NPM package that allows you to send mail to anyone quickly and easily.

Nodemailer installation

Open the terminal (Windows use Win + R, then enter CMD, press Enter)

Then enter the following command

NPM init -y # NPM install Nodemailer # Install the mail sending moduleCopy the code

We create a mailBot folder, enter the folder in terminal, initialize NPM, and then install NodeMailer

Use of nodemailer – send mail

Next, create a new index.js file in your project and write the following code

const nodemailer = require("nodemailer");
// Send mail function
async function sendMail(text) {
  var user = "[email protected]";// Your email address
  var pass = "xxx"; // How to obtain the authorization code
  var to = "[email protected]";// Your email address
  let transporter = nodemailer.createTransport({
    host: "smtp.qq.com".port: 587.secure: false.auth: {
      user: user, // User account
      pass: pass, // Authorization code, obtained through QQ}});let info = await transporter.sendMail({
    from: 'Dear husband <${user}> `.// sender address
    to: 'Dear wife <${to}> `.// list of receivers
    subject: "Dear Wife.".// Subject line
    text: text, // plain text body
  });
  console.log("Sent successfully");
}

// Test it
send('Hello wife')

Copy the code

To execute the code of the js file, type node index.js in the terminal.

Through the above code, we can send any sentence to the person you want to send!

Note: for your QQ email pass (authorization code), you need to enter [Settings] – [account] of your QQ email, and then open SMTP in the place shown in picture 1 below, and view your authorization code in the place shown in Picture 2 below

Five, automatic generation of love words

Of course, now there is a disadvantage, since to love, it is necessary to send a more pleasant words, we also call rainbow fart…

There is a rainbow fart generation website, website name is very interesting, their experience.

We use the interface of this site to generate the content we want to say.

Use the AXIos module to download the generated love words.

Install axios and enter the following command in the terminal

npm i axios
Copy the code

Using axios to get the love words, add the following code to index.js:

const { default: Axios } = require("axios");
function getHoneyedWords() {
  var url = "https://chp.shadiao.app/api.php";
  // Get information about this interface
  return Axios.get(url);
}
Copy the code

6. Use email to send love messages

Add the test mail sending code in index.js as follows

// Get love words
getHoneyedWords().then(res= >{
    console.log(res.data)
  // Send an email
    sendMail(res.data);
})

Copy the code

Enter node index.js in terminal

** results show: ** Spring water is born, the beginning of spring Lin sheng, spring breeze ten miles, not as good as you!

Seven. Send it regularly every day

Considering that regular delivery every day will show more sincerity, then make a regular delivery. We need to start a scheduled task using the node-schedule module.

Install, enter the following information in terminal

npm install node-schedule
Copy the code

Add the following code using index.js

const schedule = require("node-schedule");
// It will be delivered at 5:21 p.m. every day
schedule.scheduleJob({ hour: 17.minute: 21 }, function () {
  console.log("Start task :" + new Date());
  getHoneyedWords().then((res) = > {
    console.log(res.data);
    sendMail(res.data);
  });
});

Copy the code

Enter node index.js in terminal

Then every day at 5:21 PM will automatically send a love sentence!

Even the status of the family has been raised!