精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● CGI>>编程技巧>>利用perl程序发送smtp邮件的程序代码

主题:利用perl程序发送smtp邮件的程序代码
发信人: syeffy(☆吹吹峰☆)
整理人: workingnow(2002-09-30 11:47:11), 站内信件
#########声明,为转贴,发现了好的文章,希望大家分享############

################################################################# 
  # 
  # 描述: 
  # 发送stmp电子邮件 
  # 
  # 内容提要: 
  # 有关smtp电子邮件的协议是RFC 821,这个协议的具体内容在: 
  # http://pubweb.nexor.co.uk/public/rfc/rfcs/rfc821.txt 
  # 设置变量 $verbose = 1 进入调试程序模式 
  # 
  # 使用方法: 
  # &smtp_send ( "mail.yourcompany.com", "Test Message", "[email protected]", 
  # "[email protected]", "[email protected]", 
  # "This is a test.\n\nHi.\n" ); 
  # 
  ################################################################# 
  $SMTP_PORT = 25; 
  $AF_INET = 2; 
  $SOCK_STREAM = 1; 
   
  #$verbose = 1; 
   
  # 
  # Send Internet mail 
  # 
  sub smtp_send 
  { 
   local ( $mailserver, $subject, $recip, $cc, $from, $text ) = @_; 
   
   $allrecip = $recip . " " . $cc; 
   @allrecips = split ( /[, ]+/, $allrecip ); 
   @recips = split ( /[, ]+/, $recip ); 
   if ( $verbose ) { 
   printf ( "mail server = %s\n", $mailserver ); 
   printf ( "subject = %s\n", $subject ); 
   printf ( "recip = %s\n", $recip ); 
   printf ( "from = %s\n", $from ); 
   printf ( "recips = @recips\n" ); 
   } 
   
   # smtp port is 25 
   $port = $SMTP_PORT; 
   
   $sockaddr = 'S n a4 x8'; 
   
   chop($hostname = `hostname`); 
   
   ($name,$aliases,$proto) = getprotobyname('tcp'); 
   ($name,$aliases,$port) = getservbyname($port,'tcp') 
   unless $port =~ /^\d+$/;; 
   ($name,$aliases,$type,$len,$thisaddr) = 
   gethostbyname($hostname); 
   ($name,$aliases,$type,$len,$thataddr) = gethostbyname($mailserver); 
   
   $this = pack($sockaddr, $AF_INET, 0, $thisaddr); 
   $that = pack($sockaddr, $AF_INET, $port, $thataddr); 
   
   # Make the socket filehandle. 
   socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "$0: cannot open socket"; 
   
   # Give the socket an address. 
   bind(S, $this) || die "$0: cannot bind socket"; 
   
   # Call up the server. 
   connect(S,$that) || die "$0: cannot connect socket"; 
   
   # Set socket to be command buffered. 
   select(S); $| = 1; select(STDOUT); 
   
   sleep 1; 
   
   # Read line that identitifes the sendmail 
   sysread ( S, $data, 200 ) || die "$0: unable to read response"; 
   
   # Some servers require us to say hello 
   $out = "HELO\r\n"; 
   syswrite ( S, $out, length ( $out ) ); 
   sysread ( S, $data, 200 ) || die "$0: unable to read response"; 
   
   # Tell who we are... 
   $out = "MAIL FROM: $from\r\n"; 
   syswrite ( S, $out, length ( $out ) ); 
   
   # we should get a return of "250 ..." 
   sysread ( S, $data, 200 ) || die "$0: unable to read response"; 
   ($return) = split ( / /, $data ); 
   if ( $return != 250 ) { 
   $error = $data; 
   return 1; 
   } 
   if ( $verbose ) { printf "sendmail: $data\n"; } 
   
   # Tell who we are sending to... 
   for ( $i = 0; $i < @allrecips; $i++ ) {
$out = "RCPT TO: $allrecips[$i]\r\n";
syswrite ( S, $out, length ( $out ) );
# we should get a return of "250 ..."
sysread ( S, $data, 200 ) || die "$0: unable to read response";
($return) = split ( / /, $data );
if ( $return != 250 ) {
$error = $data;
return 1;
}
if ( $verbose ) { printf "sendmail: $data\n"; }
}

# Begin data of message
$out = "DATA\r\n";
syswrite ( S, $out, length ( $out ) );
# we should get a return of "354 ..."
sysread ( S, $data, 200 ) || die "$0: unable to read response";
($return) = split ( / /, $data );
if ( $return != 354 ) {
$error = $data;
return 1;
}
if ( $verbose ) { printf "sendmail: $data\n"; }

# Include all "To:" line that lists all recipients for all to see.
$out = "To: @recips\r\n";
syswrite ( S, $out, length ( $out ) );
foreach $i (1 .. $#recip) {
$out = "\r\n\t$recip[$i]";
syswrite ( S, $out, length ( $out ) );
}

# Give a subject if we have one...
$out = "Subject: $subject\r\n";
syswrite ( S, $out, length ( $out ) );

# Redirect errors to someone...
#printf S "Errors-to: Your Name <[email protected]>\n"; 
   
   # Set the reply address to someone... 
   $out = "Subject: $subject\r\n"; 
   syswrite ( S, $out, length ( $out ) ); 
   
   # 
   # Print data (message text) 
   # End the message with ".\n" 
   # 
   $out = "$text\r\n.\r\n"; 
   syswrite ( S, $out, length ( $out ) ); 
   
   # we should get a return of "250 ..." 
   sysread ( S, $data, 200 ) || die "$0: unable to read response"; 
   ($return) = split ( / /, $data ); 
   if ( $return != 250 ) { 
   $error = $data; 
   return 1; 
   } 
   if ( $verbose ) { printf "sendmail: $data\n"; } 
   
   # Close connection. 
   $out = "quit\r\n"; 
   syswrite ( S, $out, length ( $out ) ); 
   close (S); 
   
   return 0; 
  } 


[关闭][返回]