Skip to content

Instantly share code, notes, and snippets.

@Pompeu
Last active July 21, 2021 01:17
Show Gist options
  • Save Pompeu/06fe610ba82ef0b4c82026c8073aa04b to your computer and use it in GitHub Desktop.
Save Pompeu/06fe610ba82ef0b4c82026c8073aa04b to your computer and use it in GitHub Desktop.
send_email.js
// comandos
// npm init
// npm install rxjs
// npm install @sendgrid/mail
const fs = require("fs");
const { from } = require("rxjs");
const { filter, map } = require("rxjs/operators");
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey('sua chave sandgrid aqui');
const removeBlankLines = (x) => x !== "";
const mailBuild = (email) => {
const body = {
to: email,
from: "noreplay@nodejsbrasil.com.br",
subject: "Sandgrid Test",
text: "Pompeulimp eu vou te seguir insta relaxa!! @pompeulimp",
html: `<strong>Ola vem seguir no insta vem por favor? Sr. ${email}</strong>`,
};
return body;
};
const sendEmail = (body) =>
sgMail
.send(body)
.then((result) => {
console.log("Email enviado");
console.log(result);
})
.catch((error) => {
console.error(error);
if (error.response) {
console.error(error.response.body);
}
});
const emaillist = fs.readFileSync("./mail_list.txt", "utf8").split("\n");
from(emaillist)
.pipe(filter(removeBlankLines), map(mailBuild))
.subscribe(sendEmail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment