App-MonM

 view release on metacpan or  search on metacpan

lib/App/MonM/Channel/Email.pm  view on Meta::CPAN

Default: undef

See L<Email::Sender::Transport::SMTP>

=item B<To>, B<Cc>, B<Bcc>

Recipient address (Email addresses)

=item B<Type>

    Type    Email

Required directive!

Defines type of channel. MUST BE set to "Email" value

=back

About common directives see L<App::MonM::Channel/CONFIGURATION DIRECTIVES>

=head1 HISTORY

See C<Changes> file

=head1 DEPENDENCIES

L<Email::MIME>, L<Email::Sender>, L<Net::SMTP>

=head1 TO DO

See C<TODO> file

=head1 SEE ALSO

L<Email::MIME>, L<Email::Sender>, L<Net::SMTP>

=head1 AUTHOR

Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>

=head1 COPYRIGHT

Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See C<LICENSE> file and L<https://dev.perl.org/licenses/>

=cut

use vars qw/$VERSION/;
$VERSION = '1.00';

use CTK::ConfGenUtil;
use CTK::TFVals qw/ :ALL /;
use Email::Sender::Simple qw//;
use Email::Sender::Transport::SMTP;
use Try::Tiny;

use App::MonM::Util qw/ set2attr /;

sub sendmsg {
    my $self = shift;
    return $self->maybe::next::method() unless $self->type eq 'email';
    my $message = $self->message;

    #printf "Send message %s to %s (%s) via %s\n", $self->message->msgid, $self->message->to, $self->message->recipient,  $self->type;
    #print App::MonM::Util::explain($self->chconf);

    # eXtra headers (extension headers)
    $message->email->header_str_set("X-Mailer" => sprintf("%s/%s", __PACKAGE__, $VERSION) );

    # SMTP Options
    my $options = set2attr($self->chconf) || {};
    #print App::MonM::Util::explain($options);

    # General
    my $try_sendmail_first = value($options, "host") ? 0 : 1;
    my $sent_status = 1;
    my $sent_error = "";

    # Try via sendmail
    if ($try_sendmail_first) {
        try {
            Email::Sender::Simple->send($message->email);
        } catch {
            $sent_status = 0;
            $sent_error = $_ || 'unknown sendmail error';
        };
        return 1 if $sent_status;
    }

    # Now send via SMTP
    $sent_status = 1;
    my $transport = Email::Sender::Transport::SMTP->new($options);
    try {
        Email::Sender::Simple->send($message->email, { transport => $transport });
    } catch {
        $sent_status = 0;
        $sent_error = $_ || 'unknown SMTP error';
    };
    return 1 if $sent_status;

    # Errors
    $self->error(sprintf("Can't send message: %s", $sent_error // "unknown error"));
    return 0;
}

1;

__END__



( run in 0.635 second using v1.01-cache-2.11-cpan-39bf76dae61 )