文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用perl写的基于pop3和smtp的收发邮件程序

用perl写的基于pop3和smtp的收发邮件程序

时间:2008-01-13  来源:xp5211314

#!/usr/local/bin/perl -w
##基于SMTP的邮件发送程序##

use Net::SMTP_auth;

#get'the smtp server host.

print "please enter the SMTP server host!\n";
$mailhost=<>;
chomp ($mailhost);
#$mailhost= substr ($mailhost,1); #delete the "." in the front of $usernamein.

print $mailhost."\n\n";

#connect with SMTP server

$smtp = Net::SMTP_auth->new($mailhost) || die "Can't connect to SMTP Server";
print "The SMTP server is:".$smtp->banner()."\n";

#get'the username and password.

print "Please enter your Email username:\n";
$username=<>;
chomp($username);
print "Your Email username is : ".$username."\n\n";
print "Please enter your Email password:\n";
$passwd=<>;
chomp($passwd);
print "Your Email password is : ".$passwd."\n\n";

#generate your source address.

$saddress=$username."\@" ;
$saddress.=substr($mailhost,5);
$saddress=~s/@/\@/g;#regex expression, for example : xiaoping4220\@163.com.

print "Your Email address is : ".$saddress."\n\n";

#authentication

$num=$smtp->auth ('LOGIN',$username,$passwd);
#if ($num!=1)

#{

#     print "authenticate failed!\n";

#    exit;

#}

#else

#{    

#    print "authenticate sucessful!\n";

#}


#get'the destination address.

print "please enter the email destination address:\n";
$destaddress=<>;
chomp ($destaddress);
$destaddress=~s/@/\@/g; #regex expression, for example : xiaoping4220\@163.com.

print "The destination Email Address is : ".$destaddress."\n\n";

$smtp->mail($saddress);
$smtp->to($destaddress);
#mail data department.

$smtp->data();
$smtp->datasend("From: $saddress \n");
$smtp->datasend("To: $destaddress \n");
$smtp->datasend("Subject: perl mail test\n");
$smtp->datasend("\n");
$smtp->datasend("This is xiaoping's test email \n");
$smtp->datasend("Have a good day!\n");
$smtp->dataend();
$smtp->quit;

print "The mail has been sent successful!";
exit 0;

 

需要在cpan下载Net::SMTP_auth模块。

 

2.基于POP3的邮件收发程序

 

#!/usr/bin/perl

#date:8,1,2008
#author:xiaoping
#contact email:[email protected]

use Net::POP3;
#print "Please enter your Email Address:\n";
#$email = <>;
#chomp($email);

$email="pop.163.com";
#Constructor function.
$pop = Net::POP3->new($email)or die("ERROR: Unable to initiate. ");
print $pop->banner();

print "Please enter your Email username:\n";
$username=<>;
chomp($username);
#$username= substr ($usernamein,1);#delete the "." in the front of $usernamein.
print $username;

print "Please enter your Email password:\n";
$passwd=<>;
chomp($passwd);
print $passwd."\n";

# login in and gett the total number of mails.
$num = $pop->login($username,$passwd) or die("ERROR: Unable to login. ");

# gett the context of mails
if ($num>0){
   my $msgnums = $pop->list; # hashref of msgnum => size
      foreach my $msgnum (keys %$msgnums) {
        my $msg = $pop->get($msgnum);
        print @$msg;        
        #$pop->delete($msgnum);        
      }
 }
 $pop->quit;

相关阅读 更多 +
排行榜 更多 +
打螺丝高手

打螺丝高手

模拟经营 下载
解救火柴人计划安卓版

解救火柴人计划安卓版

体育竞技 下载
鸡生化精英安卓版

鸡生化精英安卓版

飞行射击 下载