Use SMTP Integration but set body programatically

I have an SMTP integration set up with the from email filled in. I want to write a javascript query that loops through a table of email addresses and links and programmatically sets the SMTP query's to email and body then sends it for each record (like a mail merge).

I'm testing it with just this code and the to email already filled in in the SMTP query:

emailQuery.body = 'Test';
emailQuery.trigger();

But the email sends with an empty body. Is what I'm trying to do not possible with the SMTP integration?

I got the test one to work using Temporary State Variables.

However, now once I put it into a loop, the temporary state variables no longer work.

This is the code I have:

for (i = 0; i < emails.length; i++) {
  emailBody.setValue(baseEmailBody.value.replaceAll('[link]',links[i]));
  toEmail.setValue(emails[i]);
  emailQuery.trigger();
}

where "links" and "emails" are just test in line arrays with sample emails and links. However, when it runs, all the emails get sent to the last email in the array with the last link in the array.

What would be the proper way to do this?

I finally got it to work using this code:

if (currentIndex.value < TESTcustomerEmails.data.length) {
emailBody.setValue(baseEmailBody.value.replaceAll('[link]',TESTcustomerEmails.data[currentIndex.value]['link']).replaceAll('[fname]',TESTcustomerEmails.data[currentIndex.value]['firstname']));
          toEmail.setValue(TESTcustomerEmails.data[currentIndex.value]['email']);
          currentIndex.setValue(currentIndex.value + 1);
          emailQuery.trigger();
        }

Basically I had to create a temporary state variable to hold the current index, then in the SMTP query, set it to recursively call the above query after successful trigger.

Seems like there should be an easier way, but maybe not.

(TESTcustomerEmails is a Google Doc with test emails and links. toEmail, emailBody, baseEmailBody are temporary state variables.)

1 Like