DEV Community

Explorer
Explorer

Posted on

📧 Sending Emails Programmatically in Joget via BeanShell

Overview

While Joget provides a built-in Email Tool, there are scenarios where you need to send notifications dynamically within a complex script. Whether you are looping through a list of unique recipients or triggering alerts based on conditional logic, using BeanShell to invoke the Email Tool plugin gives you the flexibility to send emails exactly when and how you need them.

How It Works

Instead of reinventing the SMTP connection, this script leverages Joget's existing Email Tool plugin. It programmatically retrieves the plugin, injects the necessary server configurations (SMTP host, port, etc.) from the App Definition, and executes the send command. This ensures your script uses the global email settings already configured in your Joget platform.

Where to Use in Joget

  • Workflow Builder: Within a BeanShell Tool to send custom alerts during process transitions.
  • Form Builder: In a Form Post-Processor to notify stakeholders immediately upon data submission.
  • Userview: Inside a BeanShell Menu to trigger manual email blasts.

Full Code

⚙️ Programmatic Email Script
This script identifies the org.joget.apps.app.lib.EmailTool and executes it using your system's default properties.

import java.util.Map;
import java.util.HashMap;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppPluginUtil;
import org.joget.apps.app.service.AppUtil;
import org.joget.plugin.base.ApplicationPlugin;
import org.joget.plugin.base.Plugin;
import org.joget.plugin.base.PluginManager;
import org.joget.plugin.property.model.PropertyEditable;
import org.joget.commons.util.LogUtil;

public Object sendEmail() {
    LogUtil.info("EmailScript", "Email transmission started...");

    // 1. Setup Environment
    AppDefinition appDef = AppUtil.getCurrentAppDefinition();
    PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");

    // 2. Define Recipients (Add your logic here to pull from a form or database)
    String[] emails = new String[]{"recipient@example.com"}; 

    // 3. Initialize the Email Tool Plugin
    Plugin plugin = pluginManager.getPlugin("org.joget.apps.app.lib.EmailTool");
    Map propertiesMap = AppPluginUtil.getDefaultProperties(plugin, null, appDef, null);
    propertiesMap.put("pluginManager", pluginManager);
    propertiesMap.put("appDef", appDef);

    ApplicationPlugin emailTool = (ApplicationPlugin) plugin;

    // 4. Loop through recipients and send
    for (String email : emails) {
        propertiesMap.put("toSpecific", email);
        propertiesMap.put("subject", "Automated Notification: Action Required");
        propertiesMap.put("message", "This is a programmatically generated email from Joget.");

        // Set properties and execute
        ((PropertyEditable) emailTool).setProperties(propertiesMap);
        emailTool.execute(propertiesMap);

        LogUtil.info("EmailScript", "Successfully sent to: " + email);
    }

    LogUtil.info("EmailScript", "Email transmission completed.");
    return null;
}

// Execute the method
return sendEmail();

Enter fullscreen mode Exit fullscreen mode

Example Use Cases

  • 💡 Dynamic Batching: Sending individual emails to a list of users selected in a Checkbox or Multi-Select field.
  • 💡 Custom Attachments: Using script logic to determine which files from a File Upload field should be attached.
  • 💡 API-Triggered Alerts: Sending an email notification after a successful (or failed) external API call.

Customization Tips

  • ⚙️ Recipients: You can replace the emails array with a query that fetches user emails from a Joget User Directory or a specific DataList.
  • ⚙️ HTML Content: Ensure your message body uses valid HTML if you want to include links, tables, or professional branding.
  • ⚙️ Logging: Keep the LogUtil calls during testing; they are your best friend for verifying that the loop is executing correctly in catalina.out.

Key Benefits

  • Zero Redundancy: Reuses the SMTP settings already defined in your App, avoiding hardcoded passwords.
  • Granular Control: Send emails based on data values that a standard Workflow Email Tool cannot easily parse.
  • Scalability: Easily handle one recipient or one hundred within the same logic block.

Final Thoughts

Invoking plugins via BeanShell is a "power user" move that bridges the gap between standard configuration and custom coding. It keeps your app clean by utilizing Joget's core engine while giving you the surgical precision needed for specialized notifications. Always test with a single email address before running the script on a live production dataset!


Top comments (0)