gary kuipers has asked for the wisdom of the Perl Monks concerning the following question:

I would approciate help on this program as it generates the following error (the email addresses are not real in this example):

RCPT TO:<validrecipient@validdomain.com>
501 Unknown Command

also

544 no valid RCPT address specified (the address is a valid one, I can send mail using hotmail, MS Outlook Express and Ximian, is it probably because of the 501 error above?)

additionally:
the $smtp->dataend(); generates a 501 unknown command, as does the $smtp->quit;

What am I doing wrong? Or has my ISP implemented ESMTP in a weird way so that Net::SMTP commands are not understood by their server?

Thanks!

Here's the program:
use Net::SMTP; sSendEmail('validrecipient@validdomain.com','validrecipient@validdomai +n.com','ssssssubject','bbbbbbody'); sub sSendEmail { my ($to,$from,$subject,@body) = @_; my $smtp = Net::SMTP->new('mail.validdomain.com', Debug => 1); die "Could not open SMTP connection $!" if (! defined $smtp); print "Connected to: ", $smtp->domain. "\n"; $smtp->auth('garykuipers@ondacom.biz','judge'); print "debug:before recipient\n"; $smtp->recipient($to); print "debug:after recipient\n"; $smtp->mail($from); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); foreach my $line (@body) { $smtp->datasend("$line\n"); } print "debug:before dataend\n"; $smtp->dataend(); print "debug:before quit\n"; $smtp->quit; print "debug:after quit\n"; print "Mail sent to: |$to|\n"; }

Edit by dws to use <code> tags

Replies are listed 'Best First'.
Re: Net::SMTP generates unexpected errors in Debug Mode
by pg (Canon) on Dec 22, 2002 at 20:41 UTC
    Are you sure the error code is 544, not 554? RFC2821 does not define any error code 544. 554 is a valid error code, and means "no valid rcpt", which fits this case.

    The problem is obviously with the email address itself. As you didn't provide the address (which is understandable), it is difficult to debug for you. However I still can provide you some suggestions:
    1. Don't surrounding your email address with <>
    2. If your valid address contains specialy characters, such like space, put them in quot, like: "A valid address"@valid.com. For example:
      sSendEmail('"a valid address"@valid.com'...
    3. I don't think this is a problem to you, as I saw you used single quot, but I put it here any way. If you use double quot, better be careful. If you say "abc@hij.com", Perl will try to resolve @hij for you. Even if @hij does not exist, this is still a problem, as Perl will resolve your email address to abc.com. If you don't have warnings truned on, all this happens quitely. (Of course, you can say abc\@hij.com)
    Update:
    1. poj is rigth about the sequence. According to RFC2821: "There are three steps to SMTP mail transactions. The transaction starts with a MAIL command which gives the sender identification. (In general, the MAIL command may be sent only when no mail transaction is in progress; see section 4.1.4.) A series of one or more RCPT commands(pg comment: In Net::SMTP, you don't need to call recipient multiple times, as that function accepts multiple recipients, and resolves them into multiple RCPT commands for you) follows giving the receiver information. Then a DATA command initiates transfer of the mail data and is terminated by the "end of mail" data indicator, which also confirms the transaction. "
    2. However I don't think you can remove the auth command. I don't think that's the problem any way, and if your server has SASL authentication turned on, it is not something you would be able to skip.
Re: Net::SMTP generates unexpected errors in Debug Mode
by poj (Abbot) on Dec 22, 2002 at 21:00 UTC
    I got your script to work by putting the mail($from) before the recipient($to) and removing the auth line. I trust your auth parameters are not real !
    poj
      That was it! It is the order in which the methods are invoked. Thanks!
        When in doubt, I often just telnet into the SMTP server (port 25) and try the commands manually:

        HELO domain.com
        MAIL FROM: user@domain.com
        RCPT TO: user@destination.com
        DATA
        ..<blah blah>

        Some SMTP servers are very lenient and some are very strict. Try your best to stick to the RFC and I think you'll be safe in any case:

        http://www.ietf.org/rfc/rfc0821.txt