Sending email using perl

Discussion in 'Perl Misc' started by Lars Roland, Nov 15, 2004.

  1. Lars Roland

    Lars Roland Guest

    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
     
    Lars Roland, Nov 15, 2004
    #1
    1. Advertising

  2. On Mon, 15 Nov 2004 13:24:21 +0100, Lars Roland <>
    wrote:

    >#!/usr/bin/perl
    >use strict;


    While you're there you may

    use warnings;

    too.

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


    C<shift;> is completely equivalent to C<shift(@ARGV);>, in this
    context. With the difference that it is more terse and readable...

    >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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Nov 15, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee

    [perl-python] sending email

    Xah Lee, Jan 29, 2005, in forum: Python
    Replies:
    5
    Views:
    382
    Chris Mattern
    Jan 30, 2005
  2. Luke
    Replies:
    2
    Views:
    4,721
    Nigel Wade
    Mar 15, 2007
  3. rote
    Replies:
    8
    Views:
    774
  4. Kevin B. Pease
    Replies:
    1
    Views:
    354
    A. Sinan Unur
    Nov 19, 2004
  5. bl8n8r
    Replies:
    3
    Views:
    121
    J├╝rgen Exner
    Apr 18, 2008
Loading...

Share This Page