Show
Ignore:
Timestamp:
06/18/07 21:31:33 (19 months ago)
Author:
dom
Message:

Add send_email function (references #138)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/OpenGuides/Utils.pm

    r1070 r1092  
    1010use Wiki::Toolkit::Plugin::RSS::Reader; 
    1111use URI::Escape; 
     12use MIME::Lite; 
    1213 
    1314=head1 NAME 
     
    393394}; 
    394395 
     396=item B<send_email> 
     397 
     398    eval { OpenGuides::Utils->send_email( 
     399            config        => $config, 
     400            subject       => "Subject", 
     401            body          => "Test body", 
     402            admin         => 1, 
     403            nobcc         => 1, 
     404            return_output => 1 
     405    ) }; 
     406 
     407    if ($@) { 
     408        print "Error mailing admin: $@\n"; 
     409    } else { 
     410        print "Mailed admin\n"; 
     411    } 
     412 
     413Send out email. If C<admin> is true, the email will be sent to the site 
     414admin. If C<to> is defined, email will be sent to addresses in that 
     415arrayref. If C<nobcc> is true, there will be no Bcc to the admin. 
     416 
     417C<subject> and C<body> are mandatory arguments. 
     418 
     419Debugging: if C<return_output> is true, the message will be returned as 
     420a string instead of being sent by email. 
     421 
     422=cut 
     423 
     424sub send_email { 
     425    my ( $self, %args ) = @_; 
     426    my $config = $args{config} or die "config argument not supplied"; 
     427    my @to; 
     428    @to = @{$args{to}} if $args{to}; 
     429    my @bcc; 
     430    push @to, $config->contact_email if $args{admin}; 
     431    die "No recipients specified" unless scalar @to; 
     432    die "No subject specified" unless $args{subject}; 
     433    die "No body specified" unless $args{body}; 
     434    my $to_str = join ',', @to; 
     435    push @bcc, $config->contact_email unless $args{nobcc}; 
     436    my $bcc_str = join ',', @bcc; 
     437    my $msg = MIME::Lite->new( 
     438        From    => $config->contact_email, 
     439        To      => $to_str, 
     440        Bcc     => $bcc_str, 
     441        Subject => $args{subject}, 
     442        Data    => $args{body} 
     443    ); 
     444 
     445    if ( $args{return_output} ) { 
     446        return $msg->as_string; 
     447    } else { 
     448        $msg->send or die "Couldn't send mail!"; 
     449    } 
     450} 
     451 
    395452=back 
    396453