This page provides examples of various things you can do with the Email Extension plugin for email notifications.
There are several simple examples referenced in the README
document; more examples can be found at src/main/resources/hudson/plugins/emailext/templates
.
To set a message as important, set the appropriate headers in a pre-send script:
if (build.result.toString().equals("FAILURE")) {
msg.addHeader("X-Priority", "1 (Highest)");
msg.addHeader("Importance", "High");
}
cancel = build.result.toString().equals("ABORTED");
See also:
-
Javadoc for core Jenkins classes, especially
AbstractBuild
To filter out the recipients that do not contain @example.com
:
recipients = msg.getRecipients(jakarta.mail.Message.RecipientType.TO)
filtered = recipients.findAll { addr -> addr.toString().endsWith('@example.com') }
msg.setRecipients(jakarta.mail.Message.RecipientType.TO, filtered as jakarta.mail.Address[])
To filter out the recipients that do not contain one of the defined whitelist values:
emailWhitelist = ["person1", "person2", "@goodDomain1.com", "@goodDomain2.com"]
def includedInWhitelist(addr) {
for (whiteAddress in emailWhitelist) {
if (addr.toString().contains(whiteAddress)) {
return 1
break
}
}
return 0
}
recipients = msg.getRecipients(jakarta.mail.Message.RecipientType.TO)
filtered = recipients.findAll { addr -> includedInWhitelist(addr) > 0 }
msg.setRecipients(jakarta.mail.Message.RecipientType.TO, filtered as jakarta.mail.Address[])
Post-send scripts are available starting with version 2.41.
The Amazon Simple Email Service (SES) rewrites the message ID of outgoing emails.
This means subsequent failure/success notifications will not be in the same thread, because they reference a nonexistent message ID in the In-Reply-To
header.
The rewritten message ID is returned as last message of the SMTP transaction:
250 Ok <00000123456abcde-1234abcd-abcd-1234-1234-1234abcd1234-000000@eu-west-1.amazonses.com>
The following post-send script fetches the rewritten message ID for later use in In-Reply-To
headers:
import com.sun.mail.smtp.SMTPTransport;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String smtpHost = props.getProperty("mail.smtp.host", "");
String awsRegex = "^email-smtp\\.([a-z0-9-]+)\\.amazonaws\\.com\$";
Pattern p = Pattern.compile(awsRegex);
Matcher m = p.matcher(smtpHost);
if (m.matches()) {
String region = m.group(1);
if (transport instanceof SMTPTransport) {
String response = ((SMTPTransport)transport).getLastServerResponse();
String[] parts = response.trim().split(" +");
if (parts.length == 3 && parts[0].equals("250") && parts[1].equals("Ok")) {
String MessageID = "<" + parts[2] + "@" + region + ".amazonses.com>";
msg.setHeader("Message-ID", MessageID);
}
}
}