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