Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMTP authentication in PERL script for Windows

Status
Not open for further replies.

Newposter

Technical User
May 9, 2002
735
0
0
US
I have a PERL script to generate a form email, and it worked flawlessly thousands of times, until my ISP started requiring authentication. I'm stumped now in trying to write the line(s) of code to send out the username and password to authenticate to the ISP's mail server, which is my smart host. The script without authentication is as follows:

send_mail($to, $from, $subject, $comments, $username, $userip, $useremail, $userurl, $daten, $timen);

sub send_mail {

use Net::SMTP;

my $relay = "myISP.net";
my $smtp = Net::SMTP->new($relay)
|| die "Can't open mail connection: $!";

$smtp->mail($fromemail);
$smtp->to($masteremail);

$smtp->data();
$smtp->datasend("To: $masteremail\n");
$smtp->datasend("From: mydomain.com\n");
$smtp->datasend("Subject: New email sent to mydomain.com\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->datasend("A copy of the message can be found below\n");
$smtp->datasend("======================================\n");
$smtp->datasend(" NAME: $username($userip) at $daten($timen)\n");
$smtp->datasend(" EMAIL: $useremail\n");
$smtp->datasend(" \n");
$smtp->datasend(" $comments\n");
$smtp->datasend(" \n");

$smtp->dataend();
$smtp->quit();
}
&guestbook_succ("Your message has been successfully sent.");
}

Can anyone help with the missing code to pass the username and password? I'm using Mailenable for Windows on a Windows 2000 platform. Works great except for this new snag. The error I get is:

"Communications Error: Socket connection to myISP.net failed. Message Delivery Failure (EBFC37A9E37D4A458C909D66A33F4.MAI) Attempt (0): Could not find mail server for domain (). Message has been requeued."

Last night I discovered for inexplicable reasons that if I reboot the server (not merely restart the SMTP service), the message delivers instantly. Obviously I can't live with that as a method. Any help appreciated.

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
Thanks, it looks like the username and password are expected to be encoded 'per RFC 2554'. Does that mean I should use Windows MD5 encoding, or can I test this just to begin with by using plain text, to see if it works?

And I presume the AUTH command comes just between the "new($relay)" and the "|| die" lines?

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
Newposter said:
Thanks, it looks like the username and password are expected to be encoded 'per RFC 2554'. Does that mean I should use Windows MD5 encoding, or can I test this just to begin with by using plain text, to see if it works?

My guess (never having used it!) would be that plain text should be fine and Net::SMTP will look after any encoding for you, if required.

Newposter said:
And I presume the AUTH command comes just between the "new($relay)" and the "|| die" lines?

No, because they are actually one line of logic. I'd try it on the line after || die.

Annihilannic.
 
Hello,

The following method should be sufficient, which covers Annihilannic's suggestions.

Code:
my $smtp_username = 'your email/username';
my $smtp_password = 'your password';
my $smtp = Net::SMTP->new($relay) || die "Can't open mail connection: $!";
$smtp->auth($smtp_username, $smtp_password) || die "Can't authenticate SASL: $!";

Chris
 
Thanks, but it didn't work. Error log shows:

Can't open mail connection: Unknown error at xxxxx\cgi-bin\\SENDEM~1.CGI line 153.\n

That line is:

my $smtp = Net::SMTP->new($relay) || die "Can't open mail connection: $!";

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
OK, I changed my relay from my ISP to my own mailserver name, and this time I got this error:

Can't locate object method "auth" via package "Net::SMTP" (perhaps you forgot to load "Net::SMTP"?) at xxx\\cgi-bin\\SENDEM~1.CGI line 154.\n

Line 154 is:

$smtp->auth($smtp_username, $smtp_password) || die "Can't authenticate SASL: $!";

So now I'm wondering, it sounds like my PERL installation is lacking. It's PERL for Windows, version 5.6.1, and I did a full install years ago. Could it be that the AUTH module is not part of it?

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
Indeed, I just checked an old system with perl 5.6.1 installed, and the Net::SMTP package appears to be missing the auth() method.

You should be able to update either perl or just that module easily enough?

Annihilannic.
 
Thanks for your diligence! Hopefully I can just overwrite the installation with a newer one. Will report back.

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
Is there a way to tell if the auth() method is part of my PERL installation? I just installed ActiveState PERL 5.10.1 and updated the shebang to the new PERL path.

Using my own mailserver name as the relay, I get this error:

"Can't authenticate SASL: No such file or directory at xxx\\cgi-bin\\SENDEM~1.CGI line 154.\n"

Using my ISP as the relay, I get this error:

"Can't open mail connection: Unknown error at xxx\\SENDEM~1.CGI line 153.\n"

It also took nearly a full minute to fail, instead of seconds.

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
perldoc Net::SMTP should tell you what methods are available in the currently installed module.

Not very helpful error messages... it's hard to imagine what file or directory it is looking for to perform SMTP AUTH.

I just copied your code and ran it under ActiveState perl 5.8.9, defined my own values for the input variables and (after I remembered to remove your definition of $relay) it worked fine.

I couldn't test auth() though because our internal SMTP isn't configured for that.

Before I removed that $relay definition I had exactly the same "Unknown error" result as you; have you tested connectivity to your relay using telnet myisp.net 25 or similar?

Annihilannic.
 
Yes, I can telnet to the ISP fine, at least outside of my home network. I will try inside tonight.

The "Can't open mail connection" error is the result of the || die code in my script, and references the path to my script, and the line for defining the relay. It always worked before.

So the code works with a newer version of PERL without the line: my $relay = "myISP.net"; or without the line: my $smtp = Net::SMTP->new($relay) ?? Can you please post the code that worked, with generic info in place of any variables that are confidential to you? Thanks.

Perldoc references NET::SMTP::TLS now for TLS and AUTH, but I tried that and it didn't work. It wasn't clear what the correct syntax was.



Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
The code was exactly as per your original post. I just defined all of the variables with values appropriate for my network to test it.

I just mentioned the "myisp.net" thing because I overlooked the definition of that variable the first time I tried it, and received the same error that you were getting; which suggested to me that yours could simply have been a connectivity issue with the SMTP server.

So, in short, the code works with a newer version of PERL, with the line 'my $relay = "my-own-smtp-gateway.net"'. I haven't tested with SMTP AUTH.

Annihilannic.
 
Hmm. As I said, if I reboot the server (not merely restart the mailserver's services), the email delivers instantly, even if it's been sitting in the queue overnight. I find that very odd, and can't be anything due to the script.

Newposter
"Good judgment comes from experience. Experience comes from bad judgment."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top