Skip to content

Sending and reading mail

Mail is still the simplest way for a workflow to tell a person something: a report is ready, a request needs approval, a run failed at three in the morning. The Mail plug-in sends mail over SMTP. With the Net plug-in beside it, it can also read a mailbox over POP3 or IMAP.

In this recipe you set the server-wide mail defaults once, send a test message with a built-in workflow, and then write an action that sends an HTML report built from the rows recipe 5.4 stored in PostgreSQL.

Getting ready

  • An SMTP server that Orchestrator can reach. This recipe uses mail01.vmw.lab on port 587 with STARTTLS, and a mailbox account vcfguide@vmw.lab that is allowed to send.
  • A mailbox to send to. The examples use mayank@vmw.lab.
  • A login with administrator rights, the action module com.vcfguide.chapter5, and, for the report, the database db01-vcfguide and the action latestVmFacts from recipe 5.4.

How to do it

Trust the mail server’s certificate

  1. Go to Library > Workflows, search with the tag configuration, and run Import a certificate from URL.
  2. Enter mail01.vmw.lab:587 as the URL, without https://, and click Run. Check that the certificate shown in the run’s log is the one your mail server presents.
Needs a lab check. Check that Import a certificate from URL on 9.1.1 can read the certificate of an SMTP server that offers it only after STARTTLS (port 587). Broadcom KB 332546 imports smtp.yourcompany.com:587 this way on 8.x, through Control Center. If the workflow fails, import the CA certificate as a PEM file with Import a trusted certificate from a file instead, and record which one works.

Set the default SMTP connection

  1. Search with the tag mail and run Configure mail.
  2. On the Host tab, enter the SMTP host mail01.vmw.lab and the SMTP port 587.
  3. On the Credentials tab, enter the user vcfguide@vmw.lab and its password.
  4. On the Email Content tab, enter the sender’s address vcfguide@vmw.lab and the sender’s name VCF Orchestrator Guide. Click Run.
Figure 5.13. The default mail settings, set once with the Configure mail workflow.

On 9.1.1, Configure mail has exactly those six inputs, in the groups Host (SMTP host and port, both required), Credentials (user and password, optional) and Email Content (sender’s address and name, both required). It has no SSL or STARTTLS setting: that’s chosen per message. Its script is one line, ConnectionManager.save(smtpHost, smtpPort, username, password, fromAddress, fromName).

Send a test message

  1. Search with the tag mail and run Send notification (TLSv1.2). On 9.1.1 the plain Send notification is marked (Deprecated). Enter the Destination address mayank@vmw.lab, the subject Test from vco01, and a line of text as the content. Leave the SMTP fields empty, so the defaults from step 6 are used. If your server needs STARTTLS, switch on useStartTls in the Host group.
  2. Click Run. The run completes and the message arrives from VCF Orchestrator Guide.
Figure 5.14. A test message sent with the Send notification workflow.

On 9.1.1 the library’s Mail folder has three senders:

Workflow What it does
Send notification (TLSv1.2) One recipient. Its script starts with new EmailMessage("TLSv1.2") and offers useStartTls, but no useSsl.
Send notification to mailing list Adds CC and BCC lists.
Send notification (Deprecated) The old workflow, with both useSsl and useStartTls. Don’t use it for new work.

Each SMTP input left empty falls back to the Configure mail defaults.

Send an HTML report from an action

  1. In Library > Actions, create the action sendHtmlMail in com.vcfguide.chapter5, with the inputs toAddress, subject, and html, all of type string, and the Return type void:
com.vcfguide.chapter5/sendHtmlMail
// Sends an HTML message with the server's default SMTP settings.
// toAddress can hold several addresses separated by commas.
if (!toAddress) {
throw "sendHtmlMail: no recipient";
}
var message = new EmailMessage("TLSv1.2"); // as Broadcom's Send notification (TLSv1.2)
message.useStartTls = true; // the server in this book needs STARTTLS on 587
message.toAddress = toAddress;
message.subject = subject;
message.addMimePart(html, "text/html; charset=UTF-8");
message.sendMessage();
System.log("Mail '" + subject + "' sent to " + toAddress);
  1. Create the action mailVmFactsReport, with the inputs toAddress and databaseName of type string, and the Return type void. It turns the latest rows of vm_facts into an HTML list:
