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

Hello :

I am once again, working an automated MLM script for my client. My program incorporates Mail::Bulkmail, DBI and MIME::Lite.

 

Here is the code:

 

#!/usr/bin/perl -w ## Test Email Subroutine: use strict; use DBI; use Mail::Bulkmail; use MIME::Lite; use Net::SMTP; my($email_name, $body_text, @corpus); ## Components my $sender = "checkitout\@ogundevelopment.com"; #Step 1: Give Subject line and Open Body of Email: print"\n Please indicate the file that contains the body of your email +:\t\t"; print"\n"; $body_text = <STDIN>; #Open Email File and load into array @corpus. open(MYFILE, $body_text) || die "Cannot Open File $body_text.\n"; @corpus = <MYFILE>; close(MYFILE); mailfunc($sender,@corpus); exit(0); #### Subroutine for sending email message: sub mailfunc { my($from, @body) = @_; my $relay = "gorillatrades.com"; ## Mail Server. my $smtp = Net::SMTP->new($relay); ## Mail Object Handler. die "Could not commicate with $relay. $!" if (!defined $smtp); my @images; ## Stores Images references; my $message = join(' ', @body),"\n"; # Create the initial text of the message my $mime_msg = MIME::Lite->new( Type => "multipart/mixed", ) or die "Error creating MIME body: $!\n"; $mime_msg-> attach(Type => 'text/html', Data => "$message" ); my $message_body = $mime_msg-> body_as_string(); #### Send out Email. my $bulk = Mail::Bulkmail->new( From => $from, Smtp =>$relay, Subject => 'This is a test message!', Message => $message_body ); #Database my $dbh = DBI -> connect("DBI:mysql:database=$datab +ase , $user, $pass, {PrintError => 0, RaiseError => 1}); my $sth = $dbh->prepare("SELECT email FROM beta"); $sth->execute; $bulk->LIST(\&{$sth->fetchrow_array()}); ## Second Send Message Using ONLY MIME #MIME::Lite->send('smtp', "ogundevelopment.com", Timeout=>60); #$mime_msg-> send(); $bulk->bulkmail; $dbh->disconnect; }

The error I get is this:

 

DBI::db=HASH(0x848a9c4)->disconnect invalidates 1 active statement handle (either destroy statement handles or call finish on them before disconnecting) at t1.pl line 88.

 

Please help.

 

Peace --

Cappa

PS: I would like to thank all of the Perl Monks for their previous assistance YOU GUYS ROCK!!!

Replies are listed 'Best First'.
Re: DBI Driven MLM
by jZed (Prior) on Dec 06, 2005 at 02:04 UTC
    As far as I can see, you only call $sth->fetchrow_array() once, not in a loop. That means you will only ever fetch a single row. Since you disconnect before fetching the remaining rows, you get a warning that you are disconnecting while the statement handle is still active (i.e. still has rows to fetch). Either fetch all the rows, or else use $sth->finish() if you really intend to only fetch one row.
Re: DBI Driven MLM
by wazzuteke (Hermit) on Dec 06, 2005 at 03:56 UTC
    Although the $bulk->LIST() can effectively accept a code-reference as a parameter, it won't work well in this context. To extend the previous comment, I would re-factor to the following logic, avoiding the code-ref and replacing with an array ref:
    ... $bulk->LIST( $dbh->fetchall_arrayref() ); $dbh->rollback(); # Not necessary, but I like to do it $dbh->disconnect(); # Look below for more detail ...
    Technically, DBI will automatically rollback and disconnect via the DESTROY method, however there are the potential for warnings if you let DBI handle it, so it's always nice to do it your self.

    Overall, this should not only fix your problem, but it shoudl also set the entire list of e-mail addresses in your database to the LIST attribute.

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );