Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array not functioning

Status
Not open for further replies.

spyderco

Programmer
Jan 23, 2005
107
0
0
US
I have a form which stores data into an array. When I do a test print on the array @mails, it prints out the email addresses that are inside. The array can have anywhere between 1 and 5 email addresses as these are form fields.

Code:
   my $fmail1  = param("fmail1");
   my $fmail2  = param("fmail2");
   my $fmail3  = param("fmail3");
   my $fmail4  = param("fmail4");
   my $fmail5  = param("fmail5");
   my @mails = qq($fmail1 $fmail2 $fmail3 $fmail4 $fmail5);

   foreach(@mails) { print "$_<p>"; }


However, later down the script I have the following snippet and it FAILS. Even if the test print above shows there are email addresses in the array, it fails and prints

failed.
failed.
failed.
failed.
failed.
The emails have been delivered.



Is there something wrong with the conditionals or how I am using $mails[$cnt]? I can't put my finger on what's causing this.

Code:
   for my $cnt (1 .. 5)
   {
      if ($mails[$cnt] ne "")
      {
         open(MAIL,"| $sendmail") or die "Can't open $sendmail sendmail because: $!";
         print MAIL "To: $mails[$cnt]\n";
         print MAIL "From: $email\n";
         print MAIL "Subject: Refer A Friend from $names[$cnt]\n\n";
         print MAIL "Dear $names[$cnt],\n\n";
         print MAIL "Your friend, $name at $email, visited out site recently and thought you would be interested..\n\n";
         print MAIL "This is NOT spam. This email was sent by someone who said they knew you.  Your email address is not stored in any databases and will not 

be sold or given to anyone at any time.  This is the only time you will receive an email from us.";
         close(MAIL);
      }
      else
      {
         print "$mails[$cnt] failed.<br>";
      }

   }
 
That's cos you're assigning a string to an array!
Try:
Code:
my @mails = ($fmail1 $fmail2 $fmail3 $fmail4 $fmail5);
without the "qq" prefix.

Trojan.
 
That may have been an initial problem but it wasn't causing this :(

I'll post the whole code to see if anyone can figure out why it initially saves the variables in the array but then it doesn't email properly for the recipients.

Code:
#!/usr/bin/perl

use warnings;
use strict;

use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);

#######################################
# Configuration section               #
#######################################
my $admin_mail = "###";
# change the above to the email address of the administrator

my $sendmail = '/usr/sbin/sendmail';


#######################################
# Do not edit below                   #
#######################################
print header;
if (param())
{
   ######
   # Setup our vars
   ######
   my $email   = param("email");
   my $name    = param("name");
   my $fname1  = param("fname1");
   my $fname2  = param("fname2");
   my $fname3  = param("fname3");
   my $fname4  = param("fname4");
   my $fname5  = param("fname5");
   my $fmail1  = param("fmail1");
   my $fmail2  = param("fmail2");
   my $fmail3  = param("fmail3");
   my $fmail4  = param("fmail4");
   my $fmail5  = param("fmail5");
   my $message = param("message");

   my @names = ("$fname1", "$fname2", "$fname3", "$fname4", "$fname5");
   my @mails = ("$fmail1", "$fmail2", "$fmail3", "$fmail4", "$fmail5");

   foreach(@names) { print "$_ "; }
   print "<br>";
   foreach(@mails) { print "$_<p>"; }

   my $time = localtime();

   ######
   # Email function starts here
   ######

   # email to site admin
   open(MAIL,"| $sendmail -t") or die "Can't open $sendmail sendmail because: $!";
   print MAIL "To: $admin_mail\n";
   print MAIL "From: $email\n";
   print MAIL "Subject: Refer A Friend was used\n\n";
   print MAIL "Name: $name\n";
   print MAIL "Email: $email\n";
   print MAIL "Time: $time\n\n";
   print MAIL "Email addresses this user refered:\n";
   print MAIL "$fmail1" if ($fmail1 ne "");
   print MAIL ", $fmail2" if ($fmail2 ne "");
   print MAIL ", $fmail3" if ($fmail3 ne "");
   print MAIL ", $fmail4" if ($fmail4 ne "");
   print MAIL ", $fmail5" if ($fmail5 ne "");
   print MAIL "\n\n Custom message:\n";
   print MAIL "$message";
   close(MAIL);

   # email to user
   open(MAIL,"| $sendmail -t") or die "Can't open $sendmail sendmail because: $!";
   print MAIL "To: $email\n";
   print MAIL "From: $email\n";
   print MAIL "Subject: Refer A Friend - Thank you\n\n";
   print MAIL "You recently used the Refer a Friend feature on our web site.  This is a confirmation e-mail showing you the information we recieved.  We 

thank you for spreading the word about our site.  Your e-mail address is now in the award program.\n\n";
   print MAIL "Name: $name\n";
   print MAIL "Email: $email\n";
   print MAIL "Time: $time\n\n";
   print MAIL "Email addresses you refered:\n";
   print MAIL "$fmail1" if ($fmail1 ne "");
   print MAIL ", $fmail2" if ($fmail2 ne "");
   print MAIL ", $fmail3" if ($fmail3 ne "");
   print MAIL ", $fmail4" if ($fmail4 ne "");
   print MAIL ", $fmail5" if ($fmail5 ne "");
   print MAIL "\n\n Custom message:\n";
   print MAIL "$message";
   close(MAIL);

   # email to recipients
   #my $cnt = 0;
   for my $cnt (1 .. 5)
   {
      if ($mails[$cnt] ne "")
      {
         open(MAIL,"| $sendmail") or die "Can't open $sendmail sendmail because: $!";
         print MAIL "To: $mails[$cnt]\n";
         print MAIL "From: $email\n";
         print MAIL "Subject: Refer A Friend from $names[$cnt]\n\n";
         print MAIL "Dear $names[$cnt],\n\n";
         print MAIL "Your friend, $name at $email, visited out site recently and thought you would be interested..\n\n";
         print MAIL "This is NOT spam. This email was sent by someone who said they knew you.  Your email address is not stored in any databases and will not 

be sold or given to anyone at any time.  This is the only time you will receive an email from us.";
         close(MAIL);
      }
      else
      {
         print "$mails[$cnt] failed.<br>";
      }

   }

  print "The emails have been delivered.<br><br>";
  print "Thank you for spreading the word about our web site.  Your email address is now entered into the contest";
}
else
{
   &printform;
}



