Sending email using perl

L

Lars Roland

Hi all

I use the folowing script to send email directly to a server (using its ip
address), my problem is that the script nerver halts (eg. its while loop
semas to run forever), can someone in here see why this is so ? as I can
not understand this:

--------------------------------------------
#!/usr/bin/perl
use strict;
use MIME::Entity;
my $mailhost = shift(@ARGV);
my $recip = shift(@ARGV);
my $sender = shift(@ARGV);
my $subject = shift(@ARGV);
my $nrofmsg = shift(@ARGV);
if ($mailhost =~ /--help/)
{
die "usage: mail.pl smtp-server recipient sender subject nrofmails.
The e-mail body is empty"
};
my $nrofsend = 1;
#the e-mail
my $message;
$message="Test";
while( $nrofsend <= $nrofmsg)
{
fork;
# MIME stuff
my $top = MIME::Entity->build(Type =>"multipart/mixed",
From => "$sender",
To => "$recip",
Subject => "Messages number $nrofsend:
$subject");
print STDOUT "Sending message $nrofsend of $nrofmsg\n";
# contact $mailhost
$top->smtpsend( Host => $mailhost,
To => $recip,
From => $sender,
Hello=> "HELO",
Port => 25) || die;
++$nrofsend;
sleep 1;
};
--------------------------------------------

example of use (if the code is put in mail.pl)

../mail.pl IP-OF-MAILSERVER RECIPIENT@EMAIL SENDER@EMAIL SUBJECT NUM_OF_EMAILS




Thanks in advance
 
M

Michele Dondi

#!/usr/bin/perl
use strict;

While you're there you may

use warnings;

too.
use MIME::Entity;
my $mailhost = shift(@ARGV);

in this said:
my $recip = shift(@ARGV);
my $sender = shift(@ARGV);
my $subject = shift(@ARGV);
my $nrofmsg = shift(@ARGV);

Apart the side effect of removing entries from @ARGV (that doesn't
have any influence on the rest of the script as far as I can see) this
would have done the same:

my ($mailhost, $recip, ..., $nrofmsg)=@ARGV;

and would be preferable under different points of view.
if ($mailhost =~ /--help/)

Not that this is technically wrong (but it is awkward and clumsy, as
for one thing I think you really want /^--help$/ aka C<$mailhost eq
'--help'>), but you may want to

use GetOpt::*;

instead.
while( $nrofsend <= $nrofmsg)
{
fork;

As a general rule fork()s are followed by code that takes different
actions based on its return value (including checking that it worked
at all).

Or do you want to exponentially increase the number of processes
generated by your program?

Remark: to be fair I'm not following the logic of your script in
detail, but I don't see why a fork() is needed at all...
# MIME stuff
my $top = MIME::Entity->build(Type =>"multipart/mixed",
From => "$sender",
To => "$recip",
Subject => "Messages number $nrofsend:
$subject");
print STDOUT "Sending message $nrofsend of $nrofmsg\n";

print()s default to STDOUT. No need to indicate so, unless you have
select()ed something else... and it doesn't seem to me that you did.


HTH,
Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top