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

Dear Perl Monks, I have a problem that needs your wisdom and urgent help... I've tried to create a web based CGI form that should sends out emails to multiple email addresses when one, or multiple checkbox(s) is selected. It works fine when one checkbox is selected, but when more than one is selected it does not work. If you check more than one box, it won't send to anyone because it wouldn't be equal to 'Email_Group1' for example.. it would be equal to something like 'group1@domain.com - group2@domain.com' if both of those checkboxes where checked. Please let me know how to make the script be able to send out to one, or multiple email address if one or more checkboxes are selected. I have not been able to resolved this for a couple of days now and have come to a dead end. Thanks so much! Below is an example of the cgi code...
if($in{'chkboxname1'} eq 'Email_Group1'){ $in{'form-to'} = 'group1@domain.com'; } if($in{'chkboxname2'} eq 'Email_Group2'){ $in{'form-to'} = 'group2@domain.com'; } if($in{'chkboxname3'} eq 'Email_Group3'){ $in{'form-to'} = 'group3@domain.com';
  • Comment on CGI/PERL email form that will send to multiple address based on checkbox selection
  • Download Code

Replies are listed 'Best First'.
Re: CGI/Perl email form that will send to multiple address based on checkbox selection
by chromatic (Archbishop) on Jun 18, 2003 at 16:26 UTC

    If you were to use CGI.pm's param() method to retrieve form values and if you were to name all checkboxes the same, you could use a hash slice. Any day you can do that is a good day.

    my @mail_groups = $q->param( 'mailgroup' ); my %group_addresses = ( group1 => 'group1@domain.com', group2 => 'group2@domain.com', group3 => 'group3@domain.com', ); my @mail_addresses = @group_addresses{ @mail_groups }; my $to = join(', ', grep { defined } @mail_addresses );
Re: CGI/PERL email form that will send to multiple address based on checkbox selection
by lnl (Pilgrim) on Jun 18, 2003 at 16:25 UTC
    Try this (a little condensed)...

    if($in{'chkboxname1'}){
    $in{'form-to'} .= ',group1@domain.com';
    }
    if($in{'chkboxname2'}){
    $in{'form-to'} .= ',group2@domain.com';
    }
    if($in{'chkboxname3'}){
    $in{'form-to'} .= ',group3@domain.com';
    $in{'form-to'} =~ s/^,//;

    --lnl

      Thank you for your help, this worked....you are the man! My thanks goes to everyone for their time.
Re: CGI/PERL email form that will send to multiple address based on checkbox selection
by ant9000 (Monk) on Jun 18, 2003 at 14:43 UTC
    You are writing over $in{'form-to'}, which does not seem appropriate... what about this:
    my @form_to=(); if($in{'chkboxname1'} eq 'Email_Group1'){ push @form_to, 'group1@domain.com'; } if($in{'chkboxname2'} eq 'Email_Group2'){ push @form_to, 'group2@domain.com'; } if($in{'chkboxname3'} eq 'Email_Group3'){ push @form_to, 'group3@domain.com'; } $in{'form-to'}=join(',',@form_to) if(@form_to);
    It's untested, but should work.
      Tried the code, it works but only sends to the first email address if I select more than one. For some reason I doesn't know or / want to send to more than one email address if one or more check boxes are selected. Am I doing anything wrong? Thanks!
        maybe try seperating the addresses by commas ie
        $address = "me@you.com, you@me.com";
        I know that's not the same format you're using but you see what I'm getting at. if your variable you're pushing onto is not empty then push a comma, and the address, otherwise just push the address.. I could be way off-base, but that's my two cents.
        Try and print the value of $in{'form-to'} of the version I suggested you: does it contain all of the required addresses? It should, as far as I can tell... so I'd suggest to check the code that actually sends the emails.