Skip to content

Latest commit

 

History

History
120 lines (92 loc) · 4.09 KB

recipes.adoc

File metadata and controls

120 lines (92 loc) · 4.09 KB

Email Extension Recipes

Templates

Additional templates in the source code

There are several simple examples referenced in the README document; more examples can be found at src/main/resources/hudson/plugins/emailext/templates.

Pre-send scripts

Setting a message as important

To set a message as important, set the appropriate headers in a pre-send script:

Setting a message as important
if (build.result.toString().equals("FAILURE")) {
    msg.addHeader("X-Priority", "1 (Highest)");
    msg.addHeader("Importance", "High");
}
cancel = build.result.toString().equals("ABORTED");

See also:

Filtering recipients on a domain

To filter out the recipients that do not contain @example.com:

Filtering recipients on a domain
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[])

Filtering recipients on a whitelist

To filter out the recipients that do not contain one of the defined whitelist values:

Filtering recipients on a whitelist
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

Post-send scripts are available starting with version 2.41.

Using the Amazon SES message ID for the In-Reply-To header

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:

Using the Amazon SES message ID
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);
        }
    }
}