Skip to content

Formbird Email Functions

Updated Galland 2023-08-22

Aside from the existing email templates in formbird there are now additional functionalities to send and format emails from the system. These functions are readily available through the rulesets and simply require #include "Email Functions JS" to be used. Please copy latest Email Functions JS ruleset includes from comp-dev-ng.

This addional functionality uses utility emailHelper to allow both html formatted emails and attachments to be easily added to emails. The functionality is separate from that described in the documentation Creating Emails in Rulesets. The latest version can be found in comp-dev-ng.

The below approach is much more simple, requires no templates and can be called from a server side ruleset. Note SendEmail and sendFormbirdEmail both require formbird version 4.1.67+.

Email Functions - sendEmail

await ft3.emailFunctions.sendEmail( ntf, to, cc, bcc, subject, content, formbirdAttachments )

This is a promised function that is included with the "Email Functions JS" ruleset includes. It is a simplified version of ft3.emailHelper.sendHtmlMail that also allows for files stored on the formbird server to be attached easily. This function can be called from either synchronous or asynchronous functions from the server side. It is much more robust thatn sendHtmlMail and is the recommended approach.

Inputs are defined as....

to: array of email strings
cc: array of email strings, if no cc emails, must be empty array []
bcc: array of email strings, if no bcc emails, must be empty array []
subject: Subject line of the email (string)
text: Text for the email as defined in options (string)
content: html string containing whatever content should be included in the email
formbirdAttachments: (optional) an array of objects in format { fileNo: '5d2bb860...' } with fileNo corresponding to uuid of files stored on the formbird server. eg. normally .../api/getFile/5d2bb860.../fileName.pdf

Example:

const from=null, to=['test@formbird.com'], cc=[], bcc=[], subject='Test Email';
const content='This is some text for inside email. <br> <div> It can also be html formatted</div>';
const formbirdAttachments = [ { fileNo: '97dd7ae0-372b-11ee-80f0-d78d7ec5c3a3'} ];

await ft3.emailFunctions.sendEmail(ntf, to, cc, bcc, subject, content, formbirdAttachments);
// OR
await ft3.emailFunctions.sendEmail(ntf, to, cc, bcc, subject, content);

Resulting email

Send Email Example

Note this function uses the default from email specified in the serverConfiguration of the environment's configuration file. eg.

"email": {
      "host": "...",
      "port": "...",
      "sender": "Formbird Alert <alert@formbird.com>",
      "timeout": 15000
  },

Email Functions - sendFormbirdEmail

await ft3.emailFunctions.sendFormbirdEmail (ntf, to, cc, bcc, subject, contentObj, formbirdAttachments) 

This is an altered version of sendEmail which uses a basic email template which can be seen below. Content object allows certain parts of the html email to be configurable. It follows the same interface as sendEmail however uses predefined content fields...

contentObj: Object containing any of the following 3 fields.
.title: text string for title within form
.footerText: footer text string fow below main block
.bodyHtml: html string for inside the main content block

Example

const from=null, to=['test@formbird.com'], cc=[], bcc=[], subject='Test Email';
const formbirdAttachments = [ { fileNo: '97dd7ae0-372b-11ee-80f0-d78d7ec5c3a3'} ];
const contentObj = {};
  contentObj.title = "Title Example";
  contentObj.footerText = "The is an example footer text";
  contentObj.bodyHtml = "<p><b>This is some text in the main bit<br><br> </b> These are some words.</p>";

ft3.emailFunctions.sendFormbirdEmail(ntf, to, cc, bcc, subject, contentObj, formbirdAttachments);
// OR
ft3.emailFunctions.sendFormbirdEmail(ntf, to, cc, bcc, subject, contentObj);

Resulting email

Send Formbird Email Example

Email Helper HTML Emails

ft3.emailHelper.sendHtmlMail( from, to, cc, bcc, subject, text, htmlContent, attachments, callbackFunction ) Requires formbird 4.1.67+.

The email helper allows html content to be sent in an email. This function does not require "Email Functions JS" to be included and is simply available through ft3 or ntf.scope. It is not a promised function and putting together attachments can be complicated for those who havent had experience with base64 file conversion.

from: Defaults to environment configuration file. Can be set to null
to: array of email strings
cc: array of email strings, if no cc emails, must be empty array []
bcc: array of email strings, if no bcc emails, must be empty array []
subject: Subject line of the email (string)
text: Text for the email as defined in options (string)
htmlContent: html string containing whatever content should be included in the email
attachments: array of attachment objects. See example below...

Example
const from=null, to=['example@mail.com'], cc=[], bcc=[], subject='Test Email';
const htmlContent='This is some text for inside email. <br> <div> It can also be html formatted</div>';
const attachments  = [{ content: `SGVsbG8sIFdvcmxkIQ==`,
                          filename: "sample.txt",
                          type: "text/plain",
                          disposition: "attachment" }];
ft3.emailHelper.sendHtmlMail(
    from,
    to, cc, bcc,
    subject,
    text,
    htmlContent,
    attachments,
    function(error, data) {
        if(error) {
        ntf.logger.info('Sent Email failed: ' + error.message);
        } else {
        ntf.logger.info("Email sent ok.");
        }
    }
); 

Related Mantis': #9421, #18123, #8808