Jenkins : Email-ext Recipes

This page is a user maintained page of recipes for various things you can do with the Email-ext plugin for email notifications.

Templates

Additional Templates In the Source Code

The "Jelly content" and "Script content" contain links to html.jelly, text.jelly, groovy-html.template and groovy-text.template files. They are quite simple ones, and for example do not contain code coverage template. On the other hand the "Template Examples" section contain link to only one file, which it too advanced for me, and didn't actually work for some reason.

Anyway it took me some time to discover that more templates are actually available at https://github.com/jenkinsci/email-ext-plugin/tree/master/src/main/resources/hudson/plugins/emailext/templates . It would be nice it the wiki page contained either a link to this directory or links to remaining files from the directory, could you please add it?

Pre-send Scripts

Set Message As Important

I wanted to mark failure messages as important ones. I found on google this comment https://issues.jenkins-ci.org/browse/JENKINS-13912?focusedCommentId=163420&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-163420, saying that it is possible the pre-send script feature, however the comment does not explain how to do it. The "Pre-send script" section contain a link to the MimeMessage API, but fails to include links to the two other objects (Build, which is especially important for testing if build failed, and PrintStream) - would it be possible to add them? Additionally it would be nice if the wiki page included some sample script, showing how to use it. 

Anyway it turned out that adding headers is quite easy, and I've finally ended with this simple script:

 

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");

Filter Recipients On Domain

The script below will filter out the recipients that DO NOT contain '@gooddomain.com'

Filter Recipients on Domain
recipients = msg.getRecipients(javax.mail.Message.RecipientType.TO)
filtered = recipients.findAll { addr -> addr.toString().endsWith('@gooddomain.com') }
msg.setRecipients(javax.mail.Message.RecipientType.TO, filtered as javax.mail.Address[])

Filter Recipients On White List

The script below will filter out the recipients that DO NOT contain one of the defined white list values

Filter Recipients On White List
emailWhiteList= ["person1", "person2", "@goodDomain1.com", "@goodDomain2.com"]

def includedInWhiteList(addr) {
    for (white_address in emailWhiteList) {
        if (addr.toString().contains(white_address)) {
            return 1
            break
        }
    }
    return 0
}

recipients = msg.getRecipients(javax.mail.Message.RecipientType.TO)
filtered = recipients.findAll { addr -> includedInWhiteList(addr) > 0 }
msg.setRecipients(javax.mail.Message.RecipientType.TO, filtered as javax.mail.Address[])

Post-send Scripts

Post-send scripts are available starting with version 2.41.

Use rewritten Message-ID from AWS SES for In-Reply-To header

The Amazon Simple Email Service (AWS SES) rewrites the Message-ID header of outgoing emails. That means subsequent failure/success notifications will not be in the same thread, because they reference a non-existing message id in the In-Reply-To header.

The rewritten message id is returned as last message of the SMTP transaction, e.g.

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 correct In-Reply-To headers:

Use AWS SES Message-ID
import com.sun.mail.smtp.SMTPTransport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;

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);
        }
    }
}