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.
- Install Blat
- Download
http://www.blat.net - Configure Blat
C:\Blat\>blat.exe -install <SMTP server address> <default from email address>
- Make sure the Blat folder is setup in the windows Path
- Download
- Create a new custom function in SAP Data Services
- Create custom function
- Create parameters
- Create HTML Code Variable
- 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>' || '</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
- Place your image file (ex: company logo) in the input directory
- 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','Recipient@Domain.com');
- Final result
- 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
- Create custom function