How to get same Mailer behaviour for Jenkins pipeline

I started using Jenkins declarative pipelines. Now I want to have the same email notification behavior as defined in the Usage of the Mailer plugin:

  1. Every failed build triggers a new e-mail.
  2. A successful build after a failed (or unstable) build triggers a new e-mail, indicating that a crisis is over.
  3. An unstable build after a successful build triggers a new e-mail, indicating that there’s a regression.
  4. Unless configured, every unstable build triggers a new e-mail, indicating that regression is still there.

I read about Notifications in Pipelines, but it does not notify based on above rules. Plus it does not contain part of the console output in the message body in case of a build failure.

  • How do I discover the additional causes of my Jenkins build?
  • Combining Build triggers in Jenkins
  • how to get submodules to trigger a Jenkins build
  • Commit with case-change is being ignored
  • Jenkins : actual and formal argument lists differ in length
  • CMake, C++ and Jenkins/Continuous integration
  • Does anybody know how to do this in declarative pipeline?

  • How to run HP fortify scan using gradle or Running Fortify from Gradle build on Jenkins
  • Not getting the coverage on new code in sonar dashboard
  • Pushing/committing with the Jenkins git plugin: #Not on any branch (GitHub repo)
  • How to have jenkins-cli.jar work with Jenkins running https?
  • Where should keys be placed on a Jenkins server?
  • Showing exception while connecting slave to master using headless jnlp
  • One Solution collect form web for “How to get same Mailer behaviour for Jenkins pipeline”

    With the following code you can use the mailer plugin in the post section. This provides the expected behaviour:

    pipeline {
      agent any
      stages {
          stage('test') {
            steps {
              script {
                  // change to 'UNSTABLE' OR 'FAILED' to test the behaviour 
                  currentBuild.result = 'SUCCESS'
              }
            }
          }
      }
      post {
            always {
              step([$class: 'Mailer',
                notifyEveryUnstableBuild: true,
                recipients: "test@test.com",
                sendToIndividuals: true])
            }
      }
    }
    
    Git Baby is a git and github fan, let's start git clone.