- 论坛徽章:
- 0
|
使用 雷傲论坛.的ESMTP发信功能,一直没有成功过. 想请教一下高手们? 错在什么地方?
是使用 Socket
论坛程序中有关发信的SUB
- sub smtpmail
- {
- eval("use Socket;");
- my ($address, $from, $replyaddr, $subject, $body, $extra) = @_;
- my ($a, $i, $name, $aliases, $proto, $type, $len, $thataddr);
- my @to = split(/, /, $address);
- foreach $i (@to)
- {
- #对地址进行解码
- my $AF_INET = 2;
- my $SOCK_STREAM = 1;
- my $SOCKADDR = 'S n a4 x8';
- ($name, $aliases, $proto) = getprotobyname('tcp');
- ($name, $aliases, $SMTP_PORT) = getservbyname($SMTP_PORT, 'tcp') unless ($SMTP_PORT =~ /^\d+$/);
- ($name, $aliases, $type, $len, $thataddr) = gethostbyname($SMTP_SERVER);
- my $this = pack($SOCKADDR, $AF_INET, 0, $thisaddr);
- my $that = pack($SOCKADDR, $AF_INET, $SMTP_PORT, $thataddr);
- #打开SMTP的socket端口
- socket(S, $AF_INET, $SOCK_STREAM, $proto);
- bind(S, $this);
- connect(S, $that);
- select(S);
- $| = 1;
- select(STDOUT);
- $a = "";
- $a = <S>;;
- if ($a !~ /^2/)
- {
- close(S);
- undef $|;
- return 0;
- }
- if ($extra eq "no")
- {#与普通SMTP服务器握手连接
- print S "HELO localhost\n";
- $a = <S>;;
- }
- else
- {#进行ESMTP身份验证
- print S "EHLO localhost\n";
- $a = <S>;;
- print S "AUTH LOGIN\n";
- $a = <S>;;
- my $encode_smtpuser = &Base64encode($SMTPUSER); #用来验证的用户名必须经过Base64编码后发往服务器
- print S "$encode_smtpuser\n";
- $a = <S>;;
- my $encode_smtppass = &Base64encode($SMTPPASS); #用来验证的密码必须经过Base64编码后发往服务器
- print S "$encode_smtppass\n";
- $a = <S>;;
- return 0 if ($a =~ /fail/i);
- }
- #发送邮件头部信息
- print S "MAIL FROM: <$from>;\n";
- $a = <S>;;
- print S "RCPT TO: <$i>;\n";
- $a = <S>;;
- #发送邮件正文
- print S "DATA\n";
- print S "From: $from\n";
- print S "To: $i\n";
- print S "Subject: $subject\n";
- print S "Reply-To: $replyaddr\n" if ($replyaddr);
- print S "X-Mailer: LeoBoard eSmtp Mail Sender\n";
- print S "Content-Type: text/html; charset=gb2312\n\n";
- print S $body;
- print S "\n";
- print S "\n\n";
- print S ".\n";
- $a = <S>;;
- print S "QUIT\n";
- $a = <S>;;
- close(S);
- undef $|;
- }
- return 1;
- }
- sub Base64encode
- #Base64编码函数
- {
- my $res = pack("u", $_[0]);
- $res =~ s/^.//mg;
- $res =~ s/\n//g;
- $res =~ tr|` -_|AA-Za-z0-9+/|;
- my $padding = (3 - length($_[0]) % 3) % 3;
- $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
- return $res;
- }
- 1;
复制代码
注: 空间不支持Sendmail 发信.[code][/code] |
|