前提

 CPANにて以下3つのモジュールをインストールしておきます。
 ※Authen::SASLをインストールしないと、エラーは出ないがSMTP認証でのメール送信ができないという状態になります。
Net::SMTP
Authen::SASL
Net::POP3

メール送信(SMTP認証付き)

use strict;
use warnings;
use Net::SMTP;

my $username = $ARGV[0];
my $password = $ARGV[1];
my $mailsend = $ARGV[2];
my $mailrcpt = $ARGV[3];
my $smtpserver = '192.168.1.26';
my $smtp;

print "$username\n";

$smtp = Net::SMTP->new($smtpserver, Port => 587) || die "Can\'t Connect to SMTP server\n";
$smtp->auth($username, $password)||die "Authentication Failed\n";
$smtp->mail($mailsend);
$smtp->to($mailrcpt);
$smtp->data();
$smtp->datasend("From: $mailsend\n");
$smtp->datasend("To: $mailrcpt\n");
$smtp->datasend("Subject: simple mail send test\n");
$smtp->datasend("\n");
$smtp->datasend("simple test\n");
$smtp->dataend();
$smtp->quit;

メール受信(POP3)

#!/usr/bin/perl

use strict;
use warnings;
use Net::POP3;

my $smtpserver = '192.168.1.26';
my $username = $ARGV[0];
my $password = $ARGV[1];

print "$username\n";

#access to pop server
my $pop = Net::POP3->new($smtpserver, Timeout=> 120) || die "Can\'t Connect to POP server\n";
$pop->login($username, $password) || die "Authentication failed\n";

my $list_href = $pop->list;

foreach my $msg_id (keys %$list_href){
my $message = $pop->get($msg_id); #get mail
print @$message;
$pop->delete($msg_id); #delete mail
}

$pop->quit;
最終更新:2010年12月26日 22:14