Cordra can be configured to allow sending emails from Type Methods. In order to enable this, you will need to add the supporting jar libraries to your Cordra instance and then configure the emails you wish to send.
For this example, we’ll use the Simple Java Mail emailing library to send emails.
The jar files needed are included in the Cordra distribution, in the extensions/user-registration/lib
directory.
To install this library, put these jar files into a directory called lib
in your Cordra data directory. If the lib
directory doesn’t exist, create the lib
directory in the Cordra data directory.
You will need to restart the Cordra server once the Java Mail Client library is installed if your Cordra server is already running.
Create a file in the data
directory called emailServerConfig.json
with the following contents:
{
"serverAddress": "smtp.example.com",
"serverPort": 587,
"enableStartTls": true,
"trustHosts": "smtp.example.com",
"username": "your-smtp-username",
"password": "your-smtp-password"
}
Be sure to replace the example settings with the settings for your actual SMTP server. Finding these settings is outside of the scope of this document, but most email providers have some way of sending emails using SMTP. You can also use Amazon’s Simple Email Service for testing.
In the data
directory, create an emailTemplates
directory. In that directory, create files called
creation.txt
and creation.html
. These files should contain the text and html (respective) that should be sent
in the email.
As an example, we’ll modify the beforeSchemaValidation
method on the User object to send an email whenever a
new User object is created. Replace the default User schema javascript with the following:
1exports.beforeSchemaValidation = beforeSchemaValidation;
2
3var emailConfig = {
4 "toAddress": "test@example.com",
5 "fromAddress": "admin@example.com",
6 "subject": "testing javascript email"
7};
8
9function beforeSchemaValidation(obj, context) {
10 if (context.isNew) {
11 sendEmail(emailConfig.toAddress);
12 }
13 return obj;
14}
15
16function sendEmail(toAddress) {
17 // Java types
18 var EmailBuilder = Java.type("org.simplejavamail.email.EmailBuilder");
19 var MailerBuilder = Java.type("org.simplejavamail.mailer.MailerBuilder");
20 var TransportStrategy = Java.type("org.simplejavamail.api.mailer.config.TransportStrategy");
21
22 // Build email
23 var serverConfig = loadServerConfig();
24 var textBody = readFileToString("emailTemplates/creation.txt");
25 var htmlBody = readFileToString("emailTemplates/creation.html");
26 var email = EmailBuilder.startingBlank()
27 .to(toAddress)
28 .from(emailConfig.fromAddress)
29 .withSubject(emailConfig.subject)
30 .withHTMLText(htmlBody)
31 .withPlainText(textBody)
32 .buildEmail();
33
34 var mailerBuilder = MailerBuilder
35 .withSMTPServer(serverConfig.serverAddress, serverConfig.serverPort, serverConfig.username, serverConfig.password)
36 .withSessionTimeout(10 * 1000);
37 if (serverConfig.enableStartTls) {
38 mailerBuilder = mailerBuilder.withTransportStrategy(TransportStrategy.SMTP_TLS);
39 } else if (serverConfig.enableSmtps) {
40 mailerBuilder = mailerBuilder.withTransportStrategy(TransportStrategy.SMTPS);
41 }
42 var mailer = mailerBuilder.buildMailer();
43 mailer.sendMail(email);
44}
45
46function loadServerConfig() {
47 var json = readFileToString("emailServerConfig.json");
48 return JSON.parse(json);
49}
50
51function readFileToString(filename) {
52 var dataDir = java.lang.System.getProperty("cordra.data");
53 var filePath = java.nio.file.Paths.get(dataDir).resolve(filename);
54 return new java.lang.String(java.nio.file.Files.readAllBytes(filePath));
55}
Next, try creating a new User in Cordra. You should receive an email containing the message specified in the JavaScript configuration.
The email templates are loaded from the files we created before (lines 24-25). Be sure to modify emailConfig
with
real email addresses to use for sending the email. Email server config is loaded from a local file in the
loadServerConfig
function (lines 47-50), so the secrets are never network accessible through Cordra.
We are using context.isNew
to make sure we only create a token on new object creation (line 10). The Java classes
from Simple Mail Client are loaded into the JavaScript (lines 18-20) and then used using the Simple Java Mail API (lines
26-44).