xand.es

Jenkins: sending email from a file

Jenkins has a very cool plugin called Editable Email Notifications. However it is a bit complicated to send a good-looking complex email from the task configuration it offers. Personally I find it easier to compose en email with an external script, write it to a file and then use the contents of this file as a body for email.

It seems pretty obvious, but the tricky part is actually reading the file into a variale for setting message's content. Fortunately the plugin offers the possiblity to execute Groovy script before sending the email (Pre-Send Script option) which may alter the content of the message body.

Here is the script:


// RESULT\_EMAIL environment variable must contain file path where the body
// is stored
def emailContentFile = build.getEnvironment()['RESULT\_EMAIL'].trim()
def emailContent = ""

def f = new File(emailContentFile).eachLine {
         line -> emailContent += "$line"; 
} 

msg.setContent(emailContent,'text/html; charset=utf-8')

The only thing I need to mention is that this way the content of Default Content parameter is completely ignored.

UPDATE: There is actually a better approach to this. You may simply provide somthing like this in as Default Content parameter:


${FILE, path="result\_email.html"}

result_email.html should be relative to workspace.