[Updated 2022-06-14] How to send arbitrary emails from Katalon/Groovy Script

Download:

website: GitHub - zehm/sendEmail: SendEmail is a lightweight, command line SMTP email client. If you have the need to send email from a command line, this free program is perfect: simple to use and feature rich. It was designed to be used in bash scripts, batch files, Perl programs and web sites, but is quite adaptable and will likely meet your requirements. SendEmail is written in Perl and is unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options, making it very easy to learn and use. SendEmail is licensed under the GNU GPL, either version 2 of the License or (at your option) any later version. [Supported Platforms: Linux, BSD, OS X, Windows 98, Windows NT, Windows 2000, & Windows XP]

Create a batch file with your command string:

@echo %DATE% %TIME%

c:\dl\sendemail\sendemail -f me@example.com -t you@example.com me@example.com -u %3 -s example-mail.server.com:587 -xu me@example.com -xp "password" -o tls=yes -o message-content-type=html -o message-file=%1 -a %2

Where

  • %1 is the message
  • %2 is an attachment (screenshot perhaps)
  • %3 is the subject line
  • See the documentation for the other options available

Create a sendEmail keyword in Katalon:

  /**
   * Sends the report in an email.
   * @param subjectLine (String) The subject line of the email.
   * @param reportFileName (String) The report file to be sent.
   */
  static void sendEmail(String subjectLine, String reportFileName) {
    String screenShot = getScreenshotPath()
    WebUI.takeScreenshot(screenShot)
    String emailParams = '"' + reportFileName + '" ' +
        '"' + screenShot + '" ' +
        '"' + subjectLine + '"'
    comment('Sending email : ' + emailParams)
    runBatchFile('sender.bat ' + emailParams + ' >> sender-log.txt')
  }

And another keyword for runBatchFile:

  /**
   * Execute a batch file situated in the KS project directory.
   * @param batchFile (String) e.g. "myfile.bat"
   */
  static void runBatchFile(String batchFile) {
    String bf = RunConfiguration.getProjectDir() + '/' + batchFile
    comment("Running batch file: " + bf)
    Runtime.runtime.exec(bf)
  }
5 Likes

@Russ_Thomas heeey, don’t encourage spammers :)) with great scripts come greater responsabilities :))
i like the solution, shushh

2 Likes

Updated OP with new links.

1 Like