$SQL = "Select * from database where active='yes' AND (TO_DAYS(NOW())-TO_DAYS(dateadded) < 1)"; #check for records created today &Do_SQL; while ($pointer = $sth->fetchrow_hashref) { $jobtype = $pointer->{'jobtype'}; $toemail = $pointer->{'email'}; $SQL3 = "Select * from database where jobtype='$jobtype' AND active='yes' ORDER BY RAND() LIMIT 5"; &Do_SQL3; while ($pointer3 = $sth3->fetchrow_hashref) { $contact= $pointer3->{'contact'}; $jobnum= $pointer3->{'jobnum'}; $picture = $pointer3->{'picture'}; $my_job = "$picture - $contact"; } } open (MAILHTML, "|$sendmail $toemail") || die "Can't open $sendmail!\n"; print MAILHTML "From: Admin \n"; print MAILHTML "Reply-to: Admin \n"; print MAILHTML "To: $toemail\n"; print MAILHTML "Subject: Recommended Jobs\n"; print MAILHTML "Content-Type: text/html\n"; print MAILHTML "$my_job"; close (MAILHTML);