Mail::EXPN - Perl extension for validation of email addresses


Mail-EXPN documentation Contained in the Mail-EXPN distribution.

Index


Code Index:

NAME

Top

Mail::EXPN - Perl extension for validation of email addresses

SYNOPSIS

Top

  use Mail::EXPN qw(isfake);

  $reason = isfake('bill@microsoft.com');
  if ($reason) {
    print "Bad email: $reason\n";
  } elsif (defined($reason)) {
    print "Email address perfect\n";
  } else {
    print "Could not verify email address: EXPN is turned off at target computer";
  }

  $reason = isfake('bigboss', 'mail.acme.com');
  ...

DESCRIPTION

Top

This module checks validity of email addresses. It ensure the existence of a username and domain, unless you specified the MTA, searches the DNS for the MTA (if not specified), and then attempts to use the SMTP keyword EXPN to verify the username. Since EXPN is usually turned off, the module will return undef in such cases, and defined but false if the verification passed. If for any reason the check failed, the module will return a string describing the reason.

CAVEATS

Top

Contemporary ISPs never turn EXPN on, to prevent mail abusers harass more efficiently by molesting only existing addresses with junk mail. Therefore, this is not an excellent solution to check the fill-out forms in your site for users supplying false email addresses. Most addresses associated with valid MTAs will return undef.

NOTE ON RFC 2821

Top

Mail::EXPN only checks the first mx specified, as it is likely to be the only one to contain the user list.

TO DO

Top

I tried to rewrite the module using Net::SMTP, but could not figure how to handle the expand and verify methods. They seemed to return an empty reply.

CREDITS

Top

Idea by Raz Information Systems, http://www.raz.co.il.

AUTHOR

Top

Ariel Brosh, schop@cpan.org.

MAINTAINER

Top

Florian Helmberger, florian@cpan.org.

VERSION

Top

$Id: EXPN.pm,v 1.3 2003/02/01 10:45:49 florian Exp $

SEE ALSO

Top

perl(1).


Mail-EXPN documentation Contained in the Mail-EXPN distribution.

# $Id: EXPN.pm,v 1.3 2003/02/01 10:45:49 florian Exp $

package Mail::EXPN;

use Net::DNS;
use Net::SMTP;
use IO::Socket;
require Exporter;
use strict;
use vars qw(@ISA @EXPORT_OK $BAD $VERSION $first);
@ISA = qw(Exporter);
@EXPORT_OK = qw(isfake $BAD);

$VERSION = '0.04';

$BAD = "SMTP response not understood";


sub isfake ($;$) {
	my @tokens = split(/\@/, shift);
	my $mx = shift;
	unless ($mx) {
		return 'not in user@host format' unless @tokens == 2;
		foreach (@tokens) {
			return 'contains illegal characters' if /[;()<>]/;
		}
		return 'malformed mail domain' unless ($tokens[1] =~ /\./);
		my @mx = mx($tokens[1]);
		return 'bogus mail domain' unless @mx;
		##@mx = sort { $b->preference <=> $a->preference} @mx;
		$mx = $mx[0]->exchange;
	}
	my $sock = new IO::Socket::INET("$mx:25") || return undef;
	my $result = step1($sock, join('@', @tokens));
	$sock->close;
	$result;
}

sub step1 {
	my ($sock, $email) = @_;
	return $BAD unless code($sock) == 220;
	$first = 1;
	out($sock, "HELO Mail-Check");
	return $BAD unless code($sock) == 250;
	out($sock, "EXPN $email");
	my $code = code($sock);
	return step2($sock, $email) if ($code == 502);
	return "" if ($code == 250);
	return "bogus username" if ($code == 550);
	return $BAD;
}

sub step2 {
	my ($sock, $email) = @_;
	out($sock, "VRFY $email");
	my $code = code($sock);
	return step3($sock, $email) if ($code == 252);
	return "bogus username" if ($code == 550);
	return "" if ($code == 250);
	return $BAD;
}

sub step3 {
	my ($sock, $email) = @_;
	out($sock, "MAIL FROM:<>");
	return $BAD unless code($sock) == 250;
	out($sock, "RCPT TO:<$email>");
	my $code = code($sock);
	return "bogus username" if ($code == 550);
	return "" if ($code == 250);
	return $BAD;
}

sub out ($$) {
	my ($sock, $text) = @_;
	$sock->send("$text\n");
}

sub code ($) {
	my ($sock) = @_;
	my $line = <$sock>;
	my @tokens = split(/[- ]+/, $line);
	my $ret = $tokens[0];
	return code($sock) if $first && $ret == 220;
	$first = undef;
	return $ret;
}

1;
__END__