Created
December 24, 2021 02:31
-
-
Save diegofcornejo/a268538abf1d3887d2d281f4da135343 to your computer and use it in GitHub Desktop.
Send bulk html emails based on xlsx file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const readXlsxFile = require('read-excel-file/node'); | |
const MAIL = require('./mail'); | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async function processArray(array) { | |
for (const item of array) { | |
await sleep(500); | |
console.log(item); | |
var data = { | |
'from': { 'name': 'Random Tech Guy', email: '[email protected]' }, | |
'to': item[1], | |
'subject': 'Welcome to Random Tech Guy Blog', | |
'html': MAIL.generate(item) | |
} | |
const headers = { | |
'Content-Type': 'application/json', | |
'x-api-key': 'api-key' | |
} | |
// Uncomment if you need generate a html file for preview | |
// require('fs').writeFileSync('preview.html', MAIL.generate(item), 'utf8'); | |
axios.post('https://api.example.com/v1/mail/send', data, { | |
headers: headers | |
}) | |
.then((response) => { | |
console.log(response.data); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}) | |
}; | |
} | |
readXlsxFile('./list.xlsx').then((rows) => { | |
processArray(rows); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
module.exports = { | |
generate: function(item) { | |
var template = | |
`<!doctype html> | |
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"> | |
<head> | |
<title> Random Tech Guy </title> | |
<!--[if !mso]><!--> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<!--<![endif]--> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style type="text/css"> | |
#outlook a { | |
padding: 0; | |
} | |
body { | |
margin: 0; | |
padding: 0; | |
-webkit-text-size-adjust: 100%; | |
-ms-text-size-adjust: 100%; | |
} | |
table, | |
td { | |
border-collapse: collapse; | |
mso-table-lspace: 0pt; | |
mso-table-rspace: 0pt; | |
} | |
img { | |
border: 0; | |
height: auto; | |
line-height: 100%; | |
outline: none; | |
text-decoration: none; | |
-ms-interpolation-mode: bicubic; | |
} | |
p { | |
display: block; | |
margin: 13px 0; | |
} | |
</style> | |
<!--[if mso]> | |
<noscript> | |
<xml> | |
<o:OfficeDocumentSettings> | |
<o:AllowPNG/> | |
<o:PixelsPerInch>96</o:PixelsPerInch> | |
</o:OfficeDocumentSettings> | |
</xml> | |
</noscript> | |
<![endif]--> | |
<!--[if lte mso 11]> | |
<style type="text/css"> | |
.mj-outlook-group-fix { width:100% !important; } | |
</style> | |
<![endif]--> | |
<style type="text/css"> | |
@media only screen and (min-width:480px) { | |
.mj-column-per-100 { | |
width: 100% !important; | |
max-width: 100%; | |
} | |
} | |
</style> | |
<style media="screen and (min-width:480px)"> | |
.moz-text-html .mj-column-per-100 { | |
width: 100% !important; | |
max-width: 100%; | |
} | |
</style> | |
<style type="text/css"> | |
@media only screen and (max-width:480px) { | |
table.mj-full-width-mobile { | |
width: 100% !important; | |
} | |
td.mj-full-width-mobile { | |
width: auto !important; | |
} | |
} | |
</style> | |
</head> | |
<body style="word-spacing:normal;background-color:#F2F4FE;"> | |
<div style="display:none;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;max-width:0px;opacity:0;overflow:hidden;"> Bienvenido </div> | |
<div style="background-color:#F2F4FE;"> | |
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:F2F4FE;background-color:F2F4FE;width:100%;"> | |
<tbody> | |
<tr> | |
<td> | |
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" bgcolor="F2F4FE" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--> | |
<div style="margin:0px auto;max-width:600px;"> | |
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"> | |
<tbody> | |
<tr> | |
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-bottom:0;text-align:center;"> | |
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--> | |
<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"> | |
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"> | |
<tbody> | |
<tr> | |
<td align="center" style="font-size:0px;padding:0;word-break:break-word;"> | |
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"> | |
<tbody> | |
<tr> | |
<td style="width:600px; background:#FFFFFF"> | |
<a href="https://diegocornejo.com" target="_blank"> | |
<center><img alt height="auto" src="https://randomtechguy.vercel.app/images/logo.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:200px;font-size:13px;margin-top:20px" width="600"></center> | |
</a> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<!--[if mso | IE]></td></tr></table><![endif]--> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<!--[if mso | IE]></td></tr></table><![endif]--> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="body-section-outlook" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--> | |
<div class="body-section" style="-webkit-box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15); -moz-box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15); box-shadow: 1px 4px 11px 0px rgba(0, 0, 0, 0.15); margin: 0px auto; max-width: 600px;"> | |
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"> | |
<tbody> | |
<tr> | |
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-bottom:0;padding-top:0;text-align:center;"> | |
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" width="600px" ><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" bgcolor="#FFFFFF" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--> | |
<div style="background:#FFFFFF;background-color:#FFFFFF;margin:0px auto;max-width:600px;"> | |
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#FFFFFF;background-color:#FFFFFF;width:100%;"> | |
<tbody> | |
<tr> | |
<td style="direction:ltr;font-size:0px;padding:20px 0;padding-left:15px;padding-right:15px;text-align:center;"> | |
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:570px;" ><![endif]--> | |
<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"> | |
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"> | |
<tbody> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:20px;font-weight:bold;line-height:24px;text-align:left;color:#212b35;">Bienvenido a Random Tech Guy</div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;line-height:24px;text-align:left;color:#637381;">Hola ${item[0]},</div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;line-height:24px;text-align:left;color:#637381;">Desde ahora tendremos una nueva plataforma y queremos compartirte tus credenciales para ingresar.</div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;line-height:24px;text-align:left;color:#637381;">Usuario: ${item[1]} <br> Contraseña: ${item[2]}</div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;line-height:24px;text-align:left;color:#637381;">Para utilizar la plataforma haz click en este enlace: <br> | |
<a href="https://diegocornejo.com" target="_blank">https://diegocornejo.com</a> | |
</div> | |
</td> | |
</tr> | |
<tr> | |
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"> | |
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:16px;font-weight:400;line-height:24px;text-align:left;color:#637381;">Random Tech Guy</div> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<!--[if mso | IE]></td></tr></table><![endif]--> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<!--[if mso | IE]></td></tr></table></td></tr></table><![endif]--> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<!--[if mso | IE]></td></tr></table><![endif]--> | |
</div> | |
</body> | |
</html>`; | |
return template; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "bulk-mail-sender", | |
"version": "0.1.0", | |
"description": "Send bulk html emails", | |
"main": "index.js", | |
"author": "Diego Cornejo", | |
"license": "*", | |
"dependencies": { | |
"axios": "^0.24.0", | |
"read-excel-file": "^3.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment