Build an Email Marketing System in Just 5 Minutes
Build your own email marketing tool

If you want to let many people know about your business, service, or product, you need to campaign through email. You might have noticed that a lot of websites have a newsletter section at the bottom. The main purpose is to send emails to a group of people.
Why create a newsletter?
- Promote a new service/product
- Occasional mail
- Notice
- Notify about subscription renewal
Requirements
- Node.js installed in your system
- An email account from where you want to send email
- A list of emails to who you want to send the email
- An email title and body
Let's get started.
Install Node.js in our system
You can install node.js by downloading, or you can also install it in many ways. Just follow the instruction on the node.js website.
URL: https://nodejs.org/en/
Install node mailer
Many other modules can be used for email, but here, we will use Nodemailer in our case.
Nodemailer is a module for Node.js applications to allow easy as cake email sending.
Here is a single line command that will install Nodemailer using npm.
npm install nodemailer
Why Nodemailer?
- A single module with a zero dependency
- Much secure
- Unicode supports so you are capable of using any language
- The environment doesn't matter. Any machine with Node.js is capable
- HTML & CSS supported that means supports custom design
- Attachment supported
There are many more in the official documentation. Please have a look.
URL: https://nodemailer.com/about/
Let's get started
You need to set up an email with an application password. Application password is not mandatory, but this is a best practice.
Put your credential here.
auth: {
user: '[email protected]',
pass: 'demo1234',
},
One important thing that might be helpful. Make this false if you are under development and you don't have an SSL certificate. But if you have deployed to your secure server, then you must turn this true.
secure: false,
Here is a complete demo you can use. Just change the auth credential, and you are ready to go.
Here in this mail, you just sent a greeting message without any style. Now let's try to design more.
let info = await transporter.sendMail({
from: '"Shoaib Mehedi 👻" <[email protected]>',
to: "[email protected], [email protected]",
subject: "Greeting! ✔",
html: "<b>Hello from hell!!</b>",
});
Look at the change. I just replaced the text portion with HTML. Now our mail looks better than before. We can use here inline CSS to style more.
Attach Image
var mailOptions = {
//...
html: 'Embedded image: <img src="cid:unique"/>',
attachments: [{
filename: 'image.png',
path: '/path/to/file',
cid: 'unique'
}]
}
In your mail body, where you are using the image tag, just write like below. Then what we need to configure the attachment section like below.
var mailOptions = {
//...
html: 'Embedded image: <img src="cid:unique"/>',
attachments: [{
filename: 'image.png',
path: '/path/to/file',
cid: 'unique'
}]
}
Note: Same cid value as in the html img src is mandatory, otherwise this will not work.
Yes, that's it. Now, let's attach different types of files to our mail body.
Let's attach something else
Here is an example of attaching different kinds of attachments that mail supports. Please have a look.
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '[email protected]', // Email address from where you wnt to send email
pass: 'demo1234', // Email pass section- Best practice is to generate application password and use it here
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Shoaib Mehedi 👻" <[email protected]>', // sender address
to: "[email protected], [email protected]", // list of receivers
subject: "Greeting! ✔", // Subject line
text: "Hello!", // plain text body
});
}
main().catch(console.error);
Source: Nodemailer
That's all. Now you are ready to use your own mail marketing system.
Schedule mail service
Suppose you want to email a group of people after a certain period, then you don't need to run your script manually. You can use a scheduler to do that. Here is an article you can follow to do that.
Schedule your tasks with Cron Job
Conclusion
You can use the script that I mentioned without any hassle. Here is a link you can learn more about Nodemailer from the documentation. Thank you for reading.
Have a great day!



Comments
There are no comments for this story
Be the first to respond and start the conversation.