Skip to content

Instantly share code, notes, and snippets.

@pditommaso
Forked from ewels/NF_logo.png
Created June 14, 2017 15:53
Show Gist options
  • Save pditommaso/8ab5808c54938cefe123705e0b90580a to your computer and use it in GitHub Desktop.
Save pditommaso/8ab5808c54938cefe123705e0b90580a to your computer and use it in GitHub Desktop.
NextFlow Multipart HTML Emails
<html>
<head>
<title>NextFlow Pipeline Complete</title>
</head>
<body>
<div style="font-family: Helvetica, Arial, sans-serif; padding: 30px; max-width: 800px; margin: 0 auto;">
<h1>NextFlow Pipeline Complete</h1>
<h2>Run Name: $runName</h2>
<% if (success){
out << """
<div style="color: #3c763d; background-color: #dff0d8; border-color: #d6e9c6; padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px;">
NGI-RNAseq execution completed successfully!
</div>
"""
} else {
out << """
<div style="color: #a94442; background-color: #f2dede; border-color: #ebccd1; padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px;">
<h4 style="margin-top:0; color: inherit;">NGI-RNAseq execution completed unsuccessfully!</h4>
<p>The exit status of the task that caused the workflow execution to fail was: <code>$exitStatus</code>.</p>
<p>The full error message was:</p>
<pre style="white-space: pre-wrap; overflow: visible; margin-bottom: 0;">${errorReport}</pre>
</div>
"""
} %>
<p>The workflow was completed at <strong>$dateComplete</strong> (duration: <strong>$duration</strong>)</p>
<p>The command used to launch the workflow was as follows:</p>
<pre style="white-space: pre-wrap; overflow: visible; background-color: #ededed; padding: 15px; border-radius: 4px; margin-bottom:30px;">$commandLine</pre>
<h3>Pipeline Configuration:</h3>
<table style="width:100%; max-width:100%; border-spacing: 0; border-collapse: collapse; border:0; margin-bottom: 30px;">
<tbody style="border-bottom: 1px solid #ddd;">
<% out << summary.collect{ k,v -> "<tr><th style='text-align:left; padding: 8px 0; line-height: 1.42857143; vertical-align: top; border-top: 1px solid #ddd;'>$k</th><td style='text-align:left; padding: 8px; line-height: 1.42857143; vertical-align: top; border-top: 1px solid #ddd;'><pre style='white-space: pre-wrap; overflow: visible;'>$v</pre></td></tr>" }.join("\n") %>
</tbody>
</table>
<p>To find out more information about NextFlow, see <a href="http://nextflow.io">http://nextflow.io</a></p>
<hr style="height: 3px; padding: 0; margin: 24px 0; background-color: #e1e4e8; border: 0;">
<img src="cid:$nf_logo_cid">
</div>
</body>
</html>
============================
NextFlow Pipeline Complete
============================
Run Name: $runName
<% if (success){
out << "NGI-RNAseq execution completed successfully!"
} else {
out << """####################################################
## NGI-RNAseq execution completed unsuccessfully! ##
####################################################
The exit status of the task that caused the workflow execution to fail was: $exitStatus.
The full error message was:
${errorReport}
"""
} %>
The workflow was completed at $dateComplete (duration: $duration)
The command used to launch the workflow was as follows:
$commandLine
Pipeline Configuration:
-----------------------
<% out << summary.collect{ k,v -> " - $k: $v" }.join("\n") %>
--
To find out more information about NextFlow, see http://nextflow.io
#!/usr/bin/env nextflow
/*
vim: syntax=groovy
-*- mode: groovy;-*-
*/
// Import some Java packages that we need for e-mail
import javax.mail.internet.*
import javax.mail.*
import javax.activation.*
// Set up the default parameters
params.email = false
params.email_protocol = "smtp"
params.email_host = "localhost"
params.email_port = "25"
// Dummy channel to get the pipeline to run
cheers=Channel.from "Bonjour", "Ciao","Hello","Hola"
process sayHello {
input: val x from cheers
script: "echo $x world!"
}
// Run when pipeline is complete
workflow.onComplete {
// Set up the e-mail variables
def email_fields = [:]
email_fields['version'] = version
email_fields['runName'] = workflow.runName
email_fields['success'] = workflow.success
email_fields['dateComplete'] = workflow.complete
email_fields['duration'] = workflow.duration
email_fields['exitStatus'] = workflow.exitStatus
email_fields['errorMessage'] = (workflow.errorMessage ?: 'None')
email_fields['errorReport'] = (workflow.errorReport ?: 'None')
email_fields['commandLine'] = workflow.commandLine
email_fields['projectDir'] = workflow.projectDir
email_fields['summary'] = [:]
email_fields['summary']['Date Started'] = workflow.start
email_fields['summary']['Date Completed'] = workflow.complete
email_fields['summary']['Nextflow Version'] = workflow.nextflow.version
email_fields['summary']['Nextflow Build'] = workflow.nextflow.build
email_fields['summary']['Nextflow Compile Timestamp'] = workflow.nextflow.timestamp
email_fields['summary']['Pipeline script file path'] = workflow.scriptFile
email_fields['summary']['Pipeline script hash ID'] = workflow.scriptId
// MIME image cid (for NF logo)
email_fields['nf_logo_cid'] = "nflogo"
// Render the e-mail TXT template
def engine = new groovy.text.GStringTemplateEngine()
def tf = new File("$baseDir/email_template.txt")
def txt_template = engine.createTemplate(tf).make(email_fields)
def email_txt = txt_template.toString()
// Render the e-mail HTML template
def hf = new File("$baseDir/email_template.html")
def html_template = engine.createTemplate(hf).make(email_fields)
def email_html = html_template.toString()
if(params.email){
try {
// Configure how to send the mail
Properties mprops = new Properties();
mprops.setProperty("mail.transport.protocol", params.email_protocol);
mprops.setProperty("mail.host", params.email_host);
mprops.setProperty("mail.smtp.port", params.email_port);
Session lSession = Session.getDefaultInstance(mprops);
new MimeMessage(lSession).with { msg ->
// Create MultiPart mail
Multipart multiPart = new MimeMultipart("related");
// txt part
/*
NB: Can't get this to work as well as the embedded image
if wanted, remove the NF Logo section and change the above
line to: Multipart multiPart = new MimeMultipart("alternative");
*/
// MimeBodyPart textPart = new MimeBodyPart();
// textPart.setText(email_txt, "utf-8");
// multiPart.addBodyPart(textPart);
// html part
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(email_html, "text/html; charset=utf-8");
multiPart.addBodyPart(htmlPart);
// NF Logo
MimeBodyPart nfLogo = new MimeBodyPart();
nfLogo.attachFile("$baseDir/NF_logo.png");
nfLogo.setContentID("<" + email_fields['nf_logo_cid'] + ">");
nfLogo.setDisposition(MimeBodyPart.INLINE);
multiPart.addBodyPart(nfLogo);
// Build the e-mail
setRecipients(MimeMessage.RecipientType.TO, new InternetAddress(params.email));
setFrom(new InternetAddress(params.email));
setSubject("NextFlow - Pipeline Complete: " + workflow.runName);
setContent(multiPart);
// Send the e-mail
Transport transporter = lSession.getTransport("smtp");
transporter.connect();
transporter.send(msg);
// Celebrate!
log.info "Sent NextFlow summary e-mail to $params.email"
}
} catch( MessagingException mex ) {
log.error "There was an error whilst sending the summary e-mail"
mex.printStackTrace()
}
} else {
log.warn "No e-mail address set - skipping summary e-mail"
}
}
Save NextFlow logo as this file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment