Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

SSL mail with auth

by zentara (Archbishop)
on Jul 26, 2007 at 14:32 UTC ( #628943=sourcecode: print w/replies, xml ) Need Help??
Category: E-MAIL Programs
Author/Contact Info zentara@zentara.net
Description: My isp just upgraded it's mail security to use ssl for both pop and smtp. Here are 2 scripts to connect to the pop-ssl and smtp-ssl-auth.
#!/usr/bin/perl
# pop3 ssl client
use warnings;
use strict;
use Mail::POP3Client;
use IO::Socket::SSL;

 my $socket = IO::Socket::SSL->new( 
                PeerAddr => 'pop.some.net',
                PeerPort => 995,
                Proto    => 'tcp') || die "No socket!";

 my $pop = Mail::POP3Client->new(debug=>1);
 $pop->User('me@some.net');
 $pop->Pass('somepass');
 $pop->Socket($socket);
 $pop->Connect();

 for(my $i = 1; $i <= $pop->Count(); $i++ ) {
   foreach( $pop->Head( $i ) ) {
     /^(From|Subject):\s+/i && print $_, "\n";
   }
 }
 $pop->Close();

#or without IO::Socket::SSL (less port choices)
# my $pop = new Mail::POP3Client( 
#                  #DEBUG    => 1,
#                  USER     => 'me@some.net',
#                  PASSWORD => 'somepass',
#                  HOST     => 'pop.some.net',
#                  USESSL   => 1, # defaults to port 995
#                  
#                );

__END__

#############################################

#!/usr/bin/perl

# smtp-ssl with auth 
use warnings;
use strict;
use Net::SMTP::SSL; 

my $user = 'user@yaya.net';
my $pass = 'somepass';

$server     = 'your-smtp-server';
my $to         = 'friend@some.net';
my $from_name  = 'me';
my $from_email = 'user@yaya.net';
my $subject    = 'smtp-ssl-auth test';

my $smtps = Net::SMTP::SSL->new($server, 
                               Port => 465,
                               DEBUG => 1,
                               ) or warn "$!\n"; 

# I just lucked out and this worked for auth (yeah inheritance :-) )
defined ($smtps->auth($user, $pass))
    or die "Can't authenticate: $!\n";


$smtps->mail($from_email);
$smtps->to($to);
$smtps->data();
$smtps->datasend("To: $to\n");
$smtps->datasend(qq^From: "$from_name" <$from_email>\n^);
$smtps->datasend("Subject: $subject\n\n");
$smtps->datasend("This will be the body of the message.\n");
$smtps->datasend("\n--\nVery Official Looking .sig here\n");
$smtps->dataend();
$smtps->quit();

print "done\n";

#You can alternatively just put everything in the 
#argument to $smtps->data(),
#and forget about datasend() and dataend();
__END__
Replies are listed 'Best First'.
Re: SSL mail with auth
by kevyt (Scribe) on Apr 07, 2010 at 07:13 UTC
    I'm getting this error on the smtp-ssl with auth code. Can't call method "auth" on an undefined value at ***.pl line 21.
      yeah, me too

        I got the same issue. To get the auth working properly, I needed to install MIME::Base64 and Authen::SASL.

        Hope this helps...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://628943]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-07-28 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?