Commit 0a058e95 authored by Karl Kornel's avatar Karl Kornel
Browse files

Add code to send the analysis email.

We still need to look up the path to laneBarcode.html.
parent e8d90ca0
Loading
Loading
Loading
Loading
+133 −0
Original line number Diff line number Diff line
@@ -217,6 +217,8 @@ EOF
	# TODO: Send barcodes email
	if ($analysis_exit_code == 0) {
		log_msg("That's good! 8-)\n");
		email_complete($RUNFOLDER, "$RealBin/email-recipients.txt",
		               $LOGPATH);
	} else {
		log_msg("That's bad 8-(\n");
		email_failure('The analysis program had an error',
@@ -457,6 +459,137 @@ sub email_recipients {
}


# email_analysis: Send an email when the analysis is complete
# $message: A message explaining what happened.
# $runfolder: The path to the run folder.
# $config: Our email recipients config file.
# $log_path: The path to the log file.
sub email_analysis {
	my ($message, $runfolder, $config, $log_path) = @_;

	# Create the body part
	my $email_body = Email::MIME->create(
		attributes => {
			content_type => 'text/plain',
			charset      => 'UTF-8',
			encoding     => 'quoted-printable',
		}
	);
	my $email_body_text = "Hello!\n\nThis is $RealScript, running on ";
	$email_body_text .= hostname() . '.  ';
	$email_body_text .= <<"EOF";
An analysis has just been completed, using the data in the following run folder:

$runfolder

The log from the run is being attached as "workflow-log.txt", along with
"laneBarcodes.html".

If everything looks good, you can deliver the files to the client by running
the following command:

sudo $RealBin/$RealScript deliver $runfolder

(You can just copy/paste the above line into a PuTTY or SecureCRT session.)

If you still need assistance, please forward this mail (with attachments) to research-computing-support\@stanford.edu.

Have a good day!

~ Mr. Workflow
EOF
	$email_body->body_str_set($email_body_text);

	# Create the log file attachment
	my $email_log = Email::MIME->create(
		attributes => {
			disposition  => 'attachment',
			name         => 'workflow-log.txt',
			content_type => 'text/plain',
			charset      => 'iso-8859-1',
			encoding     => 'base64',
		},
	);

	# We should have a log file, so read it into memory
	my $log_handle;
	my $log_body = '';
	open($log_handle, '<', $log_path) or do {
		log_msg(<<"EOF");
We have just encounted a problem when trying to re-open our log file.
The file we were trying to read is: $log_path
The error we got is: $!
EOF
		return 0;
	};
	while (my $line = <$log_handle>) {
		$log_body .= $line . "\n";
	}
	$email_log->body_set($log_body);
	close ($log_handle);
	undef $log_body;

	# Create the laneBarcode attachment
	my $email_barcode = Email::MIME->create(
		attributes => {
			disposition  => 'attachment',
			name         => 'laneBarcode.html',
			content_type => 'text/html',
			charset      => 'iso-8859-1',
			encoding     => 'base64',
		},
	);

	# Get the laneBarcode HTML file, and read it into memory
	# TODO: Get the laneBarcode path.
	my $barcode_handle;
	my $barcode_body = '';
	open($barcode_handle, '<', $barcode_path) or do {
		log_msg(<<"EOF");
We have just encountered a roblem when trying to open the landBarcode HTML file.
The file we were trying to read is: $barcode_path
The error we got is: $!
EOF
		return 0;
	};
	while (my $line = <$barcode_handle>) {
		$barcode_body .= $line . "\n";
	}
	$email_barcode->body_set($barcode_body);
	close($barcode_handle);
	undef $barcode_body;

	# Create the email
	my $email_subject = "Completed analysis by $RealScript on " .
	                    hostname();
	my $email = Email::MIME->create(
		header_str => [
			From    => 'noreply@stanford.edu',
			To      => email_recipients($config),
			Subject => $email_subject,
		],
		parts      => [ $email_body, $email_log, $email_barcode ],
	);
	log_msg('Sending a ' . length($email->as_string) . '-byte email to ');
	log_msg(email_recipients($config) . "\n");

	# Send the email
	my $email_sender = Email::Send->new({
		mailer      => 'SMTP',
		mailer_args => [
			Host => 'smtp-unencrypted.stanford.edu',
		],
	});
	my $send_result = $email_sender->send($email->as_string);
	if (!$send_result) {
		log_msg("Error sending email: $send_result\n");
		return 0;
	}

	# Finally, done!
	return 1;
}

# email_failure: Send an email when something goes wrong
# $message: A message explaining what happened.
# $action: The action being performed.