Created
September 22, 2022 05:28
-
-
Save longseespace/62e3a3771db4ca1797fded762bf19aa0 to your computer and use it in GitHub Desktop.
Send bulk email to users using MailerSend
This file contains 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 Recipient = require('mailersend').Recipient; | |
const EmailParams = require('mailersend').EmailParams; | |
const BulkEmails = require('mailersend').BulkEmails; | |
const MailerSend = require('mailersend'); | |
const users = require('./users'); | |
const MAILERSEND_API_KEY = process.env.MAILERSEND_API_KEY; | |
if (!MAILERSEND_API_KEY) { | |
console.error('MAILERSEND_API_KEY is required'); | |
process.exit(1); | |
} | |
const subject = '[Action Required] Update your approved email address at amazon.com'; | |
const templateId = 'your-unique-mailersend-template-id'; | |
const fromEmail = '[email protected]'; | |
const fromName = 'Daniel from KTool'; | |
const mailersend = new MailerSend({ | |
api_key: MAILERSEND_API_KEY, | |
}); | |
const bulkEmails = new BulkEmails(); | |
users.forEach((user) => { | |
const email = user.email; | |
const name = user.name || 'there'; | |
const recipients = [new Recipient(email, name)]; | |
const personalization = [ | |
{ | |
email, | |
data: { | |
name, | |
}, | |
}, | |
]; | |
const emailParams = new EmailParams() | |
.setFrom(fromEmail) | |
.setFromName(fromName) | |
.setRecipients(recipients) | |
.setSubject(subject) | |
.setTemplateId(templateId) | |
.setPersonalization(personalization); | |
bulkEmails.addEmail(emailParams); | |
}); | |
mailersend | |
.sendBulk(bulkEmails) | |
.then((response) => response.json()) | |
.then((data) => { | |
console.log(data); | |
mailersend | |
.getBulkEmailRequestStatus(data) | |
.then((response) => response.json()) | |
.then((data) => console.log(data)); | |
}); |
This file contains 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
module.exports = [ | |
{ email: '[email protected]', name: 'Steve Jobs' }, | |
{ email: '[email protected]', name: 'Daniel Nguyen' }, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment