App-mailmake

 view release on metacpan or  search on metacpan

scripts/mailmake  view on Meta::CPAN

    $smtp_opts{Debug}    = 1                      if( $DEBUG >= 3 );

    _message( 2, "Sending via <green>$opts->{smtp_host}</>." );
    my $rcpts = $mail->smtpsend( %smtp_opts );
    unless( defined( $rcpts ) )
    {
        _message( 1, "<red>Delivery failed:</> ", $mail->error );
        return(0);
    }
    my @addrs = ref( $rcpts ) eq 'ARRAY' ? @{$rcpts} : ( $rcpts );
    unless( $opts->{quiet} )
    {
        _message( 1, "Message accepted for: <green>", join( ', ', @addrs ), "</>" );
    }
    return(1);
}

sub _cleanup_and_exit
{
    my $exit = shift( @_ );
    $exit = 0 if( !length( $exit // '' ) || $exit !~ /^\d+$/ );
    exit( $exit );
}

sub _die
{
    my $msg = join( '', @_ );
    _message( "<red>$msg</>" );
    die( $msg . "\n" );
}

sub _message
{
    my $required_level;
    if( $_[0] =~ /^\d{1,2}$/ )
    {
        $required_level = shift( @_ );
    }
    else
    {
        $required_level = 0;
    }
    return if( !$LOG_LEVEL || $LOG_LEVEL < $required_level );
    my $msg = join( '', map( ref( $_ ) eq 'CODE' ? $_->() : $_, @_ ) );
    if( index( $msg, '</>' ) != -1 )
    {
        $msg =~ s
        {
            <([^\>]+)>(.*?)<\/>
        }
        {
            my $colour = $1;
            my $txt = $2;
            my $obj = color( $txt );
            my $code = $obj->can( $colour ) ||
                die( "Colour '$colour' is unsupported by Term::ANSIColor::Simple" );
            $code->( $obj );
        }gexs;
    }
    my $frame = 0;
    my( $pkg, $file, $line ) = caller( $frame );
    my $sub = ( caller( $frame + 1 ) )[3] // '';
    my $sub2;
    if( length( $sub ) )
    {
        $sub2 = substr( $sub, rindex( $sub, '::' ) + 2 );
    }
    else
    {
        $sub2 = 'main';
    }
    return( $err->print( "${pkg}::${sub2}() [$line]: $msg\n" ) );
}

sub _signal_handler
{
    my( $sig ) = @_;
    &_message( "Caught a $sig signal, terminating process $$" );
    if( uc( $sig ) eq 'TERM' )
    {
        &_cleanup_and_exit(0);
    }
    else
    {
        &_cleanup_and_exit(1);
    }
}

# NOTE: POD
__END__

=encoding utf-8

=pod

=head1 NAME

mailmake - Build and send RFC 2822 / MIME email from the command line

=head1 SYNOPSIS

    # Plain-text message
    mailmake --from alice@example.com --to bob@example.com \
             --subject "Hello" --plain "Hi Bob." \
             --smtp-host mail.example.com

    # HTML + plain text (alternative) with attachment
    mailmake --from alice@example.com --to bob@example.com \
             --subject "Report" \
             --plain-file body.txt --html-file body.html \
             --attach report.pdf \
             --smtp-host mail.example.com --smtp-port 587 --smtp-starttls \
             --smtp-user alice@example.com --smtp-password secret

    # Print the raw RFC 2822 message instead of sending
    mailmake --from alice@example.com --to bob@example.com \
             --subject "Test" --plain "Test" --print

    # OpenPGP detached signature
    mailmake --from alice@example.com --to bob@example.com \
             --subject "Signed" --plain "Signed message." \
             --gpg-sign --gpg-key-id FINGERPRINT \



( run in 0.553 second using v1.01-cache-2.11-cpan-5837b0d9d2c )