Using sendinblue (or other email providers) with strapi

Hi,

Looking at lifecycle hooks, I have found what I wanted. Here I can send a transactional email when a user register to the newsletter, using the sendinblue api (which makes more sense to me).
I share the code, if it might help someone else…

‘’'javascript
const validator = require(‘validator’);
const fetch = require(‘node-fetch’);

module.exports = {
lifecycles: {

async beforeCreate(data) {
  data.email = validator.normalizeEmail(data.email);
},

async afterCreate(result, data) {

  //step 1: add contact
  let url = 'https://api.sendinblue.com/v3/contacts';
  let options = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'api-key': 'api key'
    },
    body: JSON.stringify({email: result.written_email})
  };

  let isAdded = null;
  try {
    const response = await fetch(url, options);
    isAdded = await response.json();
  } catch (error) {  
  }

  //step 2: send transactional email
  url = 'https://api.sendinblue.com/v3/smtp/email';
  options.body = JSON.stringify({to: [{email: result.written_email}], templateId: 2});

  let isSent = null;
  try {
    const response = await fetch(url, options);
    isSent = await response.json();
  } catch (error) { 
  }

}

},

};
‘’’

2 Likes