com.vcfguide.chapter5/mailVmFactsReport
// Mails the ten most recent rows of vm_facts as an HTML list.
function htmlText(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
var chapter5 = System.getModule("com.vcfguide.chapter5");
var lines = chapter5.latestVmFacts(databaseName, 10);
var html = "<p>The latest VM facts recorded in " + htmlText(databaseName) + ":</p><ul>";
for (var i = 0; i < lines.length; i++) {
html += "<li>" + htmlText(lines[i]) + "</li>";
}
html += "</ul><p>Sent by " + htmlText(Server.getRunningUser()) + " from Orchestrator.</p>";
chapter5.sendHtmlMail(toAddress, "VM facts report (" + lines.length + " rows)", html);
  1. Run mailVmFactsReport with mayank@vmw.lab and db01-vcfguide. The log says Mail 'VM facts report (3 rows)' sent to mayank@vmw.lab, and the message shows a bulleted list.
Figure 5.15. An HTML report sent from an action.

Checked on 9.1.1:

  • A new EmailMessage starts with the Configure mail defaults already in smtpHost, smtpPort, fromAddress, fromName and username, so a script only sets what’s different.
  • new EmailMessage("TLSv1.2") works, and is what the current library workflow uses.
  • The API Explorer lists useStartTls and useSsl, but they’re write-only: reading one back gives undefined, even after you set it. Don’t test them in an if.
  • Server.getRunningUser() returns the user’s name as a plain string, such as svc-vcfguide@vsphere.local. It has no .name property; Server.getRunningUser().name is undefined.
Needs a lab check. With a STARTTLS server on 9.1.1, send a message with new EmailMessage(“TLSv1.2”) and useStartTls = true, and record the result.

How it works

Configure mail stores the defaults that every mail workflow and every EmailMessage object starts from: host, port, user, password, and sender. A script only sets what’s different for one message, usually the recipients, the subject, and the content. That keeps the mail account’s password out of workflows, like the database password in recipe 5.4.

EmailMessage is one message. toAddress, ccAddress, and bccAddress each take one address or several separated by commas. addMimePart(content, type) adds a part to the body: plain text with text/plain, HTML with text/html, or a file as a MimeAttachment. sendMessage() connects to the server, sends, and disconnects. If the server refuses the message, it throws, and the error text from the server is in the run’s log.

Why htmlText? The report puts VM names, which come from vCenter, into HTML. A name with < or & in it would break the layout, and a crafted name could add a link or an image to the message. Escaping every value, like sqlText in recipe 5.4, makes the content plain text inside the HTML.

Certificates. With STARTTLS, the connection starts in plain text and switches to TLS after the server offers it. Orchestrator then checks the server’s certificate against its trust store, and an untrusted certificate fails with Could not convert socket to TLS and unable to find valid certification path to requested target (KB 332546). Importing the certificate in step 2 prevents that. Orchestrator only speaks TLS 1.2 and later, so an old server that offers only TLS 1.0 or 1.1 fails too.

Back in vRO 5.5–7Set the default SMTP host, port, account, and sender on the Mail tab of the configuration interface, and import the mail server's certificate there.
In Orchestrator 9.xRun the Configure mail workflow in the Orchestrator Client, and import the certificate with Import a certificate from URL.

There's more

Port 465. Some servers want TLS from the first byte, on port 465, instead of STARTTLS. Set useSsl = true and smtpPort = 465 in the script, and leave useStartTls off. On 9.1.1 only the deprecated Send notification still has a useSsl input, so do this from a script.

Don’t send through a load balancer. Broadcom’s documentation warns that an SMTP load balancer can cause the error SMTP_HOST_UNREACHABLE. Point Configure mail at a mail server, or at a relay that answers on one address.

When the old server comes back. If you change mail servers and messages still go to the old one, look in Assets > Resources for a resource element that still holds the old server’s name. KB 402979 describes this: Configure mail reports success, but the stale resource wins. Delete it and run Configure mail again.

Needs a lab check. Check whether a 9.1.1 appliance has an Assets > Resources element for the mail settings after Configure mail runs, and what it is called. KB 402979 was written for 8.x. On 9.1.1, Configure mail saves through ConnectionManager.save.

Microsoft 365 and Gmail. Both are moving away from plain user-and-password logins for SMTP. The Mail plug-in authenticates only with a user name and password; Broadcom documents no OAuth 2.0 support. For these services, send through an internal relay that accepts mail from vco01.vmw.lab, or use the provider’s REST API with the HTTP-REST plug-in from recipe 5.2.

Reading a mailbox. The built-in workflow Retrieve messages reads a mailbox over POP3, and Retrieve messages (via MailClient) reads one over IMAP or POP3 through the MailClient scripting object. Neither deletes what it reads. Use them for a simple mail-driven trigger, such as a shared mailbox that receives requests. For anything busier, a message queue (recipe 5.7) is more reliable than polling a mailbox.

On 9.1.1, Retrieve messages (via MailClient) takes server, port, protocol (imap or pop3), useSSL, accountName and password, and fills in the port from the protocol and useSSL. Retrieve messages via MailClient (TLSv1.2) is the same with TLS 1.2. Its script shows the MailClient calls you need, shortened here:

Reading a mailbox with MailClient, as the library workflow does
var client = new MailClient();
client.setProtocol("imap");
client.enableSSL();
client.connect(server, port, accountName, password);
try {
client.openFolder("Inbox");
var messages = client.getMessages() || [];
for (var i = 0; i < messages.length; i++) {
System.log(messages[i].from + ": " + messages[i].subject);
}
} finally {
client.closeFolder();
client.close();
}

Each message also has to, getHeaders(), getContent(), and, for a message with attachments, isContentMultiPart() and getMultiPartContent().

Needs a lab check. Run the script above against an IMAP mailbox on 9.1.1 and confirm that it lists the messages without removing them.

Mail from a user interaction. When a workflow waits for a user interaction (recipe 3.9), send the approver a message with a link to the run. The approver still answers in the Orchestrator Client or in VCF Automation; the mail is only the nudge.

See also