DevCEDTeam / CED

0 stars 0 forks source link

Description #53

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

Step-by-step guide to transport bulk email addresses from the Mautic console to the Gmail API using the Google OAuth 2.0 Client, Google Firebase, and Node.js:

DevCEDTeam commented 1 year ago
  1. Install the required packages:

    • googleapis: This package provides access to various Google APIs, including the Gmail API.
    • google-auth-library: This package allows authentication with Google services using OAuth 2.0.

    You can install these packages by running the following command in your project directory:

    npm install googleapis google-auth-library
  2. Set up a Google Cloud project and enable the Gmail API:

    • Go to the Google Cloud Console and create a new project.
    • Enable the Gmail API for your project by navigating to the APIs & Services > Library section and searching for "Gmail API". Click on it, and then click the Enable button.
    • Next, create credentials for your project to authenticate the requests. Navigate to the APIs & Services > Credentials section, click the Create credentials button, and choose OAuth client ID. Follow the on-screen instructions to create a client ID and secret.
    • Make sure to add the appropriate redirect URIs (e.g., http://localhost:3000/auth/callback) for your application.
  3. Implement the code to send bulk emails using the Gmail API:

    • Import the necessary modules in your Node.js script:

      const { google } = require('googleapis');
      const { OAuth2Client } = require('google-auth-library');
    • Initialize the OAuth2 client with your credentials:

      const oAuth2Client = new OAuth2Client(
      'YOUR_CLIENT_ID',
      'YOUR_CLIENT_SECRET',
      'YOUR_REDIRECT_URI'
      );
    • Obtain an access token by generating an authorization URL and authorizing your application:

      const authorizeUrl = oAuth2Client.generateAuthUrl({
      access_type: 'offline',
      scope: ['https://www.googleapis.com/auth/gmail.send']
      });
      
      // Redirect the user to `authorizeUrl` and handle the callback to obtain the authorization code
      
      // Once you have the authorization code, exchange it for an access token
      const { tokens } = await oAuth2Client.getToken(authorizationCode);
      oAuth2Client.setCredentials(tokens);
    • Use the Gmail API to send emails:

      const gmail = google.gmail({ version: 'v1', auth: oAuth2Client });
      
      const sendMessage = async (message) => {
      try {
       const res = await gmail.users.messages.send({
         userId: 'me',
         requestBody: { raw: message }
       });
       console.log('Message sent:', res.data);
      } catch (error) {
       console.error('Error sending message:', error);
      }
      };

      The message parameter in the sendMessage function should be a formatted email message, including headers like "To", "Subject", and the email body.

  4. Use the above implementation to transport bulk email addresses:

    • Read your bulk email addresses from Mautic or any other source.
    • Iterate over the list of email addresses and call the sendMessage function for each address, passing the appropriate email content.

Step-by-step guide on how to configure Gmail API with Google OAuth 2.0 Client using Google Firebase and Node.js:

  1. Set up a new project on Google Cloud Console and enable the Gmail API (as mentioned in the previous alternative solution).

  2. Create a Firebase project:

    • Go to the Firebase Console.
    • Create a new project or select an existing project linked to your Google Cloud project.
    • Follow the instructions to set up Firebase for your project.
  3. Install the Firebase CLI by running the following command in your project directory:

    npm install -g firebase-tools
  4. Authenticate the Firebase CLI by running firebase login.

  5. Initialize Firebase in your project directory:

    • Run firebase init to initialize the Firebase project.
    • Select the Firebase features you want to use, such as Realtime Database or Firestore.
    • Choose an existing Firebase project associated with your Google Cloud project.
  6. Deploy the functions to Firebase:

    • In the Firebase project folder, navigate to the functions folder.
      cd functions
    • Edit the index.js file and replace its contents with the previously provided code example for sending emails using the Gmail API.
    • Update the necessary placeholders (YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_REDIRECT_URI) with your actual values.
    • Save the changes to the index.js file.
  7. Deploy the Firebase functions by running the following command:

    firebase deploy --only functions
  8. Test the deployment by calling the Firebase function endpoint from your application or using tools like Postman.

With the above steps, you should be able to transport bulk email addresses from the Mautic console to the Gmail API using the Google OAuth 2.0 Client, Google Firebase, and Node.js.

DevCEDTeam commented 1 year ago

//