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.labon port587with STARTTLS, and a mailbox accountvcfguide@vmw.labthat 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 databasedb01-vcfguideand the actionlatestVmFactsfrom recipe 5.4.
How to do it
Trust the mail server’s certificate
- Go to Library > Workflows, search with the tag
configuration, and run Import a certificate from URL. - Enter
mail01.vmw.lab:587as the URL, withouthttps://, and click Run. Check that the certificate shown in the run’s log is the one your mail server presents.
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
- Search with the tag
mailand run Configure mail. - On the Host tab, enter the SMTP host
mail01.vmw.laband the SMTP port587. - On the Credentials tab, enter the user
vcfguide@vmw.laband its password. - On the Email Content tab, enter the sender’s address
vcfguide@vmw.laband the sender’s nameVCF Orchestrator Guide. Click Run.
Run dialog of Configure mail, Host tab: SMTP host mail01.vmw.lab, SMTP port 587; the Credentials and Email Content tabs visible.
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
- Search with the tag
mailand run Send notification (TLSv1.2). On 9.1.1 the plain Send notification is marked (Deprecated). Enter the Destination addressmayank@vmw.lab, the subjectTest 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 onuseStartTlsin the Host group. - Click Run. The run completes and the message arrives from VCF Orchestrator Guide.
Run of Send notification (TLSv1.2) to mayank@vmw.lab, completed; the Variables tab with the empty SMTP inputs and useStartTls = true. Optionally, the received message beside it.
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
- In Library > Actions, create the action
sendHtmlMailincom.vcfguide.chapter5, with the inputstoAddress,subject, andhtml, all of typestring, and the Return typevoid:
// 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 587message.toAddress = toAddress;message.subject = subject;message.addMimePart(html, "text/html; charset=UTF-8");message.sendMessage();System.log("Mail '" + subject + "' sent to " + toAddress);- Create the action
mailVmFactsReport, with the inputstoAddressanddatabaseNameof typestring, and the Return typevoid. It turns the latest rows ofvm_factsinto an HTML list:
// Mails the ten most recent rows of vm_facts as an HTML list.function htmlText(value) { return String(value) .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """);}
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);- Run
mailVmFactsReportwithmayank@vmw.labanddb01-vcfguide. The log saysMail 'VM facts report (3 rows)' sent to mayank@vmw.lab, and the message shows a bulleted list.
The received VM facts report message in a mail client, showing the bulleted list of VMs; beside it, the Logs tab of the mailVmFactsReport run with the Mail … sent to line.
Checked on 9.1.1:
- A new
EmailMessagestarts with the Configure mail defaults already insmtpHost,smtpPort,fromAddress,fromNameandusername, 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
useStartTlsanduseSsl, but they’re write-only: reading one back givesundefined, even after you set it. Don’t test them in anif. Server.getRunningUser()returns the user’s name as a plain string, such assvc-vcfguide@vsphere.local. It has no.nameproperty;Server.getRunningUser().nameisundefined.
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.
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.
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:
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().
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
- 5.4 SQL databases, for
latestVmFactsand the escaping pattern. - 3.9 User interactions and approvals.
- 5.2 REST hosts and operations, for certificates and mail APIs.
- Broadcom TechDocs (VCF 9.0): Using the Mail plug-in and Define the default SMTP connection.
- Broadcom TechDocs (Aria Automation 8.13): Using the Mail plug-in sample workflows.
- Broadcom KB 332546: Cannot send mail, Could not convert socket to TLS.
- Broadcom KB 402979: mail still goes to the old SMTP server.
- Broadcom KB 325987: connections to TLS 1.0 and 1.1 services fail.
- CloudBlogger, the author’s blog: Using Google SMTP inside Automation and Orchestrator.
