BugMail.pm 22.6 KB
Newer Older
1
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
2
#
3 4 5 6 7 8 9 10 11 12
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
terry%netscape.com's avatar
terry%netscape.com committed
13
# The Original Code is the Bugzilla Bug Tracking System.
14
#
terry%netscape.com's avatar
terry%netscape.com committed
15
# The Initial Developer of the Original Code is Netscape Communications
16 17 18 19
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
20
# Contributor(s): Terry Weissman <terry@mozilla.org>,
21 22
#                 Bryce Nesbitt <bryce-mozilla@nextbus.com>
#                 Dan Mosedale <dmose@mozilla.org>
23
#                 Alan Raetz <al_raetz@yahoo.com>
24
#                 Jacob Steenhagen <jake@actex.net>
25
#                 Matthew Tuck <matty@chariot.net.au>
26 27
#                 Bradley Baetz <bbaetz@student.usyd.edu.au>
#                 J. Paul Reed <preed@sigkill.com>
28
#                 Gervase Markham <gerv@gerv.net>
29

30
use strict;
terry%netscape.com's avatar
terry%netscape.com committed
31

32
package Bugzilla::BugMail;
terry%netscape.com's avatar
terry%netscape.com committed
33

34 35 36 37 38
use base qw(Exporter);
@Bugzilla::BugMail::EXPORT = qw(
    PerformSubsts
);

39
use Bugzilla::Constants;
40
use Bugzilla::Config qw(:DEFAULT $datadir);
41
use Bugzilla::Util;
42

43 44 45
use Mail::Mailer;
use Mail::Header;

46 47 48 49 50 51 52 53 54
# We need these strings for the X-Bugzilla-Reasons header
# Note: this hash uses "," rather than "=>" to avoid auto-quoting of the LHS.
my %rel_names = (REL_ASSIGNEE          , "AssignedTo", 
                 REL_REPORTER          , "Reporter",
                 REL_QA                , "QAcontact",
                 REL_CC                , "CC",
                 REL_VOTER             , "Voter");

# This code is really ugly. It was a commandline interface, then it was moved.
55
# This really needs to be cleaned at some point.
56

57
my %nomail;
58

