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
      http://www.blat.net
    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
      0
  2. Create a new custom function in SAP Data Services
    1. Create custom function
      1
    2. Create parameters
      2
      3
      3_b
      4
      5
      6
      6_B
    3. Create HTML Code Variable
      7
      8
    4. Create function code
      13

      #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>' ||
      '</html>';
      
      #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);
      
      Return(1);
      #Note that you can add an additional step for file cleanup
    5. Place your image file (ex: company logo) in the input directory
      10
    6. Execute a job calling the function and passing all required arguments
      9_b

      Send_Email(1,'Example','This is a test email for the new custom function Send_Email','c:\\Temp\\SendEmail\\','logo.png','Recipient@Domain.com');
    7. Final result
      12
    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 <domain.com>
      $ MAIL FROM: <name@domain.com>
      $ RCPT TO: <name@domain.com>
      $ 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
      <html>
      <body>
      </body>
      </html>
      .
      $ QUIT

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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