DevCEDTeam / CED

0 stars 0 forks source link

Description #22

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

Mautic to GMail Integration

Step-by-step instructions that include gcloud CLI commands and sample code for installing, building, and deploying Mautic on a hosted container, configuring outbound SMTP settings, and using Cron jobs to send email messages through the Gmail API on Google Cloud using Cloud Functions for Firebase with Visual Studio Code as the code editor:

Step 1: Set up Google Cloud project and gcloud CLI

  1. Create a new project on Google Cloud Console (https://console.cloud.google.com) and note down the Project ID.
  2. Install the gcloud CLI (Google Cloud SDK) by following the instructions for your operating system: https://cloud.google.com/sdk/docs/install

Step 2: Enable necessary APIs and services

  1. Enable the necessary APIs:
    gcloud services enable containerregistry.googleapis.com
    gcloud services enable container.googleapis.com
    gcloud services enable cloudbuild.googleapis.com
    gcloud services enable pubsub.googleapis.com
    gcloud services enable cloudfunctions.googleapis.com
    gcloud services enable gmail.googleapis.com

Step 3: Set up Mautic on a hosted container

  1. Create a new directory for your Mautic project.
  2. Open a terminal or command prompt and navigate to the project directory.
  3. Create a Dockerfile in the project directory with the following content:

    FROM mautic/mautic:latest
    
    # Copy custom configuration files, if any
    COPY mautic-config/* /var/www/html/
    
    # Set up Cron jobs file
    COPY cron-jobs /etc/cron.d/mautic-cron-jobs
    
    # Give execution rights to the Cron jobs file
    RUN chmod 0644 /etc/cron.d/mautic-cron-jobs
    
    # Apply Cron jobs
    RUN crontab /etc/cron.d/mautic-cron-jobs
    
    # Expose Mautic port
    EXPOSE 80
  4. Create a cron-jobs file in the project directory with the following content:
    * * * * * www-data /usr/local/bin/php /var/www/html/app/console mautic:segments:update
    * * * * * www-data /usr/local/bin/php /var/www/html/app/console mautic:campaigns:update
  5. Build and tag the Docker image:
    docker build -t gcr.io/[PROJECT-ID]/mautic:v1 .
  6. Push the Docker image to Google Container Registry:
    docker push gcr.io/[PROJECT-ID]/mautic:v1

Step 4: Configure outbound SMTP settings on a hosted container

  1. Log in to your Mautic instance.
  2. Go to "Configuration" > "Email Settings".
  3. Configure the SMTP settings of your email provider (e.g., Gmail, SMTP server).
  4. Save the settings.

Step 5: Set up a Node.js project

  1. Open Visual Studio Code (https://code.visualstudio.com) or any preferred code editor.
  2. Create a new directory for your Cloud Functions project.
  3. Open a terminal in the project directory.
  4. Initialize a new Node.js project:
    npm init -y
  5. Install the necessary packages:
    npm install firebase-functions@latest firebase-admin@latest googleapis@latest nodemailer@latest

Step 6: Create a Cloud Function for sending emails

  1. Create a file named index.js in the project directory with the following content:

    const functions = require('firebase-functions');
    
    const { google } = require('googleapis');
    const nodemailer = require('nodemailer');
    
    // Gmail API credentials
    const credentials = require('./gmail-credentials.json');
    
    // Cloud Function to send email
    exports.sendEmail = functions.pubsub.schedule('every 1 minutes').onRun(async (context) => {
     try {
       // Create OAuth2 client
       const oauth2Client = new google.auth.OAuth2(
         credentials.client_id,
         credentials.client_secret,
         credentials.redirect_uris[0]
       );
    
       // Set access token for the client
       oauth2Client.setCredentials({
         access_token: credentials.access_token,
         refresh_token: credentials.refresh_token,
         expiry_date: credentials.expiry_date,
       });
    
       // Create a transporter with Gmail API
       const transporter = nodemailer.createTransport({
         service: 'gmail',
         auth: {
           type: 'OAuth2',
           user: 'your-email@gmail.com',
           clientId: credentials.client_id,
           clientSecret: credentials.client_secret,
           refreshToken: credentials.refresh_token,
           accessToken: credentials.access_token,
         },
       });
    
       // Send email
       await transporter.sendMail({
         from: 'your-email@gmail.com',
         to: 'recipient@example.com',
         subject: 'Hello from Mautic!',
         text: 'This is a test email sent through Mautic and Gmail API.',
       });
    
       console.log('Email sent successfully.');
       return null;
     } catch (error) {
       console.error('Error sending email:', error);
       return null;
     }
    });
  2. Create a file named gmail-credentials.json in the project directory and populate it with your Gmail API credentials. Make sure to replace the placeholder values with your actual credentials:
    {
     "client_id": "YOUR_CLIENT_ID",
     "client_secret": "YOUR_CLIENT_SECRET",
     "redirect_uris": ["YOUR_REDIRECT_URI"],
     "refresh_token": "YOUR_REFRESH_TOKEN",
     "access_token": "YOUR_ACCESS_TOKEN",
     "expiry_date": "YOUR_EXPIRY_DATE"
    }

Step 7: Deploy the Cloud Function

  1. Open a terminal in the project directory.
  2. Authenticate the gcloud CLI with your Google Cloud account:
    gcloud auth login
  3. Set the current project:
    gcloud config set project [PROJECT-ID]
  4. Deploy the Cloud Function:
    gcloud functions deploy sendEmail --runtime nodejs14 --trigger-topic email-trigger

Step 8: Set up a Cloud Scheduler job

  1. Open Google Cloud Console (https://console.cloud.google.com) and select your project.
  2. In the sidebar, go to "Cloud Scheduler" under "Tools".
  3. Click on "Create Job".
  4. Provide a name for the job.
  5. Set the frequency using the "Frequency" field (e.g., * * * * * for every minute).
  6. Set the target as "Pub/Sub".
  7. In the "Pub/Sub topic" field, enter email-trigger.
  8. Click on "Create".

That's it! You have now installed, built, and deployed Mautic on a hosted container, configured outbound SMTP settings, and set up Cron jobs to send email messages through the Gmail API on Google Cloud using Cloud Functions for Firebase.

DevCEDTeam commented 1 year ago

Instructions to Send SMTP Email Messages via GMail API

To send email messages through the Gmail API, Mautic integrates with the Gmail API using the following steps:

  1. Enable Gmail API: In the Google Cloud Console, you need to enable the Gmail API for your project. This step was covered in the previous instructions.

  2. Obtain OAuth 2.0 Credentials: Mautic needs OAuth 2.0 credentials to access the Gmail API on behalf of the user. Follow these steps to obtain the credentials:

    a. Go to the Google Cloud Console and navigate to the "APIs & Services" > "Credentials" section.

    b. Click on "Create Credentials" and select "OAuth client ID".

    c. Select the application type as "Web application".

    d. Provide a name for the OAuth client ID and specify the authorized JavaScript origins and redirect URIs. These should match the URLs of your Mautic installation.

    e. Click "Create" to generate the OAuth client ID and client secret.

    f. Make note of the client ID and client secret as you'll need them later.

  3. Configure Mautic SMTP Settings: In Mautic's configuration, you need to set up the outbound SMTP settings to connect with the Gmail API. Follow these steps:

    a. Log in to your Mautic instance as an administrator.

    b. Navigate to the "Configuration" menu and select "Email Settings".

    c. Configure the SMTP settings with the following values:

    • SMTP Host: smtp.gmail.com
    • SMTP Port: 587
    • Encryption: TLS
    • SMTP Authentication: Login
    • SMTP Username: Your Gmail email address
    • SMTP Password: Your Gmail account password or an app-specific password if you have two-factor authentication enabled.
  4. Grant Access to Gmail API: Mautic needs access to the Gmail API on behalf of the user. Follow these steps to authorize Mautic:

    a. In your Mautic instance, navigate to the "Configuration" menu and select "Plugins".

    b. Find the "Email Settings" plugin and click on the "Gmail API" tab.

    c. Click on the "Authorize Gmail Account" button.

    d. You will be redirected to the Google OAuth consent screen. Select the Gmail account you want to authorize.

    e. Grant the necessary permissions requested by Mautic to access the Gmail API.

    f. Once authorized, you will be redirected back to Mautic.

With these steps, Mautic is now configured to send email messages through the Gmail API. When Mautic triggers the email sending process, it uses the configured SMTP settings and the authorized Gmail API access to send emails on behalf of the user.