Recovering from errors in net:smtp

Hi,
    I've got a script that sends reports by email using Net::SMTP.
Occassionally I get errors from the mail server (normally rejected
addresses) which causes an exception which I catch with rescue. This is
in a class derived from Net::SMTP:

  def send (to, subject, data)
    retries = 0
    @count += 1
    to_array = to.split(/\s*,\s*/)

    hdrs = <<HDRS

···

To: #{to}
Subject: #{subject}
Date: #{Time.now.strftime("%a %b %e %T %Y %z")}
Message-Id: <selms-#{@time}-#{@count}@selms>
From: #{@from}

HDRS

      send_message( hdrs + data.join("\n") + ".\n", @from, *to_array)
  rescue Net::SMTPFatalError, Net::SMTPSyntaxError
    if $! =~ /virtual alias table/ then
      retries += 1
      STDERR.puts "mail failed #{retries} for #{to}:#{$!}"
      spleep(10)
     retry if retries <= 2
    end
    STDERR.puts "mail failed for #{to}:#{$!}"
    false
  end

I wish to 'reset' the smtp session so that I can continue sending email
without doing a finish and another start (i.e. before I retry). Is this
possible? I've looked through the docs for net::smtp and not found
anything obvious. I have not looked at the source yet.

Russell

--
Posted via http://www.ruby-forum.com/.

I haven't submitted a patch for it yet, but this will do it:

···

On Oct 10, 2006, at 5:56 PM, Russell Fulton wrote:

I wish to 'reset' the smtp session so that I can continue sending email
without doing a finish and another start (i.e. before I retry). Is this
possible? I've looked through the docs for net::smtp and not found
anything obvious. I have not looked at the source yet.

##
# Hack in RSET

class Net::SMTP # :nodoc:

   unless instance_methods.include? 'reset' then
     ##
     # Resets the SMTP connection.

     def reset
       getok 'RSET'
     end
   end

end

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Eric Hodel wrote:

Thanks very much Eric, I'm sure I could have figured this out for myself
but it would have taken much time and delving into RFCs and source code.

I haven't submitted a patch for it yet, but this will do it:

Are you going to or should I submit it on your behalf (with appropriate
attribution of course :slight_smile:

···

##
# Hack in RSET

class Net::SMTP # :nodoc:

   unless instance_methods.include? 'reset' then
     ##
     # Resets the SMTP connection.

     def reset
       getok 'RSET'
     end
   end

end

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

--
Posted via http://www.ruby-forum.com/\.