Back to Blog

Sending Custom Email

Cloud code actions provides functionality to send e-mails.

Example use cases:

You can send an email using the toolbelt.email() method.

Sending HTML

We support sending (limited) HTML formatted email with custom emails.

async payload => {
  toolbelt.email({
    to: '[email protected]',
    message: '<div><h1>Example Title</h1><p>This is an HTML example</p></div>'
  })
}

Sending data with e-mails

Cloud code supports JavaScript template tags that allows you to send information, like parameters inside the payload.

Sending an email with the name of a user

async payload => {
  toolbelt.email({
    to: '[email protected]',
    message: `Some data extracted: ${payload.user.name}`
  })
}

Sending an email with a param

async payload => {
  toolbelt.email({
    to: '[email protected]',
    message: `Some data extracted: ${payload.params.city[0].value}`
  })
}

Sending all parameters

Combining a custom formatted email with dynamic data

async payload => {

  const {
    params
  } = payload

  // Generate HTML for the params
  let paramsHtml = ''

  const paramNames = Object.keys(params)
  
  for (let i = 0; i < paramNames.length; i++) {

    const paramName = paramNames[i]

    // Get the param
    const param = params[paramName]

    // Add the param name to the HTML
    paramsHtml += `
      <p>
        <strong>${paramName}:</strong>
        <div>${param.map(values => values.value).join(', ')}</div>
      </p>`
  }

  toolbelt.email({
    to: '[email protected]',
    subject: `Collected ${paramNames.length} params`,
    message: `
    <h2>We found the following data!</h2>
    <p>This is just a small example</p>
    <p>${paramsHtml}</p>`
  })
}
Get StartedContact us