DEV Community

David
David

Posted on

So I Got Tired of Sending Emails All Day... So I Coded This Bot Instead.

Hey folks. Let's be real for a second - how many of you have found yourself stuck in "email hell" when trying to grow your side projects? I sure have.

Picture this: it's 11 PM, I'm on my third cup of coffee, manually sending the same dang email to different people. I'm a developer, for crying out loud! I automate things for a living, but here I was acting like a human copy-paste machine.

So I did what any frustrated developer would do - I spent a weekend building something to fire myself from this boring job.
What This Thing Actually Does

Basically, I wanted a simple bot that could:

Remember who I've emailed and who I haven't

Send follow-ups automatically (because I always forget)

Not mess up people's names or links

Let me sleep while it works
Enter fullscreen mode Exit fullscreen mode

The Tools I Grabbed

I went with Node.js because, well, it's what I know best. Plus, the whole async thing is perfect for "hey, go send these emails and don't bother me while you're at it."

For actually sending emails, I used SendGrid. Their free tier is generous, and let's be honest - I don't want to deal with email deliverability drama. They handle all that SPF/DKIM nonsense that makes my head hurt.

And for storing data? I kept it stupid simple - just a JSON file. Yeah, I know, not exactly production-ready, but this is a prototype, people!
Here's the Actual Code I Wrote

First, the boring setup stuff:
bash

Seriously, that's it for packages

npm init -y
npm install @sendgrid/mail

Now for the "database" - and I use that term loosely:
javascript

// leads.json - it's just an array of people
[
{
"id": 1,
"email": "someone@example.com",
"firstName": "Alex",
"status": "new",
"myCoolAffiliateLink": "https://example.com/alex"
},
{
"id": 2,
"email": "anotherperson@website.com",
"firstName": "Taylor",
"status": "waiting_for_reply",
"myCoolAffiliateLink": "https://example.com/taylor"
}
]

The email templates live in their own file:
javascript

// emails.js
const emails = {
firstEmail: {
subject: "Hey {{firstName}}, thought you might like this",
body: `

Hi {{firstName}},

Stumbled across this resource and your name immediately popped into my head.

Figured I'd share: check it out here

Cheers,
Your Name

`
Enter fullscreen mode Exit fullscreen mode

},

followUp: {
subject: "Just following up",
body: `

Hey {{firstName}},

Circling back on my previous email - did you get a chance to look?

Here's that link again: {{myCoolAffiliateLink}}

`
Enter fullscreen mode Exit fullscreen mode

}
};

module.exports = emails;

And here's the main event - the actual bot:
javascript

const sgMail = require('@sendgrid/mail');
const people = require('./leads.json');
const emailTemplates = require('./emails.js');

// Pro tip: Keep your API key out of the code!
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

async function sendEmail(person, whichEmail) {
let subject = whichEmail.subject;
let body = whichEmail.body;

// This replaces {{firstName}} with actual names, etc
Object.keys(person).forEach(key => {
subject = subject.replace({{${key}}}, person[key]);
body = body.replace({{${key}}}, person[key]);
});

const email = {
to: person.email,
from: 'you@yourdomain.com', // Use a verified email!
subject: subject,
html: body,
};

try {
await sgMail.send(email);
console.log(✓ Email sent to ${person.email});
return true;
} catch (error) {
console.log(✗ Failed for ${person.email}:, error.message);
return false;
}
}

async function run() {
for (const person of people) {
let whichTemplate = null;

if (person.status === 'new') {
  whichTemplate = emailTemplates.firstEmail;
} else if (person.status === 'waiting_for_reply') {
  whichTemplate = emailTemplates.followUp;
}

if (whichTemplate) {
  const worked = await sendEmail(person, whichTemplate);
  if (worked) {
    console.log(`Updated ${person.firstName}'s status`);
    // Here you'd update their status in a real database
  }
}
Enter fullscreen mode Exit fullscreen mode

}
}

run();

How to Make This Thing Actually Work

Get a SendGrid account (it's free to start)

Verify your email so you can actually send stuff

Set your API key: export SENDGRID_API_KEY='your_key_here'

Run it: node mailbot.js
Enter fullscreen mode Exit fullscreen mode

Was It Worth It?

Honestly? Yeah. That weekend of coding saved me hours each week. The bot never forgets to follow up, never sends an email to the wrong person, and works while I'm asleep.

Is this ready for a Fortune 500 company? Absolutely not. But for my side projects? It's perfect.
Where I'd Take This Next

If I were to make this more robust, I'd probably:

Add a proper database (maybe SQLite to start)

Put a simple web interface on it

Handle bounce backs and unsubscribes properly

Add some basic analytics
Enter fullscreen mode Exit fullscreen mode

But for now? It works, and that's what matters.

What about you? Ever built something to automate your own grunt work? I'd love to hear about it in the comments!

Top comments (0)