I am trying to create an auto-emailer that, on sending details like name and email, sends an email to the given email address saying "subscribed.". It was a task for me but have been stuck here to around 7 hours straight and have tried various aspects.
app.post('/submit', (req, res) => {
const { name, email, subject , sender , to} = req.body;
let defaultClient = new brevo.ApiClient();
let apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'API_KEY';
let apiInstance = new Brevo. TransactionalEmailsApi();
let sendSmtpEmail = new Brevo. SendSmtpEmail();
sendSmtpEmail.to = [{ email: email, name: name }];
sendSmtpEmail.templateId = 59;
sendSmtpEmail.params = { name: name };
sentSmtpEmail.subject = {subject:subject};
sendSmtpEmail.sender = {sender:sender};
sendSmtpEmail.to = {to:to};
apiInstance.sendTransacEmail(sendSmtpEmail)
.then(function (data) {
console.log('Email sent successfully.');
res.send('Email sent successfully.');
})
.catch(function (error) {
console.error('Error sending email:', error);
res.status(500).send('Error sending email.');
});
});
I used the Brevo API for creating an auto-emailer and got this code from its documentation.
const brevo = require('@getbrevo/brevo');
var defaultClient = brevo.ApiClient.instance;
let apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = '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';
let apiInstance = new Brevo. TransactionalEmailsApi();
let sendSmtpEmail = new Brevo. SendSmtpEmail();
sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.htmlContent = "<html><body><h1>Common: This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = { "name": "John", "email": "example@example.com" };
sendSmtpEmail.to = [
{ "email": "example@brevo.com", "name": "sample-name" }
];
sendSmtpEmail.replyTo = { "email": "example@brevo.com", "name": "sample-name" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = { "parameter": "My param value", "subject": "common subject" };
apiInstance.sendTransacEmail(sendSmtpEmail).then(function (data) {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}, function (error) {
console.error(error);
});
After writing this code in the above format, i am getting errors like
TypeError: brevo.ApiClient is not a constructor
at E:\Projects\email-app1\server.js:25:25
at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Projects\email-app1\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (E:\Projects\email-app1\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
at E:\Projects\email-app1\node_modules\express\lib\router\index.js:284:15
at Function.process_params (E:\Projects\email-app1\node_modules\express\lib\router\index.js:346:12)
at next (E:\Projects\email-app1\node_modules\express\lib\router\index.js:280:10)
at E:\Projects\email-app1\node_modules\body-parser\lib\read.js:137:5
at AsyncResource.runInAsyncScope (node:async_hooks:206:9)
and after changing the line
let defaultClient = new Brevo. ApiClient()
to
let defualtClient = brevo.ApiClient.instance
i am getting this error
TypeError: Cannot read properties of undefined (reading 'instance')
at E:\Projects\email-app1\server.js:25:41
at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Projects\email-app1\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (E:\Projects\email-app1\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
at E:\Projects\email-app1\node_modules\express\lib\router\index.js:284:15
at Function.process_params (E:\Projects\email-app1\node_modules\express\lib\router\index.js:346:12)
at next (E:\Projects\email-app1\node_modules\express\lib\router\index.js:280:10)
at E:\Projects\email-app1\node_modules\body-parser\lib\read.js:137:5
at AsyncResource.runInAsyncScope (node:async_hooks:206:9)
I also tried using @getbrevo/brevo
I continued searching through the documentation and this seems to work
var SibApiV3Sdk = require('sib-api-v3-sdk');
var defaultClient = SibApiV3Sdk.ApiClient.instance;
// Configure API key authorization: api-key
var apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'YOUR_API_KEY';
// Uncomment below two lines to configure authorization using: partner-key
// var partnerKey = defaultClient.authentications['partner-key'];
// partnerKey.apiKey = 'YOUR API KEY';
var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();
var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email
sendSmtpEmail = {
to: [{
email: 'testmail@example.com',
name: 'John Doe'
}],
templateId: 59,
params: {
name: 'John',
surname: 'Doe'
},
headers: {
'X-Mailin-custom': 'custom_header_1:custom_value_1|custom_header_2:custom_value_2'
}
};
apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
console.log('API called successfully. Returned data: ' + data);
}, function(error) {
console.error(error);
});