Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Net::SMTP success / failure response

by Anonymous Monk
on Mar 31, 2004 at 08:26 UTC ( [id://341246]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to send some emails with Net::SMTP, and all goes well except that I can't figure out how to tell if the mail was sent successfully. I have to record which emails are real and which receive a "User Unknown" response.

I saw this post Net:SMTP Printing to STDOUT, and according to that, and the Net::SMTP manual and Net::Cmd manual, I should do this:

$smtp = Net::SMTP->new('smtp.host.com'); $smtp->mail('someone@example.com'); $smtp->to('someone@example.com'); my $ret = $smtp->data($maildata);

If my $ret value is true, it was a sucess. If false, the mail was not sent.

That works fine when the recipient's domain is handled by the same host as I am using as my SMTP relay (smtp.host.com) but if I am sending to a non-existing recipient on another host, I want to receive false, or a "User Unknown" message. Instead, it returns true.

Any ideas how I can figure out if the mail made it without parsing the maillog, and without doing a lookup on each mailaddress, and using their MX record instead of smtp.host.com ?

Replies are listed 'Best First'.
Re: Net::SMTP success / failure response
by matija (Priest) on Mar 31, 2004 at 08:51 UTC
    It can't be done that way.

    For instance, if the remote machine is down, your mail server will be retrying your mail (in increasingly large intervals) for the next week or so, long after your script has disconnected.

    There is no way to predict what the remote machine will say. In fact, in some cases the remote machine will accept the mail, and then it will send back a bounce.

    Most of the methods which enabled you to connect to a given machine and ascertain if an email address local to that machine is valid or not have been disabled, because spammers were using them to generate target email addresses.

      your mail server will be retrying your mail

      Not quite true. It is in the general case yes, but Net::SMTP handles the connection itself, and does not hand off to your MTA, so it will deliver the mail, or die in the attempt :) (update: oops, unless you connect to your MTA... but if you're connecting directly to their MTA, in the hope of collecting the response code and message, this would be the case).

      the remote machine will accept the mail, and then it will send back a bounce

      Most notably, aol.com does this. Their user base being the size it is, I assume they decided that it would be too difficult to validate addresses on the perimeter in a timely manner. So they accept it, and then they can take all the time they like to figure out whether it can be delivered. This means that they then have to deal with bogus return paths for bounced spam, but in their terms of use they do say that they will reject mail from your host if a certain percentage of bounces bounce back to them.

        Net::SMTP talks whatever server you tell it to. In 99% of the time, that is the local MTA that relays it to the destination server. Finding the destination server requires looking up the MX records and acting like an MTA.

        For this problem of finding which email addresses are valid, that may be a good solution.

      I realize that some cases will make it impossible, I am willing to accept that. I'm just looking for a best effort right now.

      If the machine accepts the mail, and then bounces it later, I'll count it as accepted until I receive the bounced mail.
Re: Net::SMTP success / failure response
by Happy-the-monk (Canon) on Mar 31, 2004 at 09:29 UTC

    You could save the data of all the mail the programme has ever sent stored.
    Make it acessible to another programme that monitors the incoming mailbox of your original programme's sender.
    That one learns about failure receipts and takes the action you feel necessary.

    Cheers, Sören

      I was trying to avoid such measures.

      Currently, I am parsing the servers maillog, but really wanted to make the program nice and neat, not having to do that.

Re: Net::SMTP success / failure response
by idsfa (Vicar) on Mar 31, 2004 at 17:27 UTC

    "Use the source, Luke"

    From Net::SMTP's source code:

    my @ok; my $addr; foreach $addr (@_) { if($smtp->_RCPT("TO:" . _addr($smtp, $addr) . $opts)) { push(@ok,$addr) if $skip_bad; } elsif(!$skip_bad) { return 0; } } return $skip_bad ? @ok : 1;

    Which tells us that

    @goodrecips=$smtp->recipient(@recipients, { Notify => ['FAILURE'], SkipBad => 1 });

    Will return the list of valid recipients and return notifications of failures (DSNs) via SMTP. Of course, some or all of this may not work, depending upon the configuration of the remote SMTP server.


    If anyone needs me I'll be in the Angry Dome.
Re: Net::SMTP success / failure response
by eclark (Scribe) on Mar 31, 2004 at 18:06 UTC

    Sorry, your relay has no way of knowing whether that user exists. The only way to do it properly is to check for bounces, this is still true if you deliver to that domain's MX.

    Also, in the sample code you provided you should check the return value of each method. Newer spam catching techniques would consider your software a spam agent because it would ignore the result of the RCPT TO command and try to send DATA anyway.

      That's what I was beginning to be afraid of.

      The tip about cehcking the other return values is helpful however. I'll have to look into that. Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://341246]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found