Configuring Jenkins Email Extension Plugin and Groovy Email Notification Script
This guide explains how to install and configure Jenkins' Email Extension plugin, set up SMTP settings, create a shared library Groovy script for email notifications, and test the email functionality within a Jenkins pipeline.
Jenkins requires the Email Extension plugin to send build notifications; install the plugin and navigate to System → Configure System → Extended E‑mail Notification.
Enter SMTP server details (e.g., smtp.qq.com, port 465) and enable SSL, using an authorized code as the password.
In a shared library, add a Groovy file src/org/devops/toemail.groovy that defines an Email method, which uses emailext to send an HTML‑formatted email containing build information such as job name, build ID, status, and links.
package org.devops
//定义邮件内容
def Email(status,emailUser){
emailext body: """
<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\">
</head>
<body leftmargin=\"8\" marginwidth=\"0\" topmargin=\"8\" marginheight=\"4\" offset=\"0\">
<img src=\"http://192.168.1.200:8080/static/0eef74bf/images/headshot.png\">
<table width=\"95%\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif\">
<tr>
<td><br> <b><font color=\"#0B610B\">构建信息</font></b> </td>
</tr>
<tr>
<td>
<ul>
<li>项目名称:${JOB_NAME}</li>
<li>构建编号:${BUILD_ID}</li>
<li>构建状态: ${status} </li>
<li>项目地址:<a href=\"${BUILD_URL}\">${BUILD_URL}</a></li>
<li>构建日志:<a href=\"${BUILD_URL}console\">${BUILD_URL}console</a></li>
</ul>
</td>
</tr>
<tr>
</table>
</body>
</html> """,
subject: "Jenkins-${JOB_NAME}项目构建信息 ",
to: emailUser
}Then create a pipeline script that loads the shared library, calls the toemail.Email method in the post section for success, failure, and aborted cases, and updates GitLab commit status accordingly.
#!groovy
@Library('jenkinslibrary@master') _
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
branchName = branch - "refs/heads/"
currentBuild.description = "Trigger by ${userName} ${branch}"
gitlab.ChangeCommitStatus(projectId,commitSha,"running")
pipeline{
agent { node { label "build"}}
stages{
stage("CheckOut"){
steps{
script{
println("${branchName}")
}
}
}
}
post {
always{
script{
println("always")
}
}
success{
script{
println("success")
gitlab.ChangeCommitStatus(projectId,commitSha,"success")
toemail.Email("流水线成功",userEmail)
}
}
failure{
script{
println("failure")
gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
toemail.Email("流水线失败了!",userEmail)
}
}
aborted{
script{
println("aborted")
gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
toemail.Email("流水线被取消了!",userEmail)
}
}
}
}After configuring and running the pipeline, the email will be sent to the specified address, allowing you to verify the setup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
