Use Case-email-automation

Email Automation with CustomJS

Introduction

With CustomJS, you can set up automated emails that respond to various events and data from different platforms. In this article, we'll explore how to use CustomJS to implement email automation with Salesforce, Airtable, Google Spreadsheet, and Asana, by executing the CustomJS function via links. You will also find all of the following examples in our platform as templates when creating a function.

Why CustomJS for Email Automation?

CustomJS is a powerful SaaS platform that enables businesses to build custom automations and integrations tailored to their specific needs. With CustomJS, you can dynamically generate email content using predefined templates and prompts, ensuring personalized and relevant communication with your customers. Additionally, CustomJS allows for the automation of email sending via SMTP, making it a versatile tool for enhancing your email automation strategies.

Email Automation with ChatGPT
Email Automation with ChatGPT

Examples

An interesting usage of CustomJS is the dynamic generation of email text when a CRM link is clicked. With this feature, you can embed links in your CRM that generate personalised email content with a predefined ChatGPT prompt and dynamic parameters from the link when clicked. This allows you to easily create a AI-driven email personalisation in your sales process.

const OpenAI = require("openai");

const openai = new OpenAI({
  apiKey: variables['OpenAI API Key'],
});

const response = await openai.completions.create({
  model: "gpt-3.5-turbo-instruct",
  prompt: `
  Please write an email wihtout subject to your lead about the proof of concept status. 
  The name of my lead is: ${input.name}. 
  The project status is: ${input.projectStatus}.
  Please do not add greetings like “Best regards”, because they come from the email program.`,
  temperature: 1,
  max_tokens: 256,
  top_p: 1,
  frequency_penalty: 0,
  presence_penalty: 0,
});

const emailBody = response.choices[0].text;

const subject = 'Project Update';

return `
<!DOCTYPE html>
<html>
<body>
<script>
window.onload = function() {
  window.location.href = "mailto:${input.emailAddress}?subject=${subject}&body=${encodeURIComponent(emailBody)}";
}
</script>
</body>
</html>

2. Sending Emails via SMTP

The following function in CustomJS allows you to send an email via SMTP when accessed through a link. An HTML template can be defined, and dynamic values passed via GET parameters can be inserted using Nunjucks. Here is an example of how this can be technically implemented:

const nodemailer = require('nodemailer');
const nunjucks = require('nunjucks');

// SMTP Configuration
let transporter = nodemailer.createTransport({
    host: 'smtp.example.com',
    port: 587,
    secure: false,
    auth: {
        user: 'your-email@example.com',
        pass: 'your-email-password'
    }
});

// HTML Template
let template = `
<!DOCTYPE html>
<html>
<body>
    <h1>Hello {{ name }}</h1>
    <p>Your task is {{ task }}</p>
</body>
</html>
`;

// Email Function
function sendEmail(req, res) {
    let name = req.query.name;
    let task = req.query.task;

    let htmlContent = nunjucks.renderString(template, { name: name, task: task });

    let mailOptions = {
        from: 'your-email@example.com',
        to: 'recipient@example.com',
        subject: 'Automated Email',
        html: htmlContent
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return res.status(500).send(error.toString());
        }
        res.status(200).send('Email sent: ' + info.response);
    });
}

Integrating CustomJS with platforms like Salesforce, Airtable, Google Spreadsheet, and Asana allows you to create powerful email automation workflows. These integrations can be manually triggered by team members, providing flexibility and control over the email automation process. Explore the provided links to learn more about each integration and start enhancing your email automation processes today.


Use Cases