Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beforeEach(function() {
// Create a new Twilio SendGrid instance.
const API_KEY = process.env.API_KEY;
sendgrid.setApiKey(API_KEY);
// Create a new importer with a batch size of 2.
this.contactImporter = new ContactImporter(sendgrid, {
batchSize: 2,
});
// this.spy = sinon.spy(ContactImporter.prototype, '_sendBatch')
this.sinon.spy(ContactImporter.prototype, '_sendBatch');
// Generate some test data.
const data = [];
for (i = 0; i < 5; i++) {
const item = {
email: 'example' + i + '@example.com',
first_name: 'Test',
last_name: 'User',
};
list_ids: [EmailService.LIST_IDS.MENTORS],
contacts: [
{
email: contact.email,
first_name: contact.name,
country: contact.country,
custom_fields: {
// We can clean our list in SG with this field
e2_T: isProduction ? 'production' : 'development',
},
},
],
}),
};
return await sgClient.request(request);
}
}
import sendgrid from "@sendgrid/mail"
import sendgridClient from "@sendgrid/client"
import nodemailer, { Transporter } from "nodemailer"
import { SENDGRID_API_KEY, devEmailOptions, isProduction } from "./config"
sendgrid.setApiKey(SENDGRID_API_KEY)
sendgridClient.setApiKey(SENDGRID_API_KEY)
interface MailArgs {
templateId: string
to: string[] | string
variables?: any
}
export class Mailer {
private readonly from: string = "Split Team "
private devMail: Transporter
constructor() {
this.devMail = nodemailer.createTransport(devEmailOptions)
}
send(args: MailArgs) {
exports.handler = function(event, context, callback) {
const {
SENDGRID_API_KEY,
SENDGRID_WELCOME_SENDER_EMAIL,
SENDGRID_WELCOME_SENDER_NAME,
SENDGRID_WELCOME_TEMPLATE_ID
} = process.env;
const body = JSON.parse(event.body);
const email = body.email;
const welcomeEmail = event.queryStringParameters.welcome_email === "true";
client.setApiKey(SENDGRID_API_KEY);
addSendgridRecipient(client, email)
.then((response, body) => {
if (welcomeEmail) {
sendWelcomeEmail(
client,
email,
SENDGRID_WELCOME_SENDER_EMAIL,
SENDGRID_WELCOME_SENDER_NAME,
SENDGRID_WELCOME_TEMPLATE_ID
)
.then(response => callback(null, { statusCode: response.statusCode, body: email + " added" }) )
.catch(err => callback(err, null));
} else {
callback(null, { statusCode: response.statusCode, body: "" });
}
})
export function getSendgrid(opts: any): any {
const gateways: GatewaysConfig = config.get("gateways");
client.setApiKey(gateways.sendgridApiKey);
const sendgrid = {
...opts,
apiKey: gateways.sendgridApiKey,
client
};
const request: ClientRequest = {
method: sendgrid.method,
url: sendgrid.url,
body: []
};
sendgrid.addSubscription = async (
email: string
): Promise<[ClientResponse, any]> => {
request.body = [{ email }];
constructor() {
sgMail.setApiKey(Config.sendGrid.API_KEY);
sgClient.setApiKey(Config.sendGrid.API_KEY);
}
async sendDev(args: MailArgs) {
const request = {
method: "GET",
url: `/v3/templates/${args.templateId}`,
}
const [template] = await sendgridClient.request(request)
const version = template.body.versions[template.body.versions.length - 1]
const html = this.interpolateVariables(args.variables, version.html_content)
const subject = version.subject
const text = this.interpolateVariables(
args.variables,
version.plain_content,
)
this.devMail.sendMail({
to: args.to,
from: this.from,
subject,
html,
text,
})
}
sendgrid.addSubscription = async (
email: string
): Promise<[ClientResponse, any]> => {
request.body = [{ email }];
return client.request(request);
};
constructor() {
//Set client, initialize substitution wrappers and secret rules
//filter
this.setClient(new Client());
this.setSubstitutionWrappers('{{', '}}');
this.secretRules = [];
}