trAvis - MANAGER
Edit File: smtpauth-test
#!/usr/bin/perl eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; # not running under some shell # -*- coding: utf-8; mode:cperl -*- package TestOption; use Moose; use Moose::Util::TypeConstraints; with 'MooseX::Getopt'; has 'sender' => ( traits => [ 'Getopt' ], isa => 'Str', is => 'ro', cmd_aliases => 's', required => 1 ); has 'recipients' => ( traits => ['Array', 'Getopt' ], isa => 'ArrayRef[Str]', is => 'ro', required => 1, handles => { all => 'elements' }, cmd_aliases => 'r', ); has 'auth_id' => ( traits => [ 'Getopt' ], isa => 'Str', is => 'ro', required => 1, cmd_aliases => 'a', ); has 'password' => ( traits => [ 'Getopt' ], isa => 'Str', is => 'ro', required => 1, cmd_aliases => 'P' ); has 'hostname' => ( traits => [ 'Getopt' ], isa => 'Str', is => 'ro', default => '127.0.0.1', cmd_aliases => 'h' ); has 'port' => ( traits => [ 'Getopt' ], isa => 'Int', is => 'ro', default => 587, cmd_aliases => 'p' ); has 'is_continue' => ( traits => [ 'Getopt' ], isa => 'Bool', is => 'ro', default => 0, cmd_aliases => 'c' ); no Moose; no Moose::Util::TypeConstraints; __PACKAGE__->meta->make_immutable; package main; use strict; use warnings; use English; use Net::INET6Glue; use Net::SMTP; sub message { my ( $from, $to ) = @_; return [ "From: $from\r\n", "To: $to\r\n", "Subject: test\r\n", "\r\n", "test\r\n" ]; } my $option = TestOption->new_with_options(); my $smtp = new Net::SMTP( Host => $option->hostname(), Port => $option->port() ); if ( ! defined( $smtp ) ) { printf STDERR qq{cannot connect to %s:%s(%s)\n}, $option->hostname(), $option->port(), $ERRNO; exit( 1 ); } if ( ! $smtp->auth( $option->auth_id(), $option->password() ) ) { print STDERR qq{"AUTH ..." faiure.\n}; $smtp->quit(); exit( 0 ); } my $is_continue = $option->is_continue(); $SIG{INT} = sub { $is_continue = 0; }; do { foreach my $recipient ( $option->all() ) { if ( ! $smtp->mail( $option->sender() ) ) { printf STDERR qq{"Mail From: %s" faiure.\n}, $option->sender(); last; } if ( ! $smtp->recipient( $recipient ) ) { printf STDERR qq{"RCPT To: %s" faiure.\n}, $recipient; last; } if ( ! $smtp->data( message( $option->sender(), $recipient ) ) ) { print STDERR qq{"DATA ...." faiure.\n}; last; } printf STDERR qq{"From: %s, To: %s" Successed.\n}, $option->sender(), $recipient; } } while ( $is_continue ); $smtp->quit();