Skip to content

Commit 8db0561

Browse files
feat(tools): add batch email sending utility (#334)
* feat(tools): add batch email sending utility * fix issued by coderabbit * removed email * file name changed
1 parent b407d55 commit 8db0561

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

tools/emails.txt

Whitespace-only changes.

tools/promotional-emails.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { Resend } = require('resend');
4+
require('dotenv').config();
5+
6+
const resend = new Resend(
7+
process.env.RESEND_API_KEY_2 ||
8+
process.env.RESEND_API_KEY
9+
);
10+
11+
const BATCH_SIZE = 100;
12+
13+
async function sendEmail(email) {
14+
return resend.emails.send({
15+
from: process.env.EMAIL_FROM || 'urBackend <urbackend@apps.bitbros.in>',
16+
to: email,
17+
subject: 'Try urBackend',
18+
html: `
19+
<h2>Hi there 👋</h2>
20+
21+
<p>I'm building urBackend, an open-source backend platform that helps developers build APIs and backend services faster.</p>
22+
23+
<p>I'd love for you to try it out and share feedback.</p>
24+
25+
<p>
26+
<a href="https://urbackend.bitbros.in">
27+
Try urBackend
28+
</a>
29+
</p>
30+
31+
<p>Thanks!</p>
32+
`
33+
});
34+
}
35+
36+
async function main() {
37+
const emails = fs
38+
.readFileSync(
39+
path.join(__dirname, 'emails.txt'),
40+
'utf8'
41+
)
42+
.split(/\r?\n/)
43+
.map(e => e.trim())
44+
.filter(Boolean);
45+
46+
const parsed = Number(process.argv[2]);
47+
const batchSize = Number.isInteger(parsed) && parsed > 0 ? parsed : BATCH_SIZE;
48+
const batch = emails.slice(0, Math.min(batchSize, emails.length));
49+
console.log(`Sending ${batch.length} emails...`);
50+
51+
const redactEmail = (value) => {
52+
const [local, domain] = String(value).split('@');
53+
if (!domain) return '***';
54+
return `${local?.slice(0, 2) || '**'}***@${domain}`;
55+
};
56+
57+
for (const email of batch) {
58+
try {
59+
await sendEmail(email);
60+
console.error(`✗ ${redactEmail(email)}`, err.message);
61+
await new Promise(resolve =>
62+
setTimeout(resolve, 1000)
63+
);
64+
65+
} catch (err) {
66+
console.error(`✗ ${redactEmail(email)}`, err.message);
67+
}
68+
}
69+
70+
console.log('Finished');
71+
}
72+
73+
main();

0 commit comments

Comments
 (0)