HTML based emails from SAP DataServices

In this post I describe a quick way to send HTML based emails from SAP Data Services. This practice is handy to send alerts or information from your jobs.  The solution below uses Blat (a free SMTP tool), however I also detail the first steps of using your own SMTP code.

  1. Install Blat
    1. Download
    2. Configure Blat
      C:\Blat\>blat.exe -install <SMTP server address> <default from email address>
    3. Make sure the Blat folder is setup in the windows Path
  2. Create a new custom function in SAP Data Services
    1. Create custom function
    2. Create parameters
    3. Create HTML Code Variable
    4. Create function code

      #Function receives arguments:
      #$pMsgID - the message ID (handy to uniquely identify generated emails)
      #$pMsgSubject - the message subject
      #$pMsgContent - the message text content
      #$pInputPath - the path to use (for HTML code & image)
      #$pImgFile - the image filename 
      #$pRecipients - the email recipients list (separated by commas)
      $vHTMLCode =
      '<html>' ||
       '<body>' ||
       '<table border="1" id="vertical-1">' ||
       '<caption>Email ID: [$pMsgId]</caption>' ||
       '<tr>' ||
       '<th>Content</th>' ||
       '<td>[$pMsgContent]</td>' ||
       '</tr>' ||
       '</table>' ||
       '<img src="cid:[$pImgFile]">' ||
       '</body>' ||
      #Exit special characters for echo
      $vHTMLCode = replace_substr($vHTMLCode,'<','^<');
      $vHTMLCode = replace_substr($vHTMLCode,'>','^>');
      #Generate temporary file with HTML Code
      exec('cmd.exe','echo [$vHTMLCode] > [$pInputPath]\tmpSendEmail.html',0);
      #Use Blat to send email (HTML & Image)
      exec('cmd.exe','blat [$pInputPath]\tmpSendEmail.html -to [$pRecipients] -subject "[$pMsgSubject]" -html -embed [$pInputPath]\[$pImgFile]',0);
      #Note that you can add an additional step for file cleanup
    5. Place your image file (ex: company logo) in the input directory
    6. Execute a job calling the function and passing all required arguments

      Send_Email(1,'Example','This is a test email for the new custom function Send_Email','c:\\Temp\\SendEmail\\','logo.png','');
    7. Final result
    8. If you prefer to use your own SMTP calls, you can do that in a custom function or call a batch script.
      Below I summarized the initial things you need for your SMTP code:

      $ telnet <SMTP server> 25
      $ HELO <>
      $ MAIL FROM: <>
      $ RCPT TO: <>
      $ DATA
      # Add some header lines to tell SMTP server how to handle the HTML code
      Mime-Version: 1.0;
      Content-Type: text/html; charset="ISO-8859-1";
      Content-Transfer-Encoding: 7bit;
      #Place your HTML code below and make sure to finish the code by a single period in a new line
      $ QUIT

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s