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

I am trying to use some Perl that I last used some years ago to send an e-mails.
It seems to have got ‘rusty’ as it no longer works and I cannot see why.
The code follows.
$smtp = Net::SMTP->new($$ref_host_em); print "[send_em] smtp <$smtp>\n"; $sm_ma = $smtp->mail($$ref_from_em); $en = 0; $en = Win32::GetLastError(); $en_f = Win32::FormatMessage($en); print "[send_em] A en <$en> <$en_f> result of smtp send ma <$sm_ma>\n" +; #$sm_to = $smtp->to ($to_em_item); $sm_to = $smtp->to ($$ref_from_em); $en = 0; $en = Win32::GetLastError(); $en_f = Win32::FormatMessage($en); print "\n[send_em] B en <$en> <$en_f> result of smtp send ma <$sm_ma> + to <$sm_to> \n"; $sm_da = $smtp->data(); $en = 0; $en = Win32::GetLastError(); $en_f = Win32::FormatMessage($en); print "\n[send_em] C en <$en> <$en_f> result of smtp send ma <$sm_ma> + to <$sm_to> da <$sm_da>\n"; $sm_se = $smtp->datasend($message_body); $en = 0; $en = Win32::GetLastError(); $en_f = Win32::FormatMessage($en); print "\n[send_em] D en <$en> <$en_f> result of smtp send ma <$sm_ma> + to <$sm_to> da <$sm_da> se <$sm_se>\n"; $sm_ed = $smtp->dataend(); $en = 0; $en = Win32::GetLastError(); $en_f = Win32::FormatMessage($en); print "\n[send_em] E en <$en> <$en_f> result of smtp send ma <$sm_ma> + to <$sm_to> da <$sm_da> se <$sm_se> ed <$sm_ed>\n";

The results from this are below.
As can be seen I get the ‘handle is invalid’ error message continuously after and including the $smtp->to line.
This is even when the data used for the $smtp->to is the identical to that used for the successful $smtp->mail. I think that the $smtp = Net::SMTP->new is OK since as well as getting a ‘value’ for $smtp I get an error message if I use this code when the PC is not connected to the Internet.
Can anyone suggest what is going wrong and how to cure this?

send_em smtp <Net::SMTP=GLOB(0x1d66660)>
send_em A en <0> <The operation completed successfully.> result of smtp send ma <>
send_em B en <6> <The handle is invalid> result of smtp send ma <> to <0>
send_em C en <6> <The handle is invalid.> result of smtp send ma <> to <0> da <>
send_em D en <6> <The handle is invalid.> result of smtp send ma <> to <0> da <> se <0>
send_em E en <6> <The handle is invalid.> result of smtp send ma <> to <0> da <> se <0> ed <0>

Replies are listed 'Best First'.
Re: SMTP Email problems
by arc_of_descent (Hermit) on Jun 15, 2009 at 16:02 UTC

    There's a Debug parameter available which might help you out.

    my $smtp = Net::SMTP->new($$ref_host_em, Debug => 1);

    --
    Rohan
      Thanks for that. I have added the debug and got the output below.
      Sadly it did not help a lot and even confused me
      because I got the same messages about SMTP authentication when it was both 'on' and 'off'.
      Perhaps the last message about Unexpected EOF on command channel
      is the one I sohuld be thinking about.
      Any thoughts on this will be welcome!

      send_em no attachments
      Net::SMTP>>> Net::SMTP(2.29)
      Net::SMTP>>> Net::Cmd(2.26)
      Net::SMTP>>> Exporter(5.58)
      Net::SMTP>>> IO::Socket::INET(1.29)
      Net::SMTP>>> IO::Socket(1.29)
      Net::SMTP>>> IO::Handle(1.25)
      Net::SMTP=GLOB(0x1d66690)<<< 220 smtp807.mail.ird.yahoo.com ESMTP
      Net::SMTP=GLOB(0x1d66690)>>> EHLO localhost.localdomain
      Net::SMTP=GLOB(0x1d66690)<<< 250-smtp807.mail.ird.yahoo.com
      Net::SMTP=GLOB(0x1d66690)<<< 250-AUTH LOGIN PLAIN XYMCOOKIE
      Net::SMTP=GLOB(0x1d66690)<<< 250-PIPELINING
      Net::SMTP=GLOB(0x1d66690)<<< 250 8BITMIME
      send_em smtp <Net::SMTP=GLOB(0x1d66690)>
      Net::SMTP=GLOB(0x1d66690)>>> MAIL FROM:<correct e-mial address>
      Net::SMTP=GLOB(0x1d66690)<<< 530 authentication required - "Your email could not be sent.</br To fix this you must make a simple change to your email (known as SMTP authentication).
      For advice visit www.btyahoo.com/smtp"
      send_em A en <0> <The operation completed successfully.
      > result of smtp send ma <>
      Net::SMTP=GLOB(0x1d66690)>>> RCPT TO:<correct e-mial address>
      Net::SMTP: Unexpected EOF on command channel at email-jun09-b.pz line 159
      send_em B en <6> <The handle is invalid.
      > result of smtp send ma <> to <0>