How to Convert Emails into Support Tickets Gmail to Freshdesk

Advertisements

Create customer Support Tickets in Freshdesk with email messages in Gmail using Google Apps Script. Custom support solutions such as Freshdesk, Zoho Desk, or Zendesk provide an email address – such as [email protected] – for instantly initiating client tickets without the need to fill out complex online forms.

Any email received to your support email address is instantly transformed into a ticket, and the support request is issued a unique ticket ID.

When you create a ticket from an email address, the email subject becomes the ticket title, the email body becomes the ticket description, and the most crucial portion, the customer’s name and email address is extracted from the FROM field of the email message header. This is significant because when your helpdesk personnel answer to a ticket, the responses are delivered directly to the client.

Advertisements

Email Ticketing with Gmail Forwards – The Problem

This approach works perfectly when the client sends an email straight to the help desk email address, but what if they send an email to you, the support agent, and it is your responsibility to create a support ticket from the customer’s email?

If you forward the customer’s email message from your Gmail mailbox to Freshdesk, the ticket will still be created, but because it contains your name and email address in the FROM field, the helpdesk software will assign you as the customer even though you are only creating a support ticket on behalf of the customer. If any agent answers to such a ticket, you will be included in all communications rather than the original client because their email address is not included in the support ticket produced from the forwarded email.

I was looking into Freskdesk and ran into a similar problem. How to simply generate support tickets from existing Gmail email communications. Because I couldn’t find a ready-made solution, I opted to create one myself using the Freshdesk API and Google Apps Script.

Advertisements

Convert Gmail Emails to Freshdesk Tickets

To begin, you will need the domain (where your Freshdesk site is housed) and the API Key, which can be found on your Freshdesk profile page.

Next, go to Gmail and create a label, such as Freshdesk. Simply drag client emails from your Gmail Inbox to this new Freshdesk label, and they’ll be transformed into customer tickets immediately. Unlike email forwarding, the customer’s name and email address would be used to generate the tickets.

const GMAIL_LABEL_NAME = 'Freshdesk';
const FRESHDESK_API_KEY = 'Your API Key';
const FRESHDESK_SUBDOMAIN = 'Your subdomain';

const connectToFreshdesk_ = (postData) => {
  const API = `https://${FRESHDESK_SUBDOMAIN}.freshdesk.com/api/v2/tickets`;
  const response = UrlFetchApp.fetch(API, {
    method: 'POST',
    contentType: 'application/json',
    muteHttpExceptions: true,
    payload: JSON.stringify(postData),
    headers: {
      'Content-type': 'application/json',
      Authorization: 'Basic ' + Utilities.base64Encode(FRESHDESK_API_KEY + ':X'),
    },
  });
  if (!/^2/.test(String(response.getResponseCode()))) {
    console.error('Cannot create ticket', response.getContentText());
  }
};

const createSupportTicket_ = (message) => {
  const [email, ...names] = message.getFrom().replace(/[<>"]/g, '').split(' ').reverse();
  const postData = {
    subject: message.getSubject(),
    description: message.getBody(),
    name: names.reverse().join(' '),
    email,
    source: 3,
    status: 3,
    priority: 2,
  };
  connectToFreshdesk_('', postData);
};

const gmailToFreshdesk = () => {
  const label = GmailApp.getUserLabelByName(GMAIL_LABEL_NAME);
  label.getThreads(0, 20).forEach((thread) => {
    const [message] = thread.getMessages().reverse();
    createSupportTicket_(message);
    thread.removeLabel(label);
  });
};

Inside the Google Script editor, choose to Edit > Current Project Triggers > Add Triggers and build a new time-based trigger for the gmailToFreshdesk function that runs every 15 minutes.

Advertisements

The Google Script will now connect to your Gmail account every 15 minutes, look for new email messages under the chosen label, and convert them to Freskdesk support tickets. After the ticket is created, it will also delete the email message from the label.

There are no Gmail sending limits. You don’t have to worry about Gmail sending restrictions with Apps Script because the tickets are produced using API calls and you’re not sending (forwarding) email anyplace.

Advertisements

Leave a Comment