2
0
mirror of https://github.com/edk2-porting/linux-next.git synced 2024-12-19 10:44:14 +08:00

ktest.pl: Add MAIL_COMMAND option to define how to send email

Allow the user to override the default way to send email. This will allow
the user to add their own mailer and format for sending email.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
This commit is contained in:
Steven Rostedt (VMware) 2018-04-07 20:11:19 -04:00
parent 59f89eb1e3
commit c2d84ddb33
2 changed files with 46 additions and 20 deletions

View File

@ -219,6 +219,7 @@ my $dirname = $FindBin::Bin;
my $mailto; my $mailto;
my $mailer; my $mailer;
my $mail_path; my $mail_path;
my $mail_command;
my $email_on_error; my $email_on_error;
my $email_when_finished; my $email_when_finished;
my $email_when_started; my $email_when_started;
@ -254,6 +255,7 @@ my %option_map = (
"MAILTO" => \$mailto, "MAILTO" => \$mailto,
"MAILER" => \$mailer, "MAILER" => \$mailer,
"MAIL_PATH" => \$mail_path, "MAIL_PATH" => \$mail_path,
"MAIL_COMMAND" => \$mail_command,
"EMAIL_ON_ERROR" => \$email_on_error, "EMAIL_ON_ERROR" => \$email_on_error,
"EMAIL_WHEN_FINISHED" => \$email_when_finished, "EMAIL_WHEN_FINISHED" => \$email_when_finished,
"EMAIL_WHEN_STARTED" => \$email_when_started, "EMAIL_WHEN_STARTED" => \$email_when_started,
@ -4133,16 +4135,6 @@ sub set_test_option {
return eval_option($name, $option, $i); return eval_option($name, $option, $i);
} }
sub _mailx_send {
my ($subject, $message) = @_;
run_command "$mail_path/$mailer -s \'$subject\' $mailto <<< \'$message\'";
}
sub _sendmail_send {
my ($subject, $message) = @_;
run_command "echo \'Subject: $subject\n\n$message\' | $mail_path/sendmail -t $mailto";
}
sub find_mailer { sub find_mailer {
my ($mailer) = @_; my ($mailer) = @_;
@ -4160,22 +4152,44 @@ sub find_mailer {
return undef; return undef;
} }
sub do_send_mail {
my ($subject, $message) = @_;
if (!defined($mail_path)) {
# find the mailer
$mail_path = find_mailer $mailer;
if (!defined($mail_path)) {
die "\nCan not find $mailer in PATH\n";
}
}
if (!defined($mail_command)) {
if ($mailer eq "mail" || $mailer eq "mailx") {
$mail_command = "\$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO <<< \'\$MESSAGE\'";
} elsif ($mailer eq "sendmail" ) {
$mail_command = "echo \'Subject: \$SUBJECT\n\n\$MESSAGE\' | \$MAIL_PATH/\$MAILER -t \$MAILTO";
} else {
die "\nYour mailer: $mailer is not supported.\n";
}
}
$mail_command =~ s/\$MAILER/$mailer/g;
$mail_command =~ s/\$MAIL_PATH/$mail_path/g;
$mail_command =~ s/\$MAILTO/$mailto/g;
$mail_command =~ s/\$SUBJECT/$subject/g;
$mail_command =~ s/\$MESSAGE/$message/g;
run_command $mail_command;
}
sub send_email { sub send_email {
if (defined($mailto)) { if (defined($mailto)) {
if (!defined($mailer)) { if (!defined($mailer)) {
doprint "No email sent: email or mailer not specified in config.\n"; doprint "No email sent: email or mailer not specified in config.\n";
return; return;
} }
if (!defined($mail_path)) { do_send_mail @_;
# find the mailer
$mail_path = find_mailer $mailer;
if (!defined($mail_path)) {
die "\nCan not find $mailer in PATH\n";
}
}
if ($mailer eq "mail" || $mailer eq "mailx"){ _mailx_send(@_);}
elsif ($mailer eq "sendmail" ) { _sendmail_send(@_);}
else { die "\nYour mailer: $mailer is not supported.\n" }
} }
} }

View File

@ -415,6 +415,18 @@
# (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER}) # (default: for sendmail "/usr/sbin/sendmail", otherwise equals ${MAILER})
#MAIL_EXEC = /usr/sbin/sendmail #MAIL_EXEC = /usr/sbin/sendmail
# #
# The command used to send mail, which uses the above options
# can be modified. By default if the mailer is "sendmail" then
# MAIL_COMMAND = echo \'Subject: $SUBJECT\n\n$MESSAGE\' | $MAIL_PATH/$MAILER -t $MAILTO
# For mail or mailx:
# MAIL_COMMAND = "$MAIL_PATH/$MAILER -s \'$SUBJECT\' $MAILTO <<< \'$MESSAGE\'
# ktest.pl will do the substitution for MAIL_PATH, MAILER, MAILTO at the time
# it sends the mail if "$FOO" format is used. If "${FOO}" format is used,
# then the substitutions will occur at the time the config file is read.
# But note, MAIL_PATH and MAILER require being set by the config file if
# ${MAIL_PATH} or ${MAILER} are used, but not if $MAIL_PATH or $MAILER are.
#MAIL_COMMAND = echo \'Subject: $SUBJECT\n\n$MESSAGE\' | $MAIL_PATH/$MAILER -t $MAILTO
#
# Errors are defined as those would terminate the script # Errors are defined as those would terminate the script
# (default 1) # (default 1)
#EMAIL_ON_ERROR = 1 #EMAIL_ON_ERROR = 1