Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created May 27, 2019 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominicFinn/559df73149f56dcae2c2d01c8a6f3c2f to your computer and use it in GitHub Desktop.
Save DominicFinn/559df73149f56dcae2c2d01c8a6f3c2f to your computer and use it in GitHub Desktop.
How to create an automated email sender

Instructions for emailing sillyness.

You will need a gmail account, best of creating a new one, I just appended robot* in front of my normal one. You then need to go to https://myaccount.google.com/lesssecureapps and switch this on (hence it best to create a new account).

start a new node app

node init -y

install the required dependencies

npm install express nodemailer node-cron

create the index.js file (see in gist)

then run the fun

node index.js
const cron = require("node-cron");
const express = require("express");
const nodemailer = require("nodemailer");
const app = express();
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "<robot.your.name@gmail.com>",
pass: "<password-here>"
}
});
/*
* * * * * *
| | | | | |
| | | | | day of week
| | | | month
| | | day of month
| | hour
| minute
second ( optional )
*/
cron.schedule("* * * * *", function() {
let mailOptions = {
from: "", // sender address
to: "", // list of receivers
subject: "Subject of your email", // Subject line
html: "<p>Your html here</p>" // plain text body
};
transporter.sendMail(mailOptions, function(err, info) {
if (err) console.log(err);
else console.log(info);
});
});
app.listen(3128);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment