Author: dom Date: 2007-06-18 21:31:33 +0100 (Mon, 18 Jun 2007) New Revision: 1092
Added: trunk/t/77_send_email.t Modified: trunk/Build.PL trunk/MANIFEST trunk/PREREQUISITES trunk/lib/OpenGuides/Utils.pm Log: Add send_email function (references #138)
Modified: trunk/Build.PL =================================================================== --- trunk/Build.PL 2007-06-18 15:20:16 UTC (rev 1091) +++ trunk/Build.PL 2007-06-18 20:31:33 UTC (rev 1092) @@ -294,6 +294,7 @@ 'Geography::NationalGrid' => 0, 'HTML::Entities' => 0, 'LWP::Simple' => 0, + 'MIME::Lite' => 0, 'Parse::RecDescent' => 0, $search_module => 0, 'POSIX' => 0,
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2007-06-18 15:20:16 UTC (rev 1091) +++ trunk/MANIFEST 2007-06-18 20:31:33 UTC (rev 1092) @@ -140,5 +140,6 @@ t/74_ping_plugin.t t/75_revert_user.t t/76_detect_spam.t +t/77_send_email.t t/templates/15_test.tt wiki.cgi
Modified: trunk/PREREQUISITES =================================================================== --- trunk/PREREQUISITES 2007-06-18 15:20:16 UTC (rev 1091) +++ trunk/PREREQUISITES 2007-06-18 20:31:33 UTC (rev 1092) @@ -35,6 +35,7 @@ ) HTML::Entities LWP::Simple +MIME::Lite Module::Build (version 0.26 or later) Parse::RecDescent Plucene
Modified: trunk/lib/OpenGuides/Utils.pm =================================================================== --- trunk/lib/OpenGuides/Utils.pm 2007-06-18 15:20:16 UTC (rev 1091) +++ trunk/lib/OpenGuides/Utils.pm 2007-06-18 20:31:33 UTC (rev 1092) @@ -9,6 +9,7 @@ use Wiki::Toolkit::Formatter::UseMod; use Wiki::Toolkit::Plugin::RSS::Reader; use URI::Escape; +use MIME::Lite;
=head1 NAME
@@ -392,6 +393,62 @@
};
+=item B<send_email> + + eval { OpenGuides::Utils->send_email( + config => $config, + subject => "Subject", + body => "Test body", + admin => 1, + nobcc => 1, + return_output => 1 + ) }; + + if ($@) { + print "Error mailing admin: $@\n"; + } else { + print "Mailed admin\n"; + } + +Send out email. If C<admin> is true, the email will be sent to the site +admin. If C<to> is defined, email will be sent to addresses in that +arrayref. If C<nobcc> is true, there will be no Bcc to the admin. + +C<subject> and C<body> are mandatory arguments. + +Debugging: if C<return_output> is true, the message will be returned as +a string instead of being sent by email. + +=cut + +sub send_email { + my ( $self, %args ) = @_; + my $config = $args{config} or die "config argument not supplied"; + my @to; + @to = @{$args{to}} if $args{to}; + my @bcc; + push @to, $config->contact_email if $args{admin}; + die "No recipients specified" unless scalar @to; + die "No subject specified" unless $args{subject}; + die "No body specified" unless $args{body}; + my $to_str = join ',', @to; + push @bcc, $config->contact_email unless $args{nobcc}; + my $bcc_str = join ',', @bcc; + my $msg = MIME::Lite->new( + From => $config->contact_email, + To => $to_str, + Bcc => $bcc_str, + Subject => $args{subject}, + Data => $args{body} + ); + + if ( $args{return_output} ) { + return $msg->as_string; + } else { + $msg->send or die "Couldn't send mail!"; + } +} + =back
=head1 AUTHOR
Added: trunk/t/77_send_email.t =================================================================== --- trunk/t/77_send_email.t (rev 0) +++ trunk/t/77_send_email.t 2007-06-18 20:31:33 UTC (rev 1092) @@ -0,0 +1,44 @@ +use strict; +use OpenGuides::Config; +use OpenGuides::Utils; +use Test::More; + +plan tests => 6; + +my $config = OpenGuides::Config->new( + vars => { + contact_email => 'admin@example.com' + } +); + +my $output = OpenGuides::Utils->send_email( + config => $config, + return_output => 1, + to => [ 'user@example.com' ], + subject => 'Test subject', + body => 'Test body' +); + +like( $output, qr|^From: admin@example.com|m, "From address shown" ); +like( $output, qr|^To: user@example.com|m, "To address shown correctly" ); +like( $output, qr|^Subject: Test subject|m, "Subject shown correctly" ); +like( $output, qr|^Test body|m, "Body text appears at the start of a line" ); + +$output = OpenGuides::Utils->send_email( + config => $config, + return_output => 1, + admin => 1, + subject => 'Test subject', + body => 'Test body' +); + +like( $output, qr|^To: admin@example.com|m, "Admin address appropriately" ); + +eval { $output = OpenGuides::Utils->send_email( + config => $config, + return_output => 1, + subject => 'Test subject', + body => 'Test body' +); }; + +like( $@, qr|No recipients specified|, "No recipients error caught" );
openguides-commits@lists.openguides.org