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.
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
emailsarray 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
LogUtilcalls during testing; they are your best friend for verifying that the loop is executing correctly incatalina.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!
Source Code
import java.util.Map;
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.property.model.PropertyEditable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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("", "Testing Started");
// Manually retrieve the current application definition
AppDefinition appDef = AppUtil.getCurrentAppDefinition();
// Get the PluginManager from the AppUtil
PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
String[] emails = new String[]{"<email@example.com>"}; // Replace with actual recipient emails
// Get the Email Tool plugin
Plugin plugin = pluginManager.getPlugin("org.joget.apps.app.lib.EmailTool");
// Get the default properties for the Email Tool
Map propertiesMap = AppPluginUtil.getDefaultProperties(plugin, null, appDef, null);
propertiesMap.put("pluginManager", pluginManager);
propertiesMap.put("appDef", appDef);
// Casting the plugin to ApplicationPlugin
ApplicationPlugin emailTool = (ApplicationPlugin) plugin;
// Send email to multiple recipients
for (String email : emails) {
propertiesMap.put("toSpecific", email);
propertiesMap.put("subject", "Test Email Subject");
propertiesMap.put("message", "This is a test email content.");
// Set properties and execute the email tool
((PropertyEditable) emailTool).setProperties(propertiesMap);
emailTool.execute(propertiesMap);
}
LogUtil.info("", "Testing Ended");
return null;
}
// Call the sendEmail method
return sendEmail();
Top comments (0)