1#!perl 2 3use 5.008001; 4 5use strict; 6use warnings; 7 8use blib; 9use Getopt::Long; 10use Net::SMTP; 11 12=head1 NAME 13 14 smtp.self - mail a message via smtp 15 16=head1 DESCRIPTION 17 18C<smtp.self> will attempt to send a message to a given user 19 20=head1 OPTIONS 21 22=over 4 23 24=item -debug 25 26Enabe the output of dubug information 27 28=item -help 29 30Display this help text and quit 31 32=item -user USERNAME 33 34Send the message to C<USERNAME> 35 36=back 37 38=head1 EXAMPLE 39 40 demos/smtp.self -user foo.bar 41 42 demos/smtp.self -debug -user Graham.Barr 43 44=cut 45 46our $opt_debug = undef; 47our $opt_user = undef; 48our $opt_help = undef; 49GetOptions(qw(debug user=s help)); 50exec("pod2text $0") 51 if defined $opt_help; 52 53Net::SMTP->debug(1) if $opt_debug; 54 55my $smtp = Net::SMTP->new("mailhost"); 56 57my $user = $opt_user || $ENV{USER} || $ENV{LOGNAME}; 58 59$smtp->mail($user) && $smtp->to($user); 60$smtp->reset; 61 62if($smtp->mail($user) && $smtp->to($user)) 63 { 64 $smtp->data(); 65 66 my @data; 67 map { s/-USER-/$user/g } @data=<DATA>; ## no critic (ControlStructures::ProhibitMutatingListFunctions) 68 69 $smtp->datasend(@data); 70 $smtp->dataend; 71 } 72else 73 { 74 warn $smtp->message; 75 } 76 77$smtp->quit; 78 79__DATA__ 80To: <-USER-> 81Subject: A test message 82 83The message was sent directly via SMTP using Net::SMTP 84. 85The message was sent directly via SMTP using Net::SMTP 86