sub printform
{
print <<"ALL";
<form action="" method="post">
<table width="200" border="3" cellpadding="0" cellspacing="0" bordercolor="#000099">
  <tr>
    <td><table width="267" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2"><div align="center">Refer a Friend </div></td>
      </tr>
      <tr>
        <td width="98">Your Name </td>
        <td width="159"><input name="name" type="text" id="name"></td>
      </tr>
      <tr>
        <td>Your Email </td>
        <td><input name="email" type="text" id="email"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">Friend information (up to 5) </div></td>
      </tr>
      <tr>
        <td>Name</td>
        <td>Email</td>
      </tr>
      <tr>
        <td><input name="fname1" type="text" id="fname1"></td>
        <td><input name="fmail1" type="text" id="fmail1"></td>
      </tr>
      <tr>
        <td><input name="fname2" type="text" id="fname2"></td>
        <td><input name="fmail2" type="text" id="fmail2"></td>
      </tr>
      <tr>
        <td><input name="fname3" type="text" id="fname3"></td>
        <td><input name="fmail3" type="text" id="fmail3"></td>
      </tr>
      <tr>
        <td><input name="fname4" type="text" id="fname4"></td>
        <td><input name="fmail4" type="text" id="fmail4"></td>
      </tr>
      <tr>
        <td><input name="fname5" type="text" id="fname5"></td>
        <td><input name="fmail5" type="text" id="fmail5"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">Email text sent to friends </div></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="preset" cols="40" id="preset" readonly="true">this is text the user will email but cannot edit</textarea></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">Your personal message to your friends </div></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="message" cols="40" id="message"></textarea></td>
      </tr>
      <tr>
        <td colspan="2"><div align="center">
            <input type="submit" name="Submit" value="Submit">
        </div></td>
      </tr>
    </table></td>
  </tr>
</table>
</form>
ALL

}
 
The only obvious thing I can see is the "for my $cnt (1 .. 5)" which should probably be "for my $cnt (0 .. 4)" since all arrays (by default) start at zero.

I think you need to put some "print" statements in throughout the code to see where the elements of this array vanish.

Your syntax for accessing the array elements though is correct "$mails[$cnt]".



Trojan.



 
This is so screwy! I setup a new array and copied it over and NEITHER of them work in the conditional or near the bottom.

Something's really weird.
 
have you escaped the @ symbols in the emails addresses?


Kind Regards
Duncan
 
remove your very first condition and see what happens:

if (param())
 
Hi Kevin

if the if (param()) failed all that would happen is...

else
{
&printform;
}

... so that must be fine

i.e. the section that prints "failed" is within the first part of the if statement - so the check must be returning true


Kind Regards
Duncan
 
What exactly is it doing now, is it still printing 'failed'? I tried your code and commented out the parts that actually send the e-mail but it didn't exhibit the problem you originally posted.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top