Solved

use Net::SMTP; script error

Posted on 2004-03-24
9
503 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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
 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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