Attachmate Worldwide  |   Contact Us  |   NetIQ.com
Home » Support » Solution Library

Technical Notes

Verastream Scripting Language SMTP E-Mail Example
Technical Note 10202
Last Reviewed 30-Dec-2004
Applies To
Verastream Integration Broker version 9.0 or higher
Summary

This technical note provides a Verastream Scripting Language (VSL) code sample for using a SERVICE object to send e-mail messages via SMTP.

Background

The sample code below demonstrates direct use of an SMTP SERVICE object.

Note: This is an alternative to using the smtp_send method on the TransferData component in the Verastream Component Library repository, as described in Technical Note 10224.

A SERVICE object takes the following form:

SERVICE <service-name> TYPE=<str> ADDRESS=<addr>
MESSAGES
{
 MESSAGE <message-name1> [ FUNCTION=<function-name1> ] [ CONTEXT ]
 FIELDS
    {
      <field> [ BUFNAME=<name> ] [ FORMAT=<format> ]
      <field> [ BUFNAME=<name> ] [ FORMAT=<format> ]
      ...
      <field> REPLY [ BUFNAME=<name> ] [ FORMAT=<format> ]
      <field> [ BUFNAME=<name> ] [ FORMAT=<format> ] REPLY
      ...
    }
 MESSAGE <message-name2> [ CONTEXT ] [ FUNCTION=<function-name2> ]
 FIELDS
    {
      ...
    }

Note: The SERVICE objects are not supported by the Process Integrator graphical user interface, so they are visible only in your *.lgc file using the Logic Editor (or preferred text editor).

Sending E-Mail Using Verastream Scripting Language

The following example sends an e-mail message using Verastream Scripting Language. Edit the DoIt.Example script to set your desired SMTP server, From, To, Cc, Subject, body text, and attachment(s). This example is heavily commented for educational purposes.

#-----------------------------------------------------------
# Copyright (c) 2004 WRQ, Inc.
#
# Last Revised: 16 Sep 2003, 21 Dec 2004
# Description : Sample use of SMTP SERVICE to send e-mail 
# message with or without attachments.
#
#   This sample does *not* use the demo VCP components
#   TransferData (trdata) or SmtpServer (smtpserv).
#
# SMTP is the Simple Mail Transfer Protocol specified in 
# IETF RFC 821.
#
##------------------------------------------------------------
FUNCTION Example DoIt
{
    # Host name or IP address of your SMTP email server
    SET smtp_server 'smtphost.mycompany.com'
    
    # Email address of sender
    SET mail_from_address 'me@mycompany.com'
    
    # Email address of recipient
    # Multiple recipients can be comma-delimited
    SET mail_to_address 'you@somewhere.com'
    
    # Email address of carbon copy recipient (optional)
    # Multiple recipients can be comma-delimited
    # Note that blind carbon copy (Bcc) is not supported
    SET mail_cc_address ''
    
    # Email message subject line (optional)
    SET mail_subject    'test message - please ignore'
    
    # Email message body text    
    SET messageTextData \
'This email message sent by Verastream Integration Broker.'
    
    # The following sample block adds a binary file 
    # attachment
    # This can be repeated for multiple attachments, or 
    # omitted for none
    {
    # file name of attachment
    READ attachmentBody \
'C:\Program Files\Verastream\help\images\H_Banner_NoFrm.gif'
    
    # name of attachment to use in the email
    SET attachmentName 'H_Banner_NoFrm.gif'
    
    # attachmentType is either 'asc' for ASCII text or 'bin'
    # for binary
    SET attachmentType 'bin'
    
    # add to the COMPONENT_DATA
    ADD messageAttachments
    }
    
    # Generate and send the e-mail message
    SendEmail EmailSender
}
#------------------------------------------------------------
FUNCTION SendEmail EmailSender
{
    LOCAL FIELD enclosureHeader    string
    
    # Create the header of the email (from, to, cc, subject)
    # MIME functions are documented 
    # in help/vsl/vs_gr_6_3.html
    SET mail_header    ''
    SET mail_header    mimeAddHeaderValue(mail_header, \
                    'From', strip(mail_from_address))
    SET mail_header    mimeAddHeaderValue(mail_header, \
                    'To', strip(mail_to_address))
    continue {
    HASVALUE mail_cc_address
    TEST BASE    mail_cc_address != '' \
                && mail_cc_address != NULL
    SET mail_header    mimeAddHeaderValue(mail_header, \
                    'CC', strip(mail_cc_address))
    }
    continue {
    HASVALUE mail_subject
    TEST BASE    mail_subject != '' && mail_subject != NULL
    SET mail_header    mimeAddHeaderValue(mail_header, \
                    'Subject', strip(mail_subject))
    }
    
    # Initialize the email body field
    INIT mail_content
    
    # Determine the quantity of attachments
    countAttachments EmailSenderUtil
    
    # If there are no attachments, then simply set the 
    # message data (otherwise the message data has to be 
    # added as an attachment)
    {
    TEST BASE totalAttachments == 0
    # Set end of header with blank line as per IETF RFC 822
    SET mail_header    mail_header+"\n"
    SET mail_content messageTextData
    ALT
    SET totalAttachments totalAttachments+1
    SET attachmentType 'asc'
    SET attachmentName ''
    SET attachmentBody messageTextData
    INIT totalAddedAttachments
    addAttachment EmailSenderUtil
    # Add the rest of the attachments to mail_content
    addAttachments EmailSenderUtil
    }

    # For debugging purposes, the entire message is logged
    # This can be commented out if desired
    LOG BASE mail_header + mail_content
    
    # Specify no message encryption
    # (Encrypted email can only be read with VIB POP3 
    # email reader)
    SET encrypt_flag    'no'
    
    # ADDRESS property on the SMTP SERVICE object:
    # Set the email server host name using FIELD smtp_server
    ADDRESS smtpService    smtp_server
    
    # SEND function on the SMTP SERVICE object:
    # Send the message using FIELDs mail_header, mail_content,
    # and encrypt_flag
    SEND smtpService    mailMessage
    
ALT
    # Failures are typically caused by:
    # - The specified SMTP server host name cannot be 
    # resolved by DNS
    # - The specified SMTP server service is currently down
    # or unreachable
    # - The SMTP server requires authentication (not supported)
    # - Invalid syntax in the From, To, or CC email address
    THROW BASE        EXCEPTION='SendFailed' \
                EXCEPTIONSTRING='Failed to send data to \
                server ''' + smtp_server + ''' (smtp)'
}
#-----------------------------------------------------------
# Add attachment to mail_content
FUNCTION addAttachment EmailSenderUtil
{
    LOCAL FIELD attachmentHeader string
    LOCAL FIELD attachmentBody2    string ADJUST=not
    
    # Initialize attachmentHeader to ''
    INIT attachmentHeader
    
    # If this is the first attachment, initialize the header
    {
    TEST BASE totalAddedAttachments == 0
    SET mail_header    mimeInitEnclosures(mail_header)
    SET mail_header mail_header+"\n"
    ALT
    void
    }
    
    # Handle text vs. binary
    # MIME functions documented in help/vsl/vs_gr_6_3.html
    {
    TEST BASE    attachmentType == 'asc'
    {
        {
        HASVALUE attachmentName
        TEST BASE        attachmentName != '' \
                    && attachmentName != NULL
        SET attachmentHeader       \
                mimeAddHeaderValue(attachmentHeader, \
               'Content-Type', 'text/plain; \
                name="'+strip(attachmentName)+'"')
        ALT
        # attachmentName is empty
        SET attachmentHeader        \
                mimeAddHeaderValue(attachmentHeader, \
                'Content-Type', 'text/plain')
        }
        SET attachmentHeader       \
                mimeAddHeaderValue(attachmentHeader, \
               'Content-Transfer-Encoding', '8bit')
        SET attachmentBody2 attachmentBody
    }
    ALT
    SET attachmentHeader        \
                mimeAddHeaderValue(attachmentHeader, \
                'Content-Type', 'application/octet-stream; \
                name="'+strip(attachmentName)+'"')
    SET attachmentHeader        \
                mimeAddHeaderValue(attachmentHeader, \
                'Content-Transfer-Encoding', 'base64')
    # Encode the binary data as base 64 text
    SET attachmentBody2 mimeB64Encode(attachmentBody)
    }
    
    # Handle middle vs. last attachment
    # MIME functions documented in help/vsl/vs_gr_6_3.html
    {
    TEST BASE    totalAddedAttachments+1 < totalAttachments
    SET mail_content    mimeAddEnclosure(mail_header, \
                    mail_content, attachmentHeader, \
                    attachmentBody2)
    ALT
    SET mail_content    mimeAddLastEnclosure(mail_header, \
                    mail_content, attachmentHeader, \
                    attachmentBody2)
    }

    # Increment counter
    SET totalAddedAttachments totalAddedAttachments+1

    # Log a message
    LOG BASE 'Added attachment \
                '+string(totalAddedAttachments)+' of '+ \
               string(totalAttachments)+ \
               ' ('+strip(attachmentName)+')'
}

#-----------------------------------------------------------
# Iterate through messageAttachments; call addAttachment 
# for each

FUNCTION addAttachments EmailSenderUtil
{
    NOCURR messageAttachments
    repeat {
    NEXT messageAttachments
    {
        TEST BASE totalAddedAttachments < totalAttachments
        addAttachment EmailSenderUtil
    }
    }    
}

#-----------------------------------------------------------
# Iterate through messageAttachments; count result is 
# totalAttachments

FUNCTION countAttachments EmailSenderUtil
{
    INIT totalAttachments
    NOCURR messageAttachments
    repeat {
    NEXT messageAttachments
    SET totalAttachments totalAttachments+1
    LOG BASE 'Attachment '+string(totalAttachments)+ \
            ' ('+strip(attachmentName)+')'
    }
}
#-----------------------------------------------------------
FUNCTION START START
{
    # *** Just run the example! ***
    Example DoIt
}

#-----------------------------------------------------------
FUNCTION load modules
{
    LOADMODULE SUBAP APPLICATION='runutil' TYPE='fle'
}
#-----------------------------------------------------------
FIELD attachmentBody    byte ADJUST=not STATIC
FIELD attachmentName    string(256) STATIC
FIELD attachmentType    string(3) EXPR=asc|bin STATIC
FIELD encrypt_flag      string(3) EXPR='yes|no' STATIC
FIELD mail_body         string STATIC
FIELD mail_cc_address   string STATIC
FIELD mail_content      string STATIC
FIELD mail_from_address string STATIC

FIELD mail_header       string STATIC
FIELD mail_subject      string STATIC
FIELD mail_to_address   string STATIC
FIELD messageTextData   string STATIC
FIELD smtp_server       string STATIC
FIELD totalAddedAttachments int(3) RANGE=1..999 INIT=0 STATIC
FIELD totalAttachments int(3) RANGE=0..999 INIT=0 STATIC
COMPONENT_DATA messageAttachments
FIELDS
{
    attachmentName
    attachmentType
    attachmentBody
}
#-----------------------------------------------------------
# The VIB UIE supports SMTP for SERVICE objects of type SMTP.
#
# See also Verastream legacy documentation:
# Interconnectivity Manual, section 2.2, page 2-10
# (Adobe Acrobat file cditcman.pdf, version 7.2, page 108)
#
# Note that SERVICE objects are not supported by the 
# VIB Process Integrator graphical user interface.

SERVICE smtpService    TYPE=SMTP
MESSAGES
{
    MESSAGE mailMessage
    FIELDS
    {
    mail_header
    mail_content
    encrypt_flag
    }
}
#-----------------------------------------------------------

Related Technical Notes
10224 Sending E-mail Using TransferData Demo Component

Did this technical note answer your question?

Yes    No    Somewhat     Not sure yet

Additional comments about this tech note:

Need further help? For technical support, please contact Support.