Changeset 1092


Ignore:
Timestamp:
Jun 18, 2007, 9:31:33 PM (14 years ago)
Author:
Dominic Hargreaves
Message:

Add send_email function (references #138)

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Build.PL

    r1088 r1092  
    295295        'HTML::Entities'                      => 0,
    296296        'LWP::Simple'                         => 0,
     297        'MIME::Lite'                          => 0,
    297298        'Parse::RecDescent'                   => 0,
    298299        $search_module                        => 0,
  • trunk/MANIFEST

    r1087 r1092  
    141141t/75_revert_user.t
    142142t/76_detect_spam.t
     143t/77_send_email.t
    143144t/templates/15_test.tt
    144145wiki.cgi
  • trunk/PREREQUISITES

    r1046 r1092  
    3636HTML::Entities
    3737LWP::Simple
     38MIME::Lite
    3839Module::Build (version 0.26 or later)
    3940Parse::RecDescent
  • 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
Note: See TracChangeset for help on using the changeset viewer.