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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays in the sendmail portion of script

Status
Not open for further replies.

perlnewbie05

Programmer
Jun 4, 2005
5
US
Hi,
I'm trying to retrieve arrays through my sendmail portion of the script...

For example, note the following script:

#!/usr/bin/perl -w


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

my @decorations = param('Decorations');

print "Content-type: text/html\n\n";
print "<html><head><title>Perl</title></head>\n<body>\n";
foreach my $Decorations (@decorations) {
print "You picked $Decorations.<br>\n";
}

print "</body></html>";

I created a form with a number of checkboxes and drop down lists. In the above example, there are 4 checkboxes. Depending on what user checks off is what is displayed during output to the browser, which is great. But I also want these "arrays" or "lists" to be sent to my email via the sendmail portion of the script, but I'm not sure how to do so.

Any help would be greatly apprecated.
 
just add @decorations into the message/body of the email you are sending. Post the sendmail part of your script and someone will show you what I mean.
 
Right - or assuming you opened sendmail with a pipe, use the same type of loop you wrote above but print to the pipe.

open(SENDMAIL, "|$sendmail") or die "Hey - where's my sendmail";
print SENDMAIL $headerstuff;
print SENDMAIL "\n";
foreach my $Decorations (@decorations) {
print SENDMAIL "You picked $Decorations\n";
}
close SENDMAIL;


Tony Lawrence
Linux/Unix/Mac OS X Resources
 
Hi,
Thanks for the replies...
Below, is my full script (please note that it's most likely "choppy" but learning this program on your own isn't always the easiest way).
Please note where @Decorations or $decorations is located throughout the script because how it's written now is not working.
Thanks

#!/usr/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);

&Parse_Form;
&mime;

$first = $formdata{'first'};
$last = $formdata{'last'};
$email = $formdata{'email'};
$address = $formdata{'address'};
$address2 = $formdata{'address2'};
$city = $formdata{'city'};
$state = $formdata{'state'};
$zip = $formdata {'zip'};
$title='mail test';
$subject='Using Sendmail';
$phone1 = $formdata{'phone1'};
$phone2 = $formdata{'phone2'};
my @Decorations = param('Decorations');

open(MAIL, "|/usr/sbin/sendmail -t");

print MAIL "To: $formdata{email}\n";
print MAIL "From: admin\@chef-2-go.net\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from Yahoo! Web Hosting\n";
print MAIL "Thank you $formdata{first} $formdata{last}\n";
print MAIL "Your address is:\n";
print MAIL "$formdata{address}\n";
print MAIL "$formdata{address2}\n";
print MAIL "$formdata{city} $formdata{state} $formdata{zip}\n";
print MAIL "DECORATIONS: $formdata{Streamers} $formdata{Balloons} $formdata{Signs} $formdata{Center_Piece}\n";
print MAIL "TABLES(if any): $formdata{Yes1} $formdata{No1}\n";
print MAIL "If yes, how many? $formdata{Howmany1}\n";
print MAIL "\n";
foreach my $Decorations (@Decorations) {
print MAIL "You picked $Decorations\n";
}
close (MAIL);

open(MAIL, "|/usr/sbin/sendmail -t");

print MAIL "To: admin\@chef-2-go.net\n\n";
print MAIL "From: $formdata{email}\n";
print MAIL "Subject: Request info for $formdata{first} $formdata{last}\n\n";
## Mail Body
print MAIL "This is a request from $formdata{first} $formdata{last}\n";
print MAIL "address:\n";
print MAIL "$formdata{address}\n";
print MAIL "$formdata{address2}\n";
print MAIL "$formdata{city} $formdata{state} $formdata{zip}\n";
print MAIL "$formdata{phone1}\n";
print MAIL "DECORATIONS: $formdata{Streamers} $formdata{Balloons} $formdata{Signs}$formdata{Center_Piece}\n";
close (MAIL);

sub mime {
print "Content-type:text/html\n\n";
}

sub ErrorMessage {
print "<P>The server has a problem. Aborting script. \n";
exit;
}

