2 # Copyright (C) 2009 Alex Schroeder <alex@gnu.org>
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
23 # This script can be invoked as follows:
24 # perl smtp_test.pl -m "alex:*secret*@mail.epfarms.org" \
25 # -f "kensanata@gmail.com"
27 # -m user:password@mailhost for sending email using SMTP Auth. Without this
28 # information, the script will send mail to localhost.
29 # -f email address to use as the sender and recipient.
33 die "Must provide an SMTP host using -m\n" unless $opts{m
};
34 $opts{m
} =~ /(.*?):(.*)\@(.*)/;
35 my ($user, $password, $host) = ($1, $2, $3);
36 die "Cannot parse -m " . $opts{m
} . "\n" unless $host;
38 die "Must provide sender using -f\n" if $host && !$from;
40 my ($fh, $filename) = File
::Temp
->new(SUFFIX
=> '.html', UNLINK
=> 1);
41 print $fh qq(<body
>This is a
<b
>test
</b
>!);
45 require Net
::SMTP
::TLS
;
46 my $mail = new MIME
::Entity
->build(To
=> $from, # test!
48 Subject
=> 'Test Net::SMTP::TLS',
51 my $smtp = Net
::SMTP
::TLS
->new($host,
53 Password
=> $password,
56 $smtp->to($from); # test!
58 $smtp->datasend($mail->stringify);
63 warn "Net::SMTP::TLS problem: $@" if $@
;
66 require Net
::SMTP
::SSL
;
67 my $mail = new MIME
::Entity
->build(To
=> $from, # test!
69 Subject
=> 'Test Net::SMTP::SSL',
72 my $smtp = Net
::SMTP
::SSL
->new($host, Port
=> 465,
74 $smtp->auth($user, $password);
76 $smtp->to($from); # test!
78 $smtp->datasend($mail->stringify);
83 warn "Net::SMTP::SSL problem: $@" if $@
;