diff --git a/VIRTUAL_VACATION/Contributions.txt b/VIRTUAL_VACATION/Contributions.txt index 699af0ed..7d4d6107 100644 --- a/VIRTUAL_VACATION/Contributions.txt +++ b/VIRTUAL_VACATION/Contributions.txt @@ -91,3 +91,9 @@ Additional authors: sending vacation mails, even if one or multiple of the recipients the alias points to has vacation currently active. +2022-10-20 Jan Kruis + Add configuration parameter $replace_from ,$replace_until and $date_format for the subroutine replace_string + the subroutine repalce_string replaces in the bodytext the text defined at $replace_from and $replace_until with the date of active from and active until in the format specified at $date_format. + + Add configuration parameter $account_check and $account_name if account_check is set it wil add de value of name of the mailbox in front of de email and it is shown as sender, + if no name is specified at the mailbox, the variable friendly-name is placed in front of the email and it is shown as sender otherwise the email will show as sender. diff --git a/VIRTUAL_VACATION/vacation.pl b/VIRTUAL_VACATION/vacation.pl index 5f362035..5498f614 100644 --- a/VIRTUAL_VACATION/vacation.pl +++ b/VIRTUAL_VACATION/vacation.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# Virtual Vacation 4.2 +# Virtual Vacation 4.2.1 # # See Contributions.txt for a list of contributions. # https://github.com/postfixadmin/postfixadmin/blob/master/VIRTUAL_VACATION/Contributions.txt @@ -31,6 +31,8 @@ use Try::Tiny; use Log::Log4perl qw(get_logger :levels); use File::Basename; use Net::DNS; +use Time::Piece; + # ========== begin configuration ========== # IMPORTANT: If you put passwords into this script, then remember @@ -81,6 +83,8 @@ our $smtp_authid = ''; # sasl_password: the password to use for auth; required if username is provided our $smtp_authpwd = ''; + + # This specifies the mail 'from' name which is shown to recipients of vacation replies. # If you leave it empty, the vacation mail will contain: # From: @@ -88,6 +92,11 @@ our $smtp_authpwd = ''; # From: Some Friendly Name our $friendly_from = ''; +# If accountname_check is set it will add de account name in front of the email "AccountName " if the accountname is not a empty string +# otherwise $friendly_name will be set in front of the email if not empty. +our $accountname_check = 0; +our $account_name = ''; # leave this blank it wil be filled with 'name' field form tabel 'mailbox' + # Set to 1 to enable logging to syslog. our $syslog = 0; @@ -124,6 +133,21 @@ our $noreply_pattern = 'bounce|do-not-reply|facebook|linkedin|list-|myspace|twit # our $no_vacation_pattern = 'info\@example\.org'; our $no_vacation_pattern = 'info\@example\.org'; +# +# The subroutine repalce_string replaces in the body text the text defined at $replace_from and $replace_until with the date of activefrom and activeunitul in the format specified at $date_format. +# +# Like : +# +# %Y/%m/%d => 2022/10/01 +# %d-%m-%Y => 01-10-2022 +# %d %b %y => 01 Oct 2022 +# +# see for more option +# https://www.tutorialspoint.com/perl/perl_date_time.htm + +our $replace_from = "<%From_Date>"; #You can place your own replacement text here for active from +our $replace_until = "<%Until_Date>"; #You can place your own replacement text here for active until +our $date_format = '%Y-%m-%d'; # instead of changing this script, you can put your settings to /etc/mail/postfixadmin/vacation.conf # or /etc/postfixadmin/vacation.conf just use Perl syntax there to fill the variables listed above @@ -332,9 +356,67 @@ sub check_for_vacation { return $rv; } +# +# Get Accountname stored in name of table mailbox +# +sub get_accountname { + my ($from_mailbox) =@_; + my $logger = get_logger(); -# try and determine if email address has vacation turned on; we -# have to do alias searching, and domain aliasing resolution for this. + my $query = qq{SELECT name FROM mailbox WHERE username=? }; + my $stm = $dbh->prepare($query) or panic_prepare($query); + $stm->execute($to) or panic_execute($query,"username='$from_mailbox'"); + my @row = $stm->fetchrow_array; + my $rv = $stm->rows; + + my $accountname = $row[0]; + + return $accountname; +} + +# +# Replace <%From_Date> with date part of activefrom out the vacation table on base of email +# Replace <%Until_Date> with date part of activeuntil out the vacation table on base of email +# +# The varibale $replace_form and $replace_until will have the <%From_Date> and <%Until_Date> replace text + +sub replace_string { + my ($to) =@_; + my $logger = get_logger(); + + my $query = qq{SELECT body,activefrom,activeuntil FROM vacation WHERE email=? }; + my $stm = $dbh->prepare($query) or panic_prepare($query); + $stm->execute($to) or panic_execute($query,"email='$to'"); + my @row = $stm->fetchrow_array; + my $rv = $stm->rows; + + my $vacation_body = $row[0]; + my $f_date = substr($row[1],0,10); + my $u_date = substr($row[2],0,10); +# +# Note !! do not replace '%Y-%m-%d' with date_format because this is the format that f_date and u_date are are filled with date in this format +# $date_formart is used to diplay the dates in your choice of format. +# + my $date_f = Time::Piece->strptime($f_date,'%Y-%m-%d'); + $f_date = $date_f->strftime($date_format); + my $date_u = Time::Piece->strptime($u_date,'%Y-%m-%d'); + $u_date = $date_u->strftime('$date_format'); + + $vacation_body = replace_a_string($vacation_body,$replace_from,$f_date); + $vacation_body = replace_a_string($vacation_body,$replace_until,$u_date); + + $logger->debug ("From = $f_date Until = $u_date ** for Email = $to Body = $vacation_body "); + + return $vacation_body; +} + +sub replace_a_string { + my ( $result,$what_goes_out,$what_goes_in) =@_; + $result =~ s/$what_goes_out/$what_goes_in/ig; + return $result; +} +# try and determine if email address has vacation turned on; +# we have to do alias searching, and domain aliasing resolution for this. # If found, return ($num_matches, $real_email); sub find_real_address { my ($email) = @_; @@ -459,6 +541,9 @@ sub send_vacation_email { my $body = $row[1]; $body = Encode::decode_utf8($body) if (!Encode::is_utf8($body)); + ## Replace <%Time> marks in the Body string with dates form the table vacation + $body = replace_string ($email); + my $from = $email; my $to = $orig_from; @@ -498,14 +583,25 @@ sub send_vacation_email { my $transport = Email::Sender::Transport::SMTP->new($smtp_params); - $subject = Encode::encode_utf8($subject) if(Encode::is_utf8($subject)); - $body = Encode::encode_utf8($body) if(Encode::is_utf8($body)); +# QUESTION !! +#I think the two lines below (which I have commented out in) are no longer necessary, they appear earlier in the script see line 490 and 498 +# +# --> $subject = Encode::encode_utf8($subject) if(Encode::is_utf8($subject)); +# --> $body = Encode::encode_utf8($body) if(Encode::is_utf8($body)); +# + my $email_from = $from ; + my $account_name = get_accountname($from); - my $email_from = $from; - if($friendly_from ne '') { - $email_from = encode_mimewords($friendly_from, 'Charset', 'UTF-8') . " $from <$from>"; + if ($friendly_from ne'') { + $email_from = encode_mimewords($friendly_from, 'Charset', 'UTF-8') . " <$from>"; } + if (($accountname_check ==1) and ($account_name ne '')) { + $email_from = encode_mimewords($account_name, 'Charset', 'UTF-8') . " <$from>"; + } + + $logger->debug("** From = $from Email_from = $email_from Friendly_name = $friendly_from Accountname = $account_name **\n"); + $email = Email::Simple->create( header => [ To => $to, @@ -531,7 +627,7 @@ sub send_vacation_email { if (@_) { $logger->error("Failed to send vacation response to $to from $from subject $subject: @_"); } else { - $logger->debug("Vacation response sent to $to from $from subject $subject sent\n"); + $logger->debug("Vacation response sent to $to from $from subject $subject Email $email_from sent\n"); } } } @@ -606,6 +702,7 @@ sub check_and_clean_from_address { #$logger->debug("Address cleaned up to $address"); return $address; } + ########################### main ################################# # Take headers apart