Solved

use Net::SMTP; script error

Posted on 2004-03-24
9
498 Views
Last Modified: 2008-03-10
I am getting an error when running this script. I am running Active State on Win2K. What am I doing wrong? Code from Perl for Sys Admin, O'Reilly Press, page 256.

Here is the error.

D:\pl>perl -w PURGE.pl
Useless use of a variable in void context at PURGE.pl line 50.
Useless use of a constant in void context at PURGE.pl line 50.
syntax error at PURGE.pl line 50, near ""Unable to create new mailer
 object";"
Execution of PURGE.pl aborted due to compilation errors.

Here is the code...

use Net::SMTP;

if(-e $INFILE) { print "OK\n" } else {
      
      ### Create an e-mail message here.

      $from="server\@abc.com";
      $to="user\@abc.com";
      $subject="Windows Purge Error";
      $body="There is a Purging Error on Win2000 server.\n";

      $type="smtp";
      $server="smtp.mahapps.inside.ups.com";
      
      my $mailer = Net::SMTP->new($type, Server => $server or
(This is Line 50)      die "Unable to create new mailer object:$!\n";

      $mailer->open({From => $from,
                  To => $to,
                  Subject => $subject}) or
            die "Unable to populate mailer objects part 2:$!\n";

};

I tried use MIME::Lite;, but got this error...

D:\pl>perl -w PURGE_ATTACHMENTS.pl
Can't locate MIME/Lite.pm in @INC (@INC contains: C:/Program Files/Perl/lib C:/P
rogram Files/Perl/site/lib .) at PURGE_ATTACHMENTS.pl line 25.
BEGIN failed--compilation aborted at PURGE_ATTACHMENTS.pl line 25.

Thanks!!!
0
Comment
Question by:p1800volvo
  • 5
  • 2
  • 2
9 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 10672135
Should be:

my $mailer = Net::SMTP->new($type, Server => $server) or die "Unable to create new mailer object:$!\n";

0
 
LVL 6

Assisted Solution

by:sstouk
sstouk earned 25 total points
ID: 10672247
Try this code.
This should work:
########################
use Net::SMTP;

if(-e $INFILE) { print "OK\n" } else {
     
     ### Create an e-mail message here.


     $from="server\@abc.com";
     $to="sergy_stouk\@server.com";
     $subject="Windows Purge Error";
     $body="There is a Purging Error on Win2000 server.\n";
     $server="smtp.server.com";

   $smtp = Net::SMTP->new($server);
    $smtp->mail($from);
    $smtp->to($to);

    $smtp->data();
    $smtp->datasend("To: $to\n");
    $smtp->datasend("\n");
    $smtp->datasend("$body\n");
    $smtp->dataend();
    $smtp->quit;
};
##################################
unfortunately with Net::Smtp it is more of a lower level of Sending an e-mail and you have to encapsulate the Subject and additional header parameters into the data of the message yourself.

With MIME::Lite it is much easier and object-oriented.
You have to install MIME::Lite first.
It is not installed by default with ActiveState Perl Engine.


0
 
LVL 1

Author Comment

by:p1800volvo
ID: 10672406
When I run the fix (stupid me) from Tintin, I get...

D:\pl>perl PURGE_ATTACHMENTS.pl
Can't locate object method "open" via package "Net::SMTP" at PURGE_ATTACHMENTS.pl line 53.

When I run the script from sstouk, I get...


D:\pl>perl PURGE_ATTACHMENTS.pl
Can't call method "mail" on an undefined value at PURGE_ATTACHMENTS.pl line 68.

Thanks people!
0
 
LVL 48

Accepted Solution

by:
Tintin earned 50 total points
ID: 10672599
Sorry, my mistake.  Should be:

my $mailer = Net::SMTP->new(Server => $server) or die "Unable to create new mailer object:$!\n";

I'm pretty certain you'll get the "Unable to create new mailer object" message as the script won't be able to connect to the SMTP server.

The reason you got the results with sstouk's version, is that there was no error checking when creating the SMTP object, so if that fails, all the SMTP methods are going to fail.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:sstouk
ID: 10673585
I tested the script that I posted and it worked ASIS, of course you have to put correct SMTP Server to connect to and your own e-mail addresses.
The Error you've got is because either you specified an incorrect name for your SMTP server, or there is no connection to it or it requires Authentication (user and password) to send e-mail.
If you have any experience with Telnet  (it is available in Windows) then you can attempt to connect to the SMTP Server using port 25 and manually attempt to see its responces and if it is alive or it requires any authentication. I am sure you can find examples of responces to send to it via telnet by searching Google.
I am positive that there is nothing wrong with the example I offered above.
0
 
LVL 1

Author Comment

by:p1800volvo
ID: 10677232
"Unable to create new mailer object" it is. Oh well. Guess I will have to script a conversation like sstouk said. How does that work? Do I have to call a separate script?
0
 
LVL 1

Author Comment

by:p1800volvo
ID: 10677943
I did file lite.pm in the following directories...

C:\Program Files\Perl\site\lib\SOAP, ~\UDDI, ~\win32\OLE, ~\XML\Parser and ~\XMLRPC. Is there a bad or incomplete path setting, or are these versions not the required Mime::Lite?

Thanks again!
0
 
LVL 1

Author Comment

by:p1800volvo
ID: 10678670
Hold off for now, I have Mime::Lite working. I will work on the points tonight.

Thanks for the great help.
0
 
LVL 1

Author Comment

by:p1800volvo
ID: 10683434
50 for Tintin for the correct answer, avan though my machine did not have sendmail on it. Not his fault!

25 for sstouk foranother good example. I might use either in the future.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hard perl script 16 154
compress files in RAR using perl 13 79
PHP equivalent of opening a com object 5 69
Removing permissions on a Cyrus mailbox via a wrapper script 3 88
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now