From 6b4f996f85b222838c47683f71aa6f01155d744e Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Sun, 26 Jan 2014 00:55:01 -0800 Subject: [PATCH] Util.pm: add new sendmail_pipe and mailer_pipe utility functions Currently /usr/bin/mail is hard-coded in many places as the executable to use when sending mail messages. As a first step to eliminating this, introduce a new sendmail_pipe function that opens a pipe to a new sendmail process with the passed in arguments. Although this change hard-codes /usr/sbin/sendmail as the executable, this is a first step to making it configurable as it only then need be changed in a single location. Next introduce a mailer_pipe function that uses sendmail_pipe to simulate basic support for /usr/bin/mail semantics (only the -s option and recipients are supported). --- Girocco/Util.pm | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Girocco/Util.pm b/Girocco/Util.pm index d0dde00..6ece9c1 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -8,7 +8,7 @@ use Time::Local; BEGIN { use base qw(Exporter); - our @EXPORT = qw(scrypt jailed_file + our @EXPORT = qw(scrypt jailed_file sendmail_pipe mailer_pipe lock_file unlock_file valid_tag rand_adjust filedb_atomic_append filedb_atomic_edit filedb_grep filedb_atomic_grep valid_email valid_email_multi @@ -459,4 +459,42 @@ sub rand_adjust { return $input + int(rand(0.25 * $input)); } +# Open a pipe to a new sendmail process. The '-i' option is always passed to +# the new process followed by any addtional arguments passed in. Note that +# the sendmail process is only expected to understand the '-i', '-t' and '-f' +# options. Using any other options via this function is not guaranteed to work. +# A list of recipients may follow the options. Combining a list of recipients +# with the '-t' option is not recommended. +sub sendmail_pipe { + return undef unless @_; + my $result = open(my $pipe, '|-', '/usr/sbin/sendmail', '-i', @_); + return $result ? $pipe : undef; +} + +# Open a pipe that works similarly to a mailer such as /usr/bin/mail in that +# if the first argument is '-s', a subject line will be automatically added +# (using the second argument as the subject). Any remaining arguments are +# expected to be recipient addresses that will be added to an explicit To: +# line as well as passed on to sendmail_pipe. In addition an +# "Auto-Submitted: auto-generated" header is always added as well as a suitable +# "From:" header. +sub mailer_pipe { + my $subject = undef; + if (@_ >= 2 && $_[0] eq '-s') { + shift; + $subject = shift; + } + my $pipe = sendmail_pipe(@_); + if ($pipe) { + print $pipe "From: \"$Girocco::Config::name\" ", + "($Girocco::Config::title) ", + "<$Girocco::Config::admin>\n"; + print $pipe "To: ", join(", ", @_), "\n"; + print $pipe "Subject: $subject\n" if defined($subject); + print $pipe "Auto-Submitted: auto-generated\n"; + print $pipe "\n"; + } + return $pipe; +} + 1; -- 2.11.4.GIT