Solved

use Net::SMTP; script error

Posted on 2004-03-24
9
494 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 6

Expert Comment

by:sstouk
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

13 Experts available now in Live!

Get 1:1 Help Now