| | 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 | |
| | 413 | Send out email. If C<admin> is true, the email will be sent to the site |
| | 414 | admin. If C<to> is defined, email will be sent to addresses in that |
| | 415 | arrayref. If C<nobcc> is true, there will be no Bcc to the admin. |
| | 416 | |
| | 417 | C<subject> and C<body> are mandatory arguments. |
| | 418 | |
| | 419 | Debugging: if C<return_output> is true, the message will be returned as |
| | 420 | a string instead of being sent by email. |
| | 421 | |
| | 422 | =cut |
| | 423 | |
| | 424 | sub 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 | |