sub Parse_Form {

if ($ENV{'REQUEST_METHOD'} eq 'GET') {

@pairs = split(/&/, $ENV{'QUERY_STRING'});

} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

if ($ENV{'QUERY_STRING'}) {

@getpairs =split(/&/, $ENV{'QUERY_STRING'});

push(@pairs,@getpairs);

}

} else {

print "Content-type: text/html\n\n";

print "<P>Use Post or Get";

}

foreach $pair (@pairs) {

($key, $value) = split (/=/, $pair);

$key =~ tr/+/ /;

$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {

$formdata{$key} .= ", $value";

} else {

$formdata{$key} = $value;

}

}
}
 
Hi,
Sorry for the confusion...like I said I'm fairly new at this. And yes, this is "my" script.

I thought that the array is this:
my @Decorations = param('Decorations');

Let me just explain what I'm attempting to do: on the form is a series of checkboxes. For example, the main category is called "Decorations". The checkboxes go as follows: Signs, Center Piece, Balloons, Streamers.
Now if the user was interested in one of these items, he or she would check off, say, Balloons and Streamers. Now I want this information to appear upon clicking the submit button so what they'd see in the brower are those checked items. But at the same time, I want that information (and I assume I'd do this in a list, or an array) sent to me via the sendmail portion of the script.

Please help me on this!
Thanks again
 
Why use the "Parse_Form" sub instead of param() like you did with @Decorations?

And for your problem, you can actually get the "Decorations" to show in the browser but not in the e-mail?

 
I think your existing script will work as-is if you load the CGI module:

Code:
#!/usr/bin/perl -w
[b]use CGI qw/:standard/;[/b]
use CGI::Carp qw(fatalsToBrowser);

[the rest of your script]

you are using the param() method fetch the @Decorations array but you never loaded the CGI module.

Here is how I might do it:

Code:
#!/usr/bin/perl -w

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

print header;
print start_html;

my $first    = param('first');
my $last     = param('last');
my $email    = param('email');
my $address  = param('address');
my $address2 = param('address2');
my $city     = param('city');
my $state    = param('state');
my $zip      = param('zip');
my $title    = 'mail test';
my $subject  = 'Using Sendmail';
my $phone1   = param('phone1');
my $phone2   = param('phone2');
my $table1   = param('Yes1');
my $table2   = param('No1');
my $qty      = param('Howmany1');
my @Decorations = param('Decorations');

if (sendmail()) {
   print "Thank you for your order";
}
else {
   print "There was a problem, please try again";
}
print end_html; 


sub sendmail {
   open(MAIL, "|/usr/sbin/sendmail -t") or return(0);
   print MAIL "To: $email\n";
   print MAIL "From: admin\@chef-2-go.net\n";
   print MAIL "Subject: $subject\n\n";
   ## Mail Body
   print MAIL "This is a test message from Yahoo! Web Hosting\n";
   print MAIL "Thank you $first $last\n";
   print MAIL "Your address is:\n";
   print MAIL "$address\n";
   print MAIL "$address2\n";
   print MAIL "$city $state $zip\n";
   print MAIL "TABLES(if any):  $table1 $table2\n";
   print MAIL "If yes, how many?  $qty\n";
   print MAIL "\n";
   print MAIL "DECORATIONS: " . join(' - ',@Decorations) . "\n";
   close (MAIL);

   open(MAIL, "|/usr/sbin/sendmail -t") or return(0);
   print MAIL "To: admin\@chef-2-go.net\n\n";
   print MAIL "From: $email\n";
   print MAIL "Subject: Request info for $first $last\n\n";
   ## Mail Body
   print MAIL "This is a request from $first $last\n";
   print MAIL "address:\n";
   print MAIL "$address\n";
   print MAIL "$address2\n";
   print MAIL "$city $state $zip\n";
   print MAIL "$phone1\n";
   print MAIL "DECORATIONS: " . join(' - ',@Decorations) . "\n";
   close(MAIL);
   return(1);
}
 
I would also validate any form fields that are required (like names and addresses) before sending the email.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top