blacksmith has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I realize I wrote something about this script once before here.
I made the corrections and had this script ready to fly into production (so I thought). I was put on another task with higher priority and this went on the back burner. Now I am back and tried a test run of the program and recieved a load of error messages.

Before I get into the messages, let me give a little background on the program. The program accesses a text file. The file is formatted to where the first element is the email address. The next element(s) are files that need to be emailed to the address at the start of the line. The elements are in single quotes and seperated by commas. They are read into the program as arrays. (See the script and input below).

Now, I am needing to know what these messages are referring to and how to correct the situation. I have looked over the sender.pm and am at wits end trying to make sense of it. I appreciate the assistance. One of the error messages is
Argument "him@here.com','C:/test1.txt" isn't numeric in array element at C:\SCRIPTS\hash.pl line 27.

This is listed is listed after each email is processed. With the respected email addresses and files. The next error messages are repeated numerous times.
print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/Sen +der.pm lin e 1229. print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/Sen +der.pm lin e 816. print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/Sen +der.pm lin e 832. print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/Sen +der.pm lin e 921. print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/M ail/Sender.pm line 1246. print() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/Sen +der.pm lin e 1248. readline() on closed filehandle Symbol::GEN1 at C:/Perl/site/lib/Mail/ +Sender.pm line 1250.

Here is the content of the text file that the program accesses.
'him@here.com','C:/test1.txt' 'her@there.com','C:/test3.txt' 'it@where.com','C:/test2.txt','C:/test1.txt'
Here is the code.
#!c:/perl -w use Mail::Sender; my $time = localtime; my $subj_line = "Price Change Worksheet for $time"; my $work = 'me@work.com'; open(LIST, "c:/email.txt") or die $!; my @files; while(<LIST>) { s/^'//; s/'\n?$//; @files = split /','/, $_; my $email = shift @files; my $sender = new Mail::Sender( {smtp => 'mail.domain.com',from => $wor +k}) || die "$Mail::Sender::Error\n"; $sender->OpenMultipart( {to => $email, subject => $subj_line}); $sender->Body; $sender->SendLine('Here are the price change worksheets.'); $sender->SendFile( {description => 'Text File', encoding => 'Base64', file => \@files}) and print "Mail was sent OK." || die "$Mail::Sender::Error\n"; $sender->Close; }
One last thing, the first email address is sent an email with the proper attachments. Thanks.
blacksmith.

Replies are listed 'Best First'.
Re: Email program with attachments
by $code or die (Deacon) on Oct 09, 2001 at 03:49 UTC
    Hi blacksmith

    This code works for me:
    my $sender = new Mail::Sender( {from => $work, smtp => 'mail.domain. +com'}) || die "$Mail::Sender::Error\n"; (ref $sender->MailFile({to => $email, subject => $subj_line, file => \@files, encoding => 'base64', msg => 'Here are the price change worksheets +.'}) ) || die "Error on $email: $Mail::Sender::Error\n";
    Also, what version of Mail::Sender are you using? The latest version is 0.7.08.

    Simon Flack ($code or die)
    $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
    =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
      Thanks $code or die. The readme file says version 0.7.09. The code you provided works great. My knowledge of the ref command is lacking. But, I am still having a problem with email addresses outside of the SMTP server. I am recieving this error message
      Error on myaccount@yahoo.com: Local user "myaccount@yahoo.com" unknown on host "mail.domain.com"
      I have also tried accounts other than yahoo. If I put the addresses into a hash such as so
      #!Perl -w use strict; use Mail::Sender; my $time = localtime; my $subj_line = "Price Change Worksheet for $time"; my %file = ('employee@theirjob.com'=>'P:\\PCW1.TXT','storeowner@aol.co +m'=>'P:\\PCW2.TXT'); my $work = 'me@work.com'; foreach (keys %file) { my $sender = new Mail::Sender( {smtp => 'mail.domain.com',from => $wor +k}) || die "$Mail::Sender::Error\n"; $sender->OpenMultipart( {to => $_, subject => $subj_line}); $sender->Body; $sender->SendLine('Here are the price change worksheets.'); $sender->SendFile( {description => 'Text File', encoding => 'Base64', file => $file{$_}}) and print "Mail was sent OK." || die "$Mail::Sender::Error\n"; $sender->Close; }
      I have no problem sending mail outside of the company's mail server. WAIT!! WAIT!! I just tested that program again. It did NOT work. It gave me the error messages that I was receiving from the origin message of this thread. Okay, here is the kicker. It worked previously. The recipient of one of the mails even replied to it. Spoke with one of the Admins and was told that there is nothing wrong with the mail server. I am able to send out messages manually. Any ideas on what could be going on? Thanks again.
      blacksmith
        I would say, double-check what you have on these lines:
        my $work = 'me@work.com';
        and
        ... new Mail::Sender( {smtp => 'mail.domain.com',from => $work}) ...
        I think you forgot to change either the mailserver name or your work address. It's very likely that your SMTP server rejects emails unless the sender or recipient is a valid local account. So if you still have me@work.com as an email address or the wrong mail server, then it will be rejected.

        Simon Flack ($code or die)
        $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
        =~y'_"' ';eval"die";print $_,lc substr$@,0,3;
Re: Email program with attachments
by tstock (Curate) on Oct 08, 2001 at 23:08 UTC
    From the error messages I would think the error is not in the code you posted, but instead on the module Mail::Sender.

    Update 1: - next paragraph proven wrong, and I decided I don't like Mail::Sender :)

    Try moving the new Mail::Sender and $sender->Close outside the loop and see if that fixes it, or look inside Mail::Sender for clues.

    Update 2: - message "Mail was sent OK." is premature. (and let me in the wrong path)

    On a side note, you line reading could be done with one regex instead of two and a split:
    my ($email, $file) = $_ =~ /^'(.*)','(.*)'$/;
    or something to that effect

    tstock
    ++$|; # I call this the plunger. it cleans the pipes
      No. $sender is lexically scoped and has no meaning outside the loop.

      The error is with the code. If you look at the docs, the Close() method sends the mail, if you move $sender->Close() outside the loop, the best you can hope for is only one email being sent - the last one.

      I can understand why you think that the problem is with the module's code - the error messages look that way - because the module uses warn() and not carp() in those lines.

      Simon Flack ($code or die)
      $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
      =~y'_"' ';eval"die";print $_,lc substr$@,0,3;