Commit fb261ad7 authored by Karl Kornel's avatar Karl Kornel
Browse files

Start our email-sending code

parent 04ba9d22
Loading
Loading
Loading
Loading
+131 −0
Original line number Diff line number Diff line
@@ -4,10 +4,13 @@ use strict;
use warnings;

use Cwd;
use Email::MIME;
use Email::Send;
use FindBin qw($RealBin $RealScript);
use Fcntl qw(:flock);
use IPC::Open3;
use Symbol qw(gensym);
use Sys::Hostname;


# First, let's set our "constants".  These are settings that aren't likely to
@@ -410,6 +413,134 @@ EOF
}


# email_recipients: Parse our email config file to get a list of recipients
sub email_recipients {
	my ($path) = @_;

	my $email_handle;
	open($email_handle, '<', $path) or do {

	};

	# Go through each line, building our address list
	my @list;
	while (my $line = <$email_handle>) {
		# Remove any line endings.  We need to do this specially in case
		# the file was modified on Windows.
		$line =~ s{\A(.+)\r?\n?\z}{$1}xims;

		# Skip blank lines and comments
		next if $line =~ m{\A\s+\z}xims;
		next if $line =~ m{\A\#}xims;

		# Read each line as an email address
		push @list, $line;
	}

	# Return a list ref
	return \@list;
}


# email_failure: Send an email when something goes wrong
# $message: A message explaining what happened.
# $action: The action being performed.
# $runfolder: The path to the run folder.
# $config: Our email recipients config file.
# $log_path: The path to the log file, or undef (if there is no log file).
# $log_ref: A scalar ref, pointing to and unwritten log content.
sub email_failure {
	my ($message, $action, $runfolder, $config, $log_path, $log_ref) = @_;

	# 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!\nThis is $RealScript, running on ";
	$email_body_text .= hostname() . "\n";
	$email_body_text .= <<"EOF";
I am emailing you to report that something went wrong.

Here's a couple-word summary of what's going on:
> $message

If a log is available, it is being attached as "workflow-log.txt".

When you have a moment, please investigate.  To re-run the workflow, use the
following command:

$RealBin/$RealScript $action $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.

Apologies for the inconvenience!

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

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

	# If we have a log file, then re-read it into memory
	if (defined($log_path)) {
		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.
This happened while trying to report an error.
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_file->body_set($log_body);
	} else {
		# If there's no log file, then use what's in memory already.
		$email_file->body_str_set($$log_ref);
	}

	# Create the email
	my $email_subject = 'Something went wrong with $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_file ],
	);

	# Send the email
	my $email_sender = Email::Send->new({
		mailer => 'sendmaill',
	});
	$email_sender->send($email);

	# Finally, done!
	return 1;
}


#
# Other Subroutines Go Here
#