Sending Emails

abstract Sahi can send emails at the end of a suite, or from anywhere inside a script. To email playback reports at the end of a suite run, refer to Playback via Editor. This section explains how to send out emails from within a script.
There is a small program to send emails which is bundled with Sahi. It is just a thin wrapper over JavaMail.
  1. NOTE: Java 1.8 or above is required.
  2. Call the below function for sending emails, and make changes accordingly.
    function sendEmail($emailSubject, $emailBody) {
      var $host = "smtp.gmail.com";
      var $port = 465;
      var $username = "from@gmail.com";
      var $password = "password";
      var $isSSL = true; // set to true if you use SSL
      var $mailer = new Packages.net.sf.sahi.util.Mailer($host, $port, $username, $password, $isSSL);
      $mailer.setFrom("from@gmail.com");
      $mailer.setTo("to@example.com");
      $mailer.setSubject($emailSubject);
      $mailer.setBody($emailBody);
      $mailer.addAttachment("Attachment.zip", "d:/Attachment.zip");
      $mailer.send();
    }
    Call the below function for sending emails with inline attachments after making appropriate changes.
    function sendEmailWithInlineAttachment($emailSubject, $emailBody, $contentId1, $contentId2) {
      var $host = "smtp.gmail.com";
      var $port = 465;
      var $username = "from@gmail.com";
      var $password = "password";
      var $isSSL = true; // set to true if you use SSL
      var $mailer = new Packages.net.sf.sahi.util.Mailer($host, $port, $username, $password, $isSSL);
      $mailer.setFrom("from@gmail.com");
      $mailer.setTo("to@example.com");
      $mailer.setSubject($emailSubject);
      $mailer.setBody($emailBody);
      $mailer.addAttachment("Attachment.zip", "d:/Attachment.zip");
      $mailer.addAttachmentInline($contentId1, "d:/image1.png");
      $mailer.addAttachmentInline($contentId2, "d:/image2.png");
      $mailer.send();
    }
    Call following function to use sahi/userdata/config/email.properties file for sending email.
    infoFor details about email.properties, refer Email.properties file explained.
    function sendEmailWithProps($emailSubject, $emailBody) {
      var $props = loadProperties(_userDataPath("config/email.properties"), false);
      var $mailer = new Packages.net.sf.sahi.util.Mailer($props);
      $mailer.addTo("additional@gmail.com"); //This will append to the already present receipent address from properties file
      $mailer.setBody($emailBody);
      $mailer.setSubject($emailSubject);
      $mailer.addAttachment("Attachment.zip", "d:/Attachment.zip");
      $mailer.send();
    }
    
    function loadProperties($path, $isXML) {
      var $props = new java.util.Properties();
      try {
        var $inStream = new java.io.FileInputStream($path);
        if ($isXML) {
          $props.loadFromXML($inStream);
        } else {
          $props.load($inStream);
        }
        $inStream.close();
      } catch (e) {
                    // do nothing
      }
      return $props;
    }
    info NOTE: Refer to the APIs listed below for more details.
    Refer to Working with Java object directly in Sahi Script to see an example of reading property files via Java and Sahi Script Refer to Email.properties file explained for detailed explanation about email.properties.
  3. Call it directly as
    sendEmail("Mail from Sahi", "All izz well");
    or
    //contentId's must be unique
    var $contentId1 = "inlineImage1";
    var $contentId2 = "inlineImage2";
    var $inlineImg1 = "<img src=\"cid:" +$contentId1 + "\" />";
    var $inlineImg2 = "<img src=\"cid:"+ $contentId2 + "\" />";
    
    sendEmail("Mail from Sahi", "All izz well"+$inlineImg1 + "2nd phrase" +$inlineImg2, $contentId1, $contentId2);
    or
    sendEmailWithProps("Mail from Sahi", "All izz well");
  4. To trigger an email at the end of script, add a onScriptEnd function to your script:
    function onScriptEnd(){
      var $status = _scriptStatus(); // "FAILURE" or "SUCCESS"
      var $scriptName = _scriptName();
      var $scriptPath = _scriptPath(); // Script name with full file path
      sendEmail($status + ": " + $scriptName, "Script: " + $scriptPath + "\nStatus: " + $status);
    }
The Mailer class mentioned above exposes the following public methods that can be used while sending email:

MethodDocumentation
setTo($toList)Sets recipient address to $toList. eg. $mailer.setTo("awesome@gmail.com"); or $mailer.setTo("pro@gmail.com, else@gmail.com");
addTo($toList)Adds $toList to existing recipient To address list eg. $mailer.addTo("awesome@gmail.com"); or $mailer.addTo("pro@gmail.com, else@gmail.com");
setCC($ccList)Sets CC to $ccList. eg. $mailer.setCC("awesome@gmail.com"); or $mailer.setCC("pro@gmail.com, else@gmail.com");
addCC($ccList)Adds $ccList to existing list for CC eg. $mailer.addCC("awesome@gmail.com"); or $mailer.addCC("pro@gmail.com, else@gmail.com");
setBCC($bccList)Sets BCC to $bccList. eg. $mailer.setBCC("awesome@gmail.com"); or $mailer.setBCC("pro@gmail.com, else@gmail.com");
addBCC($bccList)Adds $bccList to existing list for BCC eg. $mailer.addBCC("awesome@gmail.com"); or $mailer.addBCC("pro@gmail.com, else@gmail.com");
setSubject($subject)Sets the subject of email to $subject.
addSubject($subject)Sets the subject of email to $subject. This is now deprecated. Use setSubject instead.
setBody($body)Sets the body of email to $body
addBody($body)Sets the body of email to $body. This is now deprecated. Use setBody instead.
addAttachment($name, $path);Adds attachment with name as $name and path as $path. Can be called multiple times for multiple attachments.
setMailProperty($property, $value);Sets additional email properties. eg. $mailer.setMailProperty("mail.smtp.starttls.enable", "true");
Refer to Sahi call back functions for more details on onScriptEnd