How to Send Email notifications via Jenkins

Reading Time: 3 minutes
Jenkins logo

Overview

 In this blog, let’s discuss the approach to sending email notifications from Jenkins. If we want to be informed immediately about problems in our pipeline, they can be very helpful.

Prerequisites:

  • Jenkins Instance
  • Basic knowledge to handle plugins in Jenkins
  • Email Account

Steps involved to send email via Jenkins

An email-sending solution is visible in Jenkins. This can be set up in the Manage Jenkins menu’s Configure System section. This is the built-in functionality of Jenkins

  • Firstly we have to set up SMTP configuration.
    • Go to Jenkins
    • Navigate to Manage Jenkins
    • Go to Configure Systems
    • Search for Email Notification
  • Download Email extension Plugin
    • Go to Jenkins
    • Navigate to Manage Jenkins
    • Click on Manage Plugins
    • Search for the “Email Extension” plugin
Plugin Manager

Test email via Jenkins Job:

Although we can send test emails from the settings, in actual situations, we prefer to get emails regarding pertinent happenings. For instance, we would desire notification when a build fails. For this, let’s make a straightforward job that merely prints a message. Click on New Item from the left panel >> Create a Freestyle project with an appropriate name. It will redirect you to the configuration screen. Scroll down to the “Build Steps” section and add an Execute shell step from the dropdown. Add a simple echo command: echo “Test job for sending emails.” Once triggered, we want a notification to be triggered from Jenkins. To achieve this we can add Post Build steps in the Jenkins job (as shown below). Add a recipients list of who will receive the email once the job is built. Save the configuration and build the job. You will be able to receive the email in the mailbox.  The email subject is “Build success in Jenkins: Email Test Job #1”. We can see these details in the body of the email.

Configuration

Another option to send email via Jenkins:

We can also use the Email Extension plugin. The fact that it supports more events than the built-in solution is crucial. Let’s access the System Configuration menu. A section titled Extended Email Notification will appear. Let’s start by entering a valid email address in the Project Recipients List area. This plugin only emails users when a build fails by default. To see all the events it supports, we should modify it. Scroll down to Triggers in the advanced settings after opening them. Let’s change the trigger type to Always. Numerous more triggers of various kinds are also present here. Many further setting options are available. We can choose who receives notifications, the subject, and content of emails, and even whether to include attachments. However, we may leave this example’s default settings in place and still save the job.

Triggers

Save and run the job to check if the mail is received. Irrespective of job status whether it’s failed or passed the recipients will be able to receive the email along with the details of the job and the build number as well.

Advantages

  • Email makes it easier for the developer to monitor the job
  • Multiple events can be triggered by making use of the plugin, which helps to monitor the job easily
  • Jobs that take a long time to run won’t need continuous attention from the developer. Once completed (irrespective of the result) it will trigger the email to notify the developer.

Conclusion

In this blog, two distinct strategies for using Jenkins to send email notifications were demonstrated. In the beginning, we used the built-in feature that can send emails when a build fails or becomes stable. As a second option, we employed the Email Extension plugin, which may be used when we require additional control over the occasions that result in the notifications.

Reference

Leave a Reply