{"id":32908,"date":"2026-11-29T22:05:42","date_gmt":"2026-11-29T22:05:42","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32908"},"modified":"2026-11-29T22:05:42","modified_gmt":"2026-11-29T22:05:42","slug":"jenkins-pipeline-code-for-sending-an-email-on-build-failure","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/jenkins-pipeline-code-for-sending-an-email-on-build-failure\/","title":{"rendered":"Jenkins Pipeline code for Sending an email on Build Failure"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To get an email when a Jenkins build fails, call the <code>mail<\/code> step (Mailer plugin) or the <code>emailext<\/code> step (Email Extension plugin) inside the <code>post { failure { ... } }<\/code> block of a declarative Jenkinsfile. The examples above show the basic pattern. Below you&#039;ll find how to set up SMTP, a cleaned-up Jenkinsfile, a richer <code>emailext<\/code> version, every post condition, and corrected ways to let a pipeline continue after a step fails.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\r\r\n pipeline {  \r\n     agent any  \r\n     stages {  \r\n         stage(<span class=\"hljs-string\">'Test'<\/span>) {  \r\n             steps {  \r\n                 sh <span class=\"hljs-string\">'echo \"Fail!\"; exit 1'<\/span>  \r\n             }  \r\n         }  \r\n     }  \r\n     post {  \r\n         always {  \r\n             <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'This will always run'<\/span>  \r\n         }  \r\n         success {  \r\n             <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'This will run only if successful'<\/span>  \r\n         }  \r\n         failure {  \r\n             mail bcc: <span class=\"hljs-string\">''<\/span>, body: <span class=\"hljs-string\">\"&lt;b&gt;Example&lt;\/b&gt;&lt;br&gt;Project: ${env.JOB_NAME} &lt;br&gt;Build Number: ${env.BUILD_NUMBER} &lt;br&gt; URL de build: ${env.BUILD_URL}\"<\/span>, cc: <span class=\"hljs-string\">''<\/span>, charset: <span class=\"hljs-string\">'UTF-8'<\/span>, from: <span class=\"hljs-string\">''<\/span>, mimeType: <span class=\"hljs-string\">'text\/html'<\/span>, replyTo: <span class=\"hljs-string\">''<\/span>, subject: <span class=\"hljs-string\">\"ERROR CI: Project name -&gt; ${env.JOB_NAME}\"<\/span>, to: <span class=\"hljs-string\">\"foo@foomail.com\"<\/span>;  \r\n         }  \r\n         unstable {  \r\n             <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'This will run only if the run was marked as unstable'<\/span>  \r\n         }  \r\n         changed {  \r\n             <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'This will run only if the state of the Pipeline has changed'<\/span>  \r\n             <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'For example, if the Pipeline was previously failing but is now successful'<\/span>  \r\n         }  \r\n     }  \r\n }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">post {\r\n always {\r\n   sh <span class=\"hljs-string\">'echo \"This will always run\"'<\/span>\r\n }\r\n success {\r\n  sh <span class=\"hljs-string\">'echo \"This will run only if successful\"'<\/span>\r\n }\r\n failure {\r\n  sh <span class=\"hljs-string\">'echo \"This will run only if failed\"'<\/span>\r\n }\r\n unstable {\r\n  sh <span class=\"hljs-string\">'echo \"This will run only if the run was marked as unstable\"'<\/span>\r\n }\r\n changed {\r\n  sh <span class=\"hljs-string\">'echo \"This will run only if the state of the Pipeline has changed\"'<\/span>\r\n  sh <span class=\"hljs-string\">'echo \"For example, the Pipeline was previously failing but is now successful\"'<\/span>\r\n  sh <span class=\"hljs-string\">'echo \"... or the other way around :)\"'<\/span>\r\n }\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">To ignore a failed step <span class=\"hljs-keyword\">in<\/span> declarative pipeline you basically have two options:\r\n\r\nUse script step and <span class=\"hljs-keyword\">try<\/span>-<span class=\"hljs-keyword\">catch<\/span> block (similar to previous proposition by R_K but <span class=\"hljs-keyword\">in<\/span> declarative style)\r\nstage(<span class=\"hljs-string\">'someStage'<\/span>) {\r\n    steps {\r\n        script {\r\n            <span class=\"hljs-keyword\">try<\/span> {\r\n                build job: <span class=\"hljs-string\">'system-check-flow'<\/span>\r\n            } <span class=\"hljs-keyword\">catch<\/span> (err) {\r\n                echo err.getMessage()\r\n            }\r\n        }\r\n        echo currentBuild.result\r\n    }\r\n}\r\nUse catchError\r\nstage(<span class=\"hljs-string\">'someStage'<\/span>) {\r\n    steps {\r\n        catchError {\r\n            build job: <span class=\"hljs-string\">'system-check-flow'<\/span>\r\n        }\r\n        echo currentBuild.result\r\n    }\r\n}\r\nIn both cases the build won<span class=\"hljs-string\">'t be aborted upon exception in build job: '<\/span>system-check-flow<span class=\"hljs-string\">'. In both cases the echo step (and any other following) will be executed.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the example above works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a declarative pipeline, the <code>post<\/code> section runs after all stages finish. Each condition block (<code>always<\/code>, <code>success<\/code>, <code>failure<\/code> and so on) runs only when the build matches that condition. In the first example, the <code>Test<\/code> stage runs <code>exit 1<\/code>. That marks the build <code>FAILURE<\/code>, so the <code>failure<\/code> block runs and the <code>mail<\/code> step sends an HTML message built from the <code>JOB_NAME<\/code>, <code>BUILD_NUMBER<\/code> and <code>BUILD_URL<\/code> environment variables.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>mail<\/code> step comes from the <strong>Mailer<\/strong> plugin. It is part of the suggested plugin set on most installations, but it sends nothing until SMTP is configured.<\/li>\n\n\n<li>The body and subject use double quotes so that Groovy fills in <code>${env.JOB_NAME}<\/code> before sending. Inside single quotes, the variables would show up literally in the email.<\/li>\n\n\n<li><code>mimeType: &#039;text\/html&#039;<\/code> makes mail clients render the <code>&lt;b&gt;<\/code> and <code>&lt;br&gt;<\/code> tags. Without it, recipients see the raw tags.<\/li>\n\n\n<li>Empty values such as <code>from: &#039;&#039;<\/code> or <code>cc: &#039;&#039;<\/code> can be left out. If <code>from<\/code> is empty, Jenkins uses the system admin email address.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> A failed test run published with <code>junit<\/code> usually marks the build <code>UNSTABLE<\/code>, not <code>FAILURE<\/code>. The <code>failure<\/code> block does not run for unstable builds. Use <code>unsuccessful<\/code> if you want an email for both.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure SMTP so Jenkins can send mail<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Manage Jenkins &gt; System<\/strong>.<\/li>\n\n\n<li>Under <strong>Jenkins Location<\/strong>, set <strong>Jenkins URL<\/strong> (for example <code>https:\/\/jenkins.example.com\/<\/code>). If you skip this, <code>BUILD_URL<\/code> is empty or null in your emails.<\/li>\n\n\n<li>Set <strong>System Admin e-mail address<\/strong>. This becomes the default sender.<\/li>\n\n\n<li>Under <strong>E-mail Notification<\/strong>, enter the SMTP server. Click <strong>Advanced<\/strong> and turn on SMTP authentication, then enter the username and password, the port, and <strong>Use SSL<\/strong> (usually port 465) or <strong>Use TLS<\/strong> (STARTTLS, usually port 587).<\/li>\n\n\n<li>Tick <strong>Test configuration by sending test e-mail<\/strong>, enter an address and click <strong>Test configuration<\/strong>. Fix any error shown before you touch the Jenkinsfile.<\/li>\n\n\n<li>If you use <code>emailext<\/code>, also fill in the separate <strong>Extended E-mail Notification<\/strong> section. It has its own SMTP settings and credentials.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> For Gmail, Microsoft 365 and similar providers, use an app password or a dedicated relay account, not a personal password. Many providers block basic SMTP authentication for normal accounts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A cleaner failure-notification Jenkinsfile<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This version keeps the recipient in one place, uses <code>unsuccessful<\/code> so unstable builds also send an email, and sends a recovery message through <code>fixed<\/code> when a broken build turns green again.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">pipeline {\n    agent any\n\n    environment {\n        NOTIFY_TO = <span class=\"hljs-string\">'ci-team@example.com'<\/span>\n    }\n\n    stages {\n        stage(<span class=\"hljs-string\">'Build'<\/span>) {\n            steps {\n                sh <span class=\"hljs-string\">'echo \"Building...\"'<\/span>\n            }\n        }\n        stage(<span class=\"hljs-string\">'Test'<\/span>) {\n            steps {\n                sh <span class=\"hljs-string\">'echo \"Fail!\"; exit 1'<\/span>\n            }\n        }\n    }\n\n    post {\n        unsuccessful {\n            mail to: env.NOTIFY_TO,\n                 subject: <span class=\"hljs-string\">\"${currentBuild.currentResult}: ${env.JOB_NAME} #${env.BUILD_NUMBER}\"<\/span>,\n                 mimeType: <span class=\"hljs-string\">'text\/html'<\/span>,\n                 charset: <span class=\"hljs-string\">'UTF-8'<\/span>,\n                 body: <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-string\">\"&lt;p&gt;&lt;b&gt;Build ${currentBuild.currentResult}&lt;\/b&gt;&lt;\/p&gt;\n&lt;p&gt;Project: ${env.JOB_NAME}&lt;br&gt;\nBuild number: ${env.BUILD_NUMBER}&lt;br&gt;\nBuild URL: &lt;a href=\"<\/span>${env.BUILD_URL}<span class=\"hljs-string\">\"&gt;${env.BUILD_URL}&lt;\/a&gt;&lt;\/p&gt;\"<\/span><span class=\"hljs-string\">\"\"<\/span>\n        }\n        fixed {\n            mail to: env.NOTIFY_TO,\n                 subject: <span class=\"hljs-string\">\"FIXED: ${env.JOB_NAME} #${env.BUILD_NUMBER}\"<\/span>,\n                 body: <span class=\"hljs-string\">\"The build is passing again: ${env.BUILD_URL}\"<\/span>\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>currentBuild.currentResult<\/code> always has a value (<code>SUCCESS<\/code>, <code>UNSTABLE<\/code>, <code>FAILURE<\/code> and so on). That makes it safer to use in messages than <code>currentBuild.result<\/code>, which stays null until something sets it. The triple-quoted string lets the HTML body span several lines and still fill in the variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Richer emails with emailext<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Email Extension<\/strong> plugin adds the <code>emailext<\/code> step. Use it when you want to attach the build log, include part of the console output, or email the developers whose commits are in the build instead of a fixed list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">post {\n    failure {\n        emailext(\n            to: 'ci-team@example.com',\n            recipientProviders: &#91;culprits(), developers(), requestor()],\n            subject: \"FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}\",\n            mimeType: 'text\/html',\n            attachLog: true,\n            body: '''<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Build <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">b<\/span>&gt;<\/span>$PROJECT_NAME #$BUILD_NUMBER<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">b<\/span>&gt;<\/span> finished with status <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">b<\/span>&gt;<\/span>$BUILD_STATUS<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">b<\/span>&gt;<\/span>.<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Console: <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"${BUILD_URL}console\"<\/span>&gt;<\/span>${BUILD_URL}console<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">pre<\/span>&gt;<\/span>${BUILD_LOG, maxLines=50}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">pre<\/span>&gt;<\/span>'''\n        )\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The body uses single quotes on purpose. That way Groovy leaves <code>$PROJECT_NAME<\/code>, <code>$BUILD_STATUS<\/code> and <code>${BUILD_LOG, maxLines=50}<\/code> alone, and the plugin&#039;s own token engine expands them. <code>culprits()<\/code> emails people who committed since the last successful build, <code>developers()<\/code> emails committers in this build, and <code>requestor()<\/code> emails the user who started the build manually.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Post conditions reference<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jenkins evaluates post conditions in a fixed order, no matter what order you write them in: <code>always<\/code>, <code>changed<\/code>, <code>fixed<\/code>, <code>regression<\/code>, <code>aborted<\/code>, <code>failure<\/code>, <code>success<\/code>, <code>unstable<\/code>, <code>unsuccessful<\/code>, <code>cleanup<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Condition<\/th><th>Runs when<\/th><th>Typical use<\/th><\/tr><\/thead><tbody><tr><td>always<\/td><td>Every build, whatever the result<\/td><td>Archive logs, publish reports<\/td><\/tr><tr><td>changed<\/td><td>Result differs from the previous build<\/td><td>Notify on any status change<\/td><\/tr><tr><td>fixed<\/td><td>Previous build failed or was unstable, this one succeeded<\/td><td>Recovery email<\/td><\/tr><tr><td>regression<\/td><td>Previous build succeeded, this one failed, was unstable or was aborted<\/td><td>Alert on newly broken builds<\/td><\/tr><tr><td>aborted<\/td><td>Build was aborted manually or by a timeout<\/td><td>Notify about cancelled runs<\/td><\/tr><tr><td>failure<\/td><td>Build result is FAILURE<\/td><td>Failure email<\/td><\/tr><tr><td>success<\/td><td>Build result is SUCCESS<\/td><td>Deployment notices<\/td><\/tr><tr><td>unstable<\/td><td>Build result is UNSTABLE (often test failures)<\/td><td>Test failure email<\/td><\/tr><tr><td>unsuccessful<\/td><td>Result is anything other than SUCCESS<\/td><td>One block for all bad outcomes<\/td><\/tr><tr><td>cleanup<\/td><td>After all other post conditions<\/td><td>Delete the workspace, release resources<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Continuing after a failed step: corrected examples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The last example above says that with either option &quot;the build won&#039;t be aborted&quot;. That is true, but the two options leave the build in different states. Plain <code>catchError<\/code> marks both the stage and the build as <code>FAILURE<\/code>, so <code>echo currentBuild.result<\/code> prints <code>FAILURE<\/code> and your <code>failure<\/code> email still goes out. A bare <code>try\/catch<\/code> hides the error completely: the build stays green and <code>currentBuild.result<\/code> is usually null. Pass the result you actually want explicitly:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">stage(<span class=\"hljs-string\">'System check'<\/span>) {\n    steps {\n        <span class=\"hljs-comment\">\/\/ Continue, mark only this stage red, keep the build green<\/span>\n        catchError(buildResult: <span class=\"hljs-string\">'SUCCESS'<\/span>, stageResult: <span class=\"hljs-string\">'FAILURE'<\/span>) {\n            build job: <span class=\"hljs-string\">'system-check-flow'<\/span>\n        }\n\n        <span class=\"hljs-comment\">\/\/ Or: continue but mark build and stage UNSTABLE<\/span>\n        warnError(<span class=\"hljs-string\">'system-check-flow failed'<\/span>) {\n            build job: <span class=\"hljs-string\">'system-check-flow'<\/span>\n        }\n\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Current result: ${currentBuild.currentResult}\"<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">For a downstream job specifically, you can skip the exception completely with <code>propagate: false<\/code> and decide what to do based on the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">stage(<span class=\"hljs-string\">'System check'<\/span>) {\n    steps {\n        script {\n            def downstream = build job: <span class=\"hljs-string\">'system-check-flow'<\/span>, propagate: <span class=\"hljs-keyword\">false<\/span>\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Downstream result: ${downstream.result}\"<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (downstream.result != <span class=\"hljs-string\">'SUCCESS'<\/span>) {\n                unstable(<span class=\"hljs-string\">\"system-check-flow finished with ${downstream.result}\"<\/span>)\n            }\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Avoid <code>catch (err)<\/code> with no rethrow. It also catches the interruption raised when someone aborts the build or a timeout fires, so the pipeline can keep running after it should have stopped.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Build fails but no email arrives and the log shows no error<\/td><td>SMTP not configured, or the message went to spam<\/td><td>Run the test email in <strong>E-mail Notification<\/strong> and check the recipient&#039;s spam folder<\/td><\/tr><tr><td><code>AuthenticationFailedException<\/code> in the console<\/td><td>Wrong username or password, or the provider blocks basic auth<\/td><td>Use an app password or SMTP relay credentials<\/td><\/tr><tr><td>Could not connect to SMTP host<\/td><td>Wrong port, SSL\/TLS mismatch or a firewall<\/td><td>Use 465 with SSL or 587 with TLS, and check outbound access from the controller<\/td><\/tr><tr><td><code>No such DSL method &#039;emailext&#039;<\/code><\/td><td>Email Extension plugin not installed<\/td><td>Install it from <strong>Manage Jenkins &gt; Plugins<\/strong><\/td><\/tr><tr><td><code>BUILD_URL<\/code> is null or empty in the email<\/td><td>Jenkins URL not set<\/td><td>Set <strong>Jenkins URL<\/strong> under <strong>Jenkins Location<\/strong><\/td><\/tr><tr><td>Email shows raw <code>&lt;b&gt;<\/code> tags<\/td><td><code>mimeType<\/code> not set to HTML<\/td><td>Add <code>mimeType: &#039;text\/html&#039;<\/code><\/td><\/tr><tr><td>Test failures do not trigger an email<\/td><td>Build is UNSTABLE, not FAILURE<\/td><td>Use <code>unstable<\/code> or <code>unsuccessful<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep SMTP credentials in Jenkins system configuration or the credentials store, never in the Jenkinsfile.<\/li>\n\n\n<li>Send to a team distribution list or use <code>recipientProviders<\/code> instead of hard-coding personal addresses in many jobs.<\/li>\n\n\n<li>Notify on <code>regression<\/code> and <code>fixed<\/code> for noisy jobs, so people get an email when the status changes, not on every red build.<\/li>\n\n\n<li>Do not attach full logs from builds that may print secrets. Jenkins masks bound credentials, but other values can still leak.<\/li>\n\n\n<li>Put shared notification logic in a shared library function so every pipeline formats emails the same way.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Why is my Jenkins pipeline not sending emails on failure?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Usually SMTP is not configured in Manage Jenkins &gt; System, or the credentials, port or SSL\/TLS settings are wrong. Send a test email from the E-mail Notification section first. Also check that the build really is FAILURE and not UNSTABLE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between mail and emailext in Jenkins?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mail<\/code> comes from the Mailer plugin and sends a simple message to fixed addresses. <code>emailext<\/code> comes from the Email Extension plugin. It can attach logs, use tokens such as <code>${BUILD_LOG}<\/code>, and email committers or the user who started the build through <code>recipientProviders<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I send an email when tests fail in Jenkins?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test failures recorded with the <code>junit<\/code> step usually make the build UNSTABLE. Put the email step in an <code>unstable<\/code> or <code>unsuccessful<\/code> post block instead of <code>failure<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I send an email from a scripted pipeline?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scripted pipelines have no <code>post<\/code> section. Wrap the build in <code>try { ... } catch (e) { mail ...; throw e }<\/code>, or send the notification in a <code>finally<\/code> block that checks <code>currentBuild.currentResult<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I get an email only when a broken build is fixed?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Put the email step in a <code>fixed<\/code> post block. It runs only when the previous build failed or was unstable and the current one succeeded.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/jenkins-pipeline-complete-reference-tutorials\/\">Jenkins Pipeline Complete Reference Tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/environment-variables-and-properties-defined-in-jenkins\/\">Environment variables and properties defined in jenkins | Jenkins Tutorial<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-jenkin-pipeline-interview-questions-and-answers\/\">Top 50 Jenkin pipeline interview questions and answers<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/jenkins\/\">More Jenkins tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To get an email when a Jenkins build fails, call the mail step (Mailer plugin) or the emailext step (Email Extension plugin) inside the post { failure&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[42],"tags":[26190,5105],"class_list":["post-32908","post","type-post","status-publish","format-standard","hentry","category-jenkins","tag-jenkins-pipeline","tag-jenkinsfile"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32908"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32908\/revisions"}],"predecessor-version":[{"id":80424,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32908\/revisions\/80424"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}