neurostuff / neurostore

The NeuroStore/Neurosynth application
https://compose.neurosynth.org
9 stars 6 forks source link

mailing: add new users to mailjet contact list #684

Closed adelavega closed 8 months ago

adelavega commented 9 months ago

We are using "mailjet" to send marketing campaigns.

I'm doing bulk import of existing user email addresses, but in the future we need to ping the mailjet API to add new users to the mailing list.

It's fairly straight forward: https://dev.mailjet.com/email/guides/contact-management/#manage-multiple-contacts-in-a-list

First we add a new contact, then associate that user with a mailing list. The ListID in this case is 122473.

jdkent commented 9 months ago

added this code to auth0 as an action upon registration:

const mailjet = require('node-mailjet');

/**
 * Handler that will be called during the execution of a PostUserRegistration flow.
 *
 * @param {Event} event - Details about the context and user that has registered.
 * @param {PostUserRegistrationAPI} api - Methods and utilities to help change the behavior after a signup.
 */
exports.onExecutePostUserRegistration = async (event, api) => {
  const mailjetConnection = mailjet.apiConnect(event.secrets.MJ_APIKEY_PUBLIC, event.secrets.MJ_APIKEY_PRIVATE);

  mailjetConnection
    .post("contact", { 'version': 'v3' })
    .request({
      "IsExcludedFromCampaigns": false,
      "Name": event.user.name,
      "Email": event.user.email
    })
    .then((contactResult) => {
      console.log(contactResult.body);

      // Run the second request only if the first request is successful
      return mailjetConnection
        .post("listrecipient", { 'version': 'v3' })
        .request({
          "IsUnsubscribed": false,
          "ContactAlt": event.user.email,
          "ListID": "122473",
        });
    })
    .then((listRecipientResult) => {
      console.log(listRecipientResult.body);
    })
    .catch((error) => {
      console.error(error.statusCode);
    });
};
jdkent commented 8 months ago

going to close this as this is currently deployed