59 60 61 62 63 64 65
my $sitespec = '@'.Param('urlbase');
$sitespec =~ s/:\/\//\./; # Make the protocol look like part of the domain
$sitespec =~ s/^([^:\/]+):(\d+)/$1/; # Remove a port number, to relocate
if ($2) {
    $sitespec = "-$2$sitespec"; # Put the port number back in, before the '@'
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
# I got sick of adding &:: to everything.
# However, 'Yuck!'
# I can't require, cause that pulls it in only once, so it won't then be
# in the global package, and these aren't modules, so I can't use globals.pl
# Remove this evilness once our stuff uses real packages.
sub AUTOLOAD {
    no strict 'refs';
    use vars qw($AUTOLOAD);
    my $subName = $AUTOLOAD;
    $subName =~ s/.*::/::/; # remove package name
    *$AUTOLOAD = \&$subName;
    goto &$AUTOLOAD;
}

# This is run when we load the package
81
if (open(NOMAIL, '<', "$datadir/nomail")) {
82 83 84 85 86 87
    while (<NOMAIL>) {
        $nomail{trim($_)} = 1;
    }
    close(NOMAIL);
}

88 89 90 91
sub FormatTriple {
    my ($a, $b, $c) = (@_);
    $^A = "";
    my $temp = formline << 'END', $a, $b, $c;
92
^>>>>>>>>>>>>>>>>>>|^<<<<<<<<<<<<<<<<<<<<<<<<<<<|^<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
END
    ; # This semicolon appeases my emacs editor macros. :-)
    return $^A;
}
    
sub FormatDouble {
    my ($a, $b) = (@_);
    $a .= ":";
    $^A = "";
    my $temp = formline << 'END', $a, $b;
^>>>>>>>>>>>>>>>>>> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
END
    ; # This semicolon appeases my emacs editor macros. :-)
    return $^A;
}

109 110 111 112 113 114 115 116 117
# This is a bit of a hack, basically keeping the old system()
# cmd line interface. Should clean this up at some point.
#
# args: bug_id, and an optional hash ref which may have keys for:
# changer, owner, qa, reporter, cc
# Optional hash contains values of people which will be forced to those
# roles when the email is sent.
# All the names are email addresses, not userids
# values are scalars, except for cc, which is a list
118
# This hash usually comes from the "mailrecipients" var in a template call.
119
sub Send($;$) {
120
    my ($id, $forced) = (@_);
121

122
    # This only works in a sub. Probably something to do with the
123 124 125
    # require abuse we do.
    GetVersionTable();

126
    return ProcessOneBug($id, $forced);
127 128
}

129 130
sub ProcessOneBug($$) {
    my ($id, $forced) = (@_);
131 132 133 134 135 136 137 138

    my @headerlist;
    my %values;
    my %defmailhead;
    my %fielddescription;

    my $msg = "";

139 140
    my $dbh = Bugzilla->dbh;
     
141 142 143 144 145 146 147 148 149 150 151 152 153 154
    SendSQL("SELECT name, description, mailhead FROM fielddefs " .
            "ORDER BY sortkey");
    while (MoreSQLData()) {
        my ($field, $description, $mailhead) = (FetchSQLData());
        push(@headerlist, $field);
        $defmailhead{$field} = $mailhead;
        $fielddescription{$field} = $description;
    }
    SendSQL("SELECT " . join(',', @::log_columns) . ", lastdiffed, now() " .
            "FROM bugs WHERE bug_id = $id");
    my @row = FetchSQLData();
    foreach my $i (@::log_columns) {
        $values{$i} = shift(@row);
    }
155 156 157
    $values{product} = get_product_name($values{product_id});
    $values{component} = get_component_name($values{component_id});

158
    my ($start, $end) = (@row);
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    # User IDs of people in various roles. More than one person can 'have' a 
    # role, if the person in that role has changed, or people are watching.
    my $reporter = $values{'reporter'};
    my @assignees = ($values{'assigned_to'});
    my @qa_contacts = ($values{'qa_contact'});
    my @ccs = @{$dbh->selectcol_arrayref("SELECT who 
                                          FROM cc WHERE bug_id = $id")};

    # Include the people passed in as being in particular roles.
    # This can include people who used to hold those roles.
    # At this point, we don't care if there are duplicates in these arrays.
    my $changer = $forced->{'changer'};
    if ($forced->{'owner'}) {
        push (@assignees, DBNameToIdAndCheck($forced->{'owner'}));
    }
175
    
176 177
    if ($forced->{'qacontact'}) {
        push (@qa_contacts, DBNameToIdAndCheck($forced->{'qacontact'}));
178
    }
179 180 181 182 183 184 185 186
    
    if ($forced->{'cc'}) {
        foreach my $cc (@{$forced->{'cc'}}) {
            push(@ccs, DBNameToIdAndCheck($cc));
        }
    }
    
    # Convert to names, for later display
187 188 189 190 191
    $values{'assigned_to'} = DBID_to_name($values{'assigned_to'});
    $values{'reporter'} = DBID_to_name($values{'reporter'});
    if ($values{'qa_contact'}) {
        $values{'qa_contact'} = DBID_to_name($values{'qa_contact'});
    }
192
    $values{'estimated_time'} = format_time_decimal($values{'estimated_time'});
193

194 195 196
    if ($values{'deadline'}) {
        $values{'deadline'} = time2str("%Y-%m-%d", str2time($values{'deadline'}));
    }
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    my @dependslist;
    SendSQL("SELECT dependson FROM dependencies WHERE 
             blocked = $id ORDER BY dependson");
    while (MoreSQLData()) {
        push(@dependslist, FetchOneColumn());
    }
    $values{'dependson'} = join(",", @dependslist);

    my @blockedlist;
    SendSQL("SELECT blocked FROM dependencies WHERE 
             dependson = $id ORDER BY blocked");
    while (MoreSQLData()) {
        push(@blockedlist, FetchOneColumn());
    }
    $values{'blocked'} = join(",", @blockedlist);

214 215
    my @diffs;

216 217 218
    # If lastdiffed is NULL, then we don't limit the search on time.
    my $when_restriction = $start ? 
        " AND bug_when > '$start' AND bug_when <= '$end'" : '';
219
    SendSQL("SELECT profiles.login_name, fielddefs.description, " .
220
            "       bug_when, removed, added, attach_id, fielddefs.name " .
221 222 223 224
            "FROM bugs_activity, fielddefs, profiles " .
            "WHERE bug_id = $id " .
            "  AND fielddefs.fieldid = bugs_activity.fieldid " .
            "  AND profiles.userid = who " .
225
            $when_restriction .
226 227 228 229 230 231 232 233 234
            "ORDER BY bug_when"
            );

    while (MoreSQLData()) {
        my @row = FetchSQLData();
        push(@diffs, \@row);
    }

    my $difftext = "";
235 236
    my $diffheader = "";
    my @diffparts;
237 238
    my $lastwho = "";
    foreach my $ref (@diffs) {
239
        my ($who, $what, $when, $old, $new, $attachid, $fieldname) = (@$ref);
240
        my $diffpart = {};
241 242
        if ($who ne $lastwho) {
            $lastwho = $who;
243 244 245
            $diffheader = "\n$who" . Param('emailsuffix') . " changed:\n\n";
            $diffheader .= FormatTriple("What    ", "Removed", "Added");
            $diffheader .= ('-' x 76) . "\n";
246
        }
247
        $what =~ s/^(Attachment )?/Attachment #$attachid / if $attachid;
248 249
        if( $fieldname eq 'estimated_time' ||
            $fieldname eq 'remaining_time' ) {
250 251
            $old = format_time_decimal($old);
            $new = format_time_decimal($new);
252
        }
253 254 255 256 257
        if ($attachid) {
            SendSQL("SELECT isprivate FROM attachments 
                     WHERE attach_id = $attachid");
            $diffpart->{'isprivate'} = FetchOneColumn();
        }
258 259 260 261 262
        $difftext = FormatTriple($what, $old, $new);
        $diffpart->{'header'} = $diffheader;
        $diffpart->{'fieldname'} = $fieldname;
        $diffpart->{'text'} = $difftext;
        push(@diffparts, $diffpart);
263 264
    }

265 266
    my $deptext = "";

267
    SendSQL("SELECT bugs_activity.bug_id, bugs.short_desc, fielddefs.name, " .
268
            "       removed, added " .
269
            "FROM bugs_activity, bugs, dependencies, fielddefs ".
270
            "WHERE bugs_activity.bug_id = dependencies.dependson " .
271
            "  AND bugs.bug_id = bugs_activity.bug_id ".
272 273 274 275
            "  AND dependencies.blocked = $id " .
            "  AND fielddefs.fieldid = bugs_activity.fieldid" .
            "  AND (fielddefs.name = 'bug_status' " .
            "    OR fielddefs.name = 'resolution') " .
276
            $when_restriction .
277 278 279 280 281
            "ORDER BY bug_when, bug_id");
    
    my $thisdiff = "";
    my $lastbug = "";
    my $interestingchange = 0;
282 283
    my $depbug = 0;
    my @depbugs;
284
    while (MoreSQLData()) {
285 286 287
        my ($summary, $what, $old, $new);
        ($depbug, $summary, $what, $old, $new) = (FetchSQLData());
        if ($depbug ne $lastbug) {
288 289 290
            if ($interestingchange) {
                $deptext .= $thisdiff;
            }
291
            $lastbug = $depbug;
292
            my $urlbase = Param("urlbase");
293
            $thisdiff =
294 295 296
              "\nBug $id depends on bug $depbug, which changed state.\n\n" . 
              "Bug $depbug Summary: $summary\n" . 
              "${urlbase}show_bug.cgi?id=$depbug\n\n"; 
297 298 299 300 301 302 303 304
            $thisdiff .= FormatTriple("What    ", "Old Value", "New Value");
            $thisdiff .= ('-' x 76) . "\n";
            $interestingchange = 0;
        }
        $thisdiff .= FormatTriple($fielddescription{$what}, $old, $new);
        if ($what eq 'bug_status' && IsOpenedState($old) ne IsOpenedState($new)) {
            $interestingchange = 1;
        }
305 306
        
        push(@depbugs, $depbug);
307
    }
308
    
309 310 311 312 313 314 315
    if ($interestingchange) {
        $deptext .= $thisdiff;
    }

    $deptext = trim($deptext);

    if ($deptext) {
316 317
        my $diffpart = {};
        $diffpart->{'text'} = "\n" . trim("\n\n" . $deptext);
318
        push(@diffparts, $diffpart);
319 320 321
    }


322
    my ($newcomments, $anyprivate) = GetLongDescriptionAsText($id, $start, $end);
323

324
    ###########################################################################
325
    # Start of email filtering code
326
    ###########################################################################
327
    
328 329 330 331 332 333 334 335 336 337
    # A user_id => roles hash to keep track of people.
    my %recipients;
    
    # Now we work out all the people involved with this bug, and note all of
    # the relationships in a hash. The keys are userids, the values are an
    # array of role constants.
    
    # Voters
    my $voters = 
          $dbh->selectcol_arrayref("SELECT who FROM votes WHERE bug_id = $id");
338
        
339
    push(@{$recipients{$_}}, REL_VOTER) foreach (@$voters);
340

341 342 343 344 345 346 347 348 349 350 351
    # CCs
    push(@{$recipients{$_}}, REL_CC) foreach (@ccs);
    
    # Reporter (there's only ever one)
    push(@{$recipients{$reporter}}, REL_REPORTER);
    
    # QA Contact
    if (Param('useqacontact')) {
        foreach (@qa_contacts) {
            # QA Contact can be blank; ignore it if so.
            push(@{$recipients{$_}}, REL_QA) if $_;
352
        }
353
    }
354

355 356
    # Assignee
    push(@{$recipients{$_}}, REL_ASSIGNEE) foreach (@assignees);
357

358 359 360 361 362 363 364 365
    # The last relevant set of people are those who are being removed from 
    # their roles in this change. We get their names out of the diffs.
    foreach my $ref (@diffs) {
        my ($who, $what, $when, $old, $new) = (@$ref);
        if ($old) {
            # You can't stop being the reporter, and mail isn't sent if you
            # remove your vote.
            if ($what eq "CC") {
366 367 368
                foreach my $cc_user (split(/[\s,]+/, $old)) {
                    push(@{$recipients{DBNameToIdAndCheck($cc_user)}}, REL_CC);
                }
369 370 371 372 373 374
            }
            elsif ($what eq "QAContact") {
                push(@{$recipients{DBNameToIdAndCheck($old)}}, REL_QA);
            }
            elsif ($what eq "AssignedTo") {
                push(@{$recipients{DBNameToIdAndCheck($old)}}, REL_ASSIGNEE);
375
            }
376
        }
377 378
    }
    
379 380 381 382 383 384 385 386 387 388 389 390 391
    if (Param("supportwatchers")) {
        # Find all those user-watching anyone on the current list, who is not 
        # on it already themselves.
        my $involved = join(",", keys %recipients);

        my $userwatchers = 
            $dbh->selectall_arrayref("SELECT watcher, watched FROM watch 
                                      WHERE watched IN ($involved)
                                      AND watcher NOT IN ($involved)");

        # Mark these people as having the role of the person they are watching
        foreach my $watch (@$userwatchers) {
            $recipients{$watch->[0]} = $recipients{$watch->[1]};
392 393
        }
    }
394 395 396 397 398 399
        
    # We now have a complete set of all the users, and their relationships to
    # the bug in question. However, we are not necessarily going to mail them
    # all - there are preferences, permissions checks and all sorts to do yet.
    my @sent;
    my @excluded;
400

401 402 403
    foreach my $user_id (keys %recipients) {
        my @rels_which_want;
        my $sent_mail = 0;
404

405
        my $user = new Bugzilla::User($user_id);
406
        
407
        if ($user->can_see_bug($id))
408
        {
409 410 411 412 413 414 415 416 417 418 419 420
            # Go through each role the user has and see if they want mail in
            # that role.
            foreach my $relationship (@{$recipients{$user_id}}) {
                if ($user->wants_bug_mail($id,
                                          $relationship, 
                                          \@diffs, 
                                          $newcomments, 
                                          $changer))
                {
                    push(@rels_which_want, $relationship);
                }
            }
421
        }
422
        
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        if (scalar(@rels_which_want)) {
            # So the user exists, can see the bug, and wants mail in at least
            # one role. But do we want to send it to them?

            # If we are using insiders, and the comment is private, only send 
            # to insiders
            my $insider_ok = 1;
            $insider_ok = 0 if (Param("insidergroup") && 
                                ($anyprivate != 0) && 
                                (!$user->groups->{Param("insidergroup")}));

            # We shouldn't send mail if this is a dependency mail (i.e. there 
            # is something in @depbugs), and any of the depending bugs are not 
            # visible to the user. This is to avoid leaking the summaries of 
            # confidential bugs.
            my $dep_ok = 1;
            foreach my $dep_id (@depbugs) {
                if (!$user->can_see_bug($dep_id)) {
                   $dep_ok = 0;
                   last;
                }
            }

            # Make sure the user isn't in the nomail list, and the insider and 
            # dep checks passed.
            if ((!$nomail{$user->login}) &&
                $insider_ok &&
                $dep_ok)
            {
                # OK, OK, if we must. Email the user.
                $sent_mail = sendMail($user, 
                                      \@headerlist,
                                      \@rels_which_want, 
                                      \%values,
                                      \%defmailhead, 
                                      \%fielddescription, 
                                      \@diffparts,
                                      $newcomments, 
                                      $anyprivate, 
                                      $start, 
                                      $id);
464
            }
465
        }
466 467 468 469 470 471 472 473
       
        if ($sent_mail) {
            push(@sent, $user->login); 
        } 
        else {
            push(@excluded, $user->login); 
        } 
    }
474
    
475
    $dbh->do("UPDATE bugs SET lastdiffed = '$end' WHERE bug_id = $id");
476

477
    return {'sent' => \@sent, 'excluded' => \@excluded};
478 479
}

480 481
sub sendMail($$$$$$$$$$$$) {
    my ($user, $hlRef, $relRef, $valueRef, $dmhRef, $fdRef,  
482
        $diffRef, $newcomments, $anyprivate, $start, 
483
        $id) = @_;
484

485 486
    my %values = %$valueRef;
    my @headerlist = @$hlRef;
487
    my %mailhead = %$dmhRef;
488
    my %fielddescription = %$fdRef;
489
    my @diffparts = @$diffRef;    
490 491 492 493 494
    my $head = "";
    
    foreach my $f (@headerlist) {
      if ($mailhead{$f}) {
        my $value = $values{$f};
495 496
        # If there isn't anything to show, don't include this header
        if (! $value) {
497 498
          next;
        }
499
        # Only send estimated_time if it is enabled and the user is in the group
500 501
        if (($f ne 'estimated_time' && $f ne 'deadline') ||
             $user->groups->{Param('timetrackinggroup')}) {
502

503 504 505
            my $desc = $fielddescription{$f};
            $head .= FormatDouble($desc, $value);
        }
506 507
      }
    }
508 509 510 511 512

    # Build difftext (the actions) by verifying the user should see them
    my $difftext = "";
    my $diffheader = "";
    my $add_diff;
513

514 515 516
    foreach my $diff (@diffparts) {
        $add_diff = 0;
        
517
        if (exists($diff->{'fieldname'}) && 
518 519
            ($diff->{'fieldname'} eq 'estimated_time' ||
             $diff->{'fieldname'} eq 'remaining_time' ||
520 521
             $diff->{'fieldname'} eq 'work_time' ||
             $diff->{'fieldname'} eq 'deadline')){
522
            if ($user->groups->{Param("timetrackinggroup")}) {
523 524
                $add_diff = 1;
            }
525 526 527 528 529
        } elsif (($diff->{'isprivate'}) 
                 && Param('insidergroup')
                 && !($user->groups->{Param('insidergroup')})
                ) {
            $add_diff = 0;
530 531 532
        } else {
            $add_diff = 1;
        }
533

534
        if ($add_diff) {
535 536
            if (exists($diff->{'header'}) && 
             ($diffheader ne $diff->{'header'})) {
537 538 539 540 541 542
                $diffheader = $diff->{'header'};
                $difftext .= $diffheader;
            }
            $difftext .= $diff->{'text'};
        }
    }
543
 
544 545
    if ($difftext eq "" && $newcomments eq "") {
      # Whoops, no differences!
546
      return 0;
547 548
    }
    
549 550
    # XXX: This needs making localisable, probably by passing the role to
    # the email template and letting it choose the text.
551
    my $reasonsbody = "------- You are receiving this mail because: -------\n";
552

553 554 555 556 557 558 559 560 561 562 563
    foreach my $relationship (@$relRef) {
        if ($relationship == REL_ASSIGNEE) {
            $reasonsbody .= "You are the assignee for the bug, or are watching the assignee.\n";
        } elsif ($relationship == REL_REPORTER) {
            $reasonsbody .= "You reported the bug, or are watching the reporter.\n";
        } elsif ($relationship == REL_QA) {
            $reasonsbody .= "You are the QA contact for the bug, or are watching the QA contact.\n";
        } elsif ($relationship == REL_CC) {
            $reasonsbody .= "You are on the CC list for the bug, or are watching someone who is.\n";
        } elsif ($relationship == REL_VOTER) {
            $reasonsbody .= "You are a voter for the bug, or are watching someone who is.\n";
564 565 566
        }
    }

567
    my $isnew = !$start;
568 569
    
    my %substs;
570

571 572 573 574 575 576
    # If an attachment was created, then add an URL. (Note: the 'g'lobal
    # replace should work with comments with multiple attachments.)

    if ( $newcomments =~ /Created an attachment \(/ ) {

        my $showattachurlbase =
577
            Param('urlbase') . "attachment.cgi?id=";
578

579
        $newcomments =~ s/(Created an attachment \(id=([0-9]+)\))/$1\n --> \(${showattachurlbase}$2&action=view\)/g;
580 581
    }

582
    $substs{"neworchanged"} = $isnew ? ' New: ' : '';
583
    $substs{"to"} = $user->email;
584 585 586 587 588 589 590
    $substs{"cc"} = '';
    $substs{"bugid"} = $id;
    if ($isnew) {
      $substs{"diffs"} = $head . "\n\n" . $newcomments;
    } else {
      $substs{"diffs"} = $difftext . "\n\n" . $newcomments;
    }
591 592
    $substs{"product"} = $values{'product'};
    $substs{"component"} = $values{'component'};
593
    $substs{"summary"} = $values{'short_desc'};
594
    $substs{"reasonsheader"} = join(" ", map { $rel_names{$_} } @$relRef);
595
    $substs{"reasonsbody"} = $reasonsbody;
596
    $substs{"space"} = " ";
597
    if ($isnew) {
598 599
        $substs{'threadingmarker'} = "Message-ID: <bug-$id-" . 
                                     $user->id . "$sitespec>";
600
    } else {
601 602
        $substs{'threadingmarker'} = "In-Reply-To: <bug-$id-" . 
                                     $user->id . "$sitespec>";
603
    }
604 605 606 607
    
    my $template = Param("newchangedmail");
    
    my $msg = PerformSubsts($template, \%substs);
608

609 610 611 612 613 614
    MessageToMTA($msg);

    return 1;
}

sub MessageToMTA ($) {
615
    my ($msg) = (@_);
616
    return if (Param('mail_delivery_method') eq "none");
617

618
    my @args;
619
    if (Param("mail_delivery_method") eq "sendmail" && !Param("sendmailnow")) {
620 621
        push @args, "-ODeliveryMode=deferred";
    }
622
    if (Param("mail_delivery_method") eq "smtp") {
623
        push @args, Server => Param("smtpserver");
624
    }
625 626
    my $mailer = new Mail::Mailer Param("mail_delivery_method"), @args;
    if (Param("mail_delivery_method") eq "testfile") {
627 628 629
        $Mail::Mailer::testfile::config{outfile} = "$datadir/mailer.testfile";
    }
    
630 631 632
    $msg =~ /(.*?)\n\n(.*)/ms;
    my @header_lines = split(/\n/, $1);
    my $body = $2;
633

634 635
    my $headers = new Mail::Header \@header_lines, Modify => 0;
    $mailer->open($headers->header_hashref);
636
    print $mailer $body;
637
    $mailer->close;
638 639
}

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
# Performs substitutions for sending out email with variables in it,
# or for inserting a parameter into some other string.
#
# Takes a string and a reference to a hash containing substitution 
# variables and their values.
#
# If the hash is not specified, or if we need to substitute something
# that's not in the hash, then we will use parameters to do the 
# substitution instead.
#
# Substitutions are always enclosed with '%' symbols. So they look like:
# %some_variable_name%. If "some_variable_name" is a key in the hash, then
# its value will be placed into the string. If it's not a key in the hash,
# then the value of the parameter called "some_variable_name" will be placed
# into the string.
sub PerformSubsts ($;$) {
    my ($str, $substs) = (@_);
    $str =~ s/%([a-z]*)%/(defined $substs->{$1} ? $substs->{$1} : Param($1))/eg;
    return $str;
}

661
1;