Open DevCEDTeam opened 8 months ago
To accomplish the tasks you've outlined in a Google Colab environment, including uploading contact data into Node Mailer, sending contacts to Mautic, generating unique token tags and dynamic URL links in Mautic, and sending outbound emails via the Gmail API, you can follow these step-by-step instructions and integrate the provided code snippets.
Step 1: Install the Latest Version of Node.js in Google Colab
Install Node.js and npm in your Google Colab environment by running the following commands:
!curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
!sudo apt-get install -y nodejs
!sudo apt-get install -y npm
Step 2: Install nodemailer and google-auth-library
Install the required Node.js packages, nodemailer, and google-auth-library:
!npm install nodemailer google-auth-library
Step 3: Set Up Google Service Account Credentials
Use the Google service account credentials to access the Gmail API:
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
SERVICE_ACCOUNT_FILE = 'path/to/service_account.json'
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
Step 4: Authenticate with the Gmail API
Set up a function to send emails using the Gmail API:
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from google.oauth2.credentials import Credentials
def send_email(to, subject, body):
try:
service = build('gmail', 'v1', credentials=creds)
message = MIMEMultipart()
text = MIMEText(body)
message.attach(text)
message['to'] = to
message['subject'] = subject
create_message = {'raw': base64.urlsafe_b64encode(message.as_bytes()).decode()}
send_message = (service.users().messages().send(userId="me", body=create_message).execute())
print(F'sent message to {to} Message Id: {send_message["id"]}')
except HttpError as error:
print(F'An error occurred: {error}')
send_message = None
return send_message
Step 5: Creating the Email Message Payload and Sending Emails
Create an email message payload and send emails using the Gmail API client:
# Define the recipient, subject, and body
to = 'recipient@example.com'
subject = 'Test email'
body = 'This is a test email sent via the Gmail API'
# Send the email
send_email(to, subject, body)
Please replace 'path/to/service_account.json' with the actual file path to your service account credentials. This code demonstrates how to set up Gmail API integration and send an email. You can extend it to handle more complex email campaigns by combining it with Node Mailer, Mautic, and other tools according to your specific requirements.
If you are using Google Colab to install the
nodemailer
library and run the script, you can follow these steps. Note thatnodemailer
is a JavaScript library, so you will need to use Node.js to run the script. Here's how you can set up and execute it:nodemailer
library:send_email.js
, with the following content:Make sure to replace the placeholders in the JavaScript code with your actual Gmail email, client ID, client secret, refresh token, and access token.
This setup will allow you to install the
nodemailer
library in your Google Colab environment and run the script to send email messages using Gmail API and OAuth2 client credentials.