| |||||||
|
|
Click here to download plain text source code.
# subroutine name: process_email_template # Input: template filename, # reference to hash tag_name=>tag_value # use admin's email for "from" if "from" stays empty # use admin's email for "to" if "to" stays empty # Processes <!--$tag_name--> tags and <!--%% "perl commands" %%--> tags inside # template file. <!--$tag_name--> tags are replaced with corresponding values. # Parses processed file and extracts mail headers "from", "to", "subject", # "bcc" and message body. # Returns: # ARRAY to be passed to &send_mail() function: ($from, $to, $subject, $body, $bcc) # # Global variables used: # %template_subst - hash for templates substitutions # %templates_cache - caching hash for small templates (<5000 bytes) # $templates_dir - filesystem path to templates dir # $admin - admin works with script (disable template substitution feature) # $admin_email - admin's email address sub process_email_template { local ($templatefile,$namespace, $admin_email_default_for_from, $admin_email_default_for_to) = @_; my $template = process_template($templatefile,$namespace); my %result; if ($template =~/^(.*?)\n\n(.*)$/s) { # Header found my $header = $1; $result{body} = $2; # Parse header line by line, and extract values for "from", "to", "subject" foreach (split(/[\x0A\x0D\n]+/,$header)) { if (m/^(subject|from|to|bcc)\s*:\s*(.*?)$/is) { my ($var, $value) = (lc($1), $2); # The only reserved meta-tag is "$admin_email" $value =~ s/\$admin_email/$admin_email/gs; $result{$var} = $value; } elsif (! m/^\s*$/s) { # Unknown header $result{body} = "Incorrect header in e-mail template !!! Can't parse line: '$_'.\n\n".$result{body}; } } } else { # Header not found $result{body} = $template; } $result{from} = $admin_email if ($result{from} eq "" && $admin_email_default_for_from); $result{to} = $admin_email if ($result{to} eq "" && $admin_email_default_for_to); return (@result{"from","to","subject","body"}, $result{bcc}?[split(/,/,$result{bcc})]:[]); } |
|||||||||||||||||||||
| Copyright © 1999-2007 Atomicsoft Ltd. All Rights Reserved. | ||