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

I'm trying to send an email based on checkbox selected in my form. The vlaue I have in my HTML form are as followed...

Checkbox #1 -> name=m_eMailgroups vlaue=user1
Checkbox #2 -> name=m_eMailgroups vlaue=user2
Checkbox #3 -> name=m_eMailgroups vlaue=user3

The question is, how do I defined the address list that the script with use to send based on selection.

I've tried $smtp->recipient(@address); and I get the following error message...

Can't call method "recipient" on an undefined value

What am I doing wrong???

my %option_to_address = ( User1 => 'user1@domain.com', # note the single quotes User2 => 'user2@domain.com', User3 => 'user3@domain.com' ); my @addresses = map($option_to_address{$_},grep($option_to_address{$_} +,$query->param("m_EmailGroups"))); for my $address (@addresses) { ...What am I missing here.. }

Edit by tye, preserve paragraph breaks

Replies are listed 'Best First'.
Re: Sending email with Net::SMTP
by Thelonius (Priest) on Jul 14, 2003 at 21:06 UTC
    I've tried $smtp->recipient(@address); and I get the following error message...

    Can't call method "recipient" on an undefined value

    The error message indicates that $smtp is not defined. Have you created it at this stage in your program? Did you check the return value from Net::SMTP->new?

    I think your map grep statement is also wrong, but first things first.

    Update: Your map grep statement is okay, but make sure the values match exactly (including upper/lower case) between your CGI form and the hash keys.

Re: Sending email with Net::SMTP
by Excalibor (Pilgrim) on Jul 14, 2003 at 21:07 UTC

    Hi there,

    I'm not sure, but the following code, using your hash, gives this:

    my @addresses = map($option_to_address{$_},grep($option_to_address{$_} +, ('user1@domain.com', 'user3@domain.com'))); print "[[[@addresses]]]\n"; # prints => [[[]]]

    I'd review that grep(), the mapping doens't seem to be working...

    good luck,

    --
    our $Perl6 is Fantastic;

Re: Sending email with Net::SMTP
by Cody Pendant (Prior) on Jul 14, 2003 at 23:18 UTC

    I have to say I feel you're confused. Or maybe I am. If you have some email addresses coming in from a checkbox group, what more do you need?

    I don't understand, from the map and grep stuff, what the point of your script is at that part.

    If you have to send email to everyone in param("m_EmailGroups"), then split it on \0 and print it to the mail program. You obviously want to do something more complex? Please explain.



    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
      Hi There! Thanks for your input... What I'm trying to do is - I have a HTML form that has multiple checkboxes...i.e. Checkbox1, name=m_EmailGroups, and Value=user1. There's also checkbox2,3,4,5,6,7,8,9. All with the same name (m_EmailGroups) but different value. I want the user, when completing filling out the form to press send, and base on his check box selection an email should be sent to the user he has chosen... I tried creating a hash, and base on his checkbox selection in the HTML form, it would pull the value and send an email based on that... Hope this sheds some light on what I'm trying to do..Right now I'm totally confused and don't know that to do next..
        If you've got your email addresses from CGI.pm, then they should be concatenated into one long string with the \0 character in between items, unless I'm crazy.

        So you can make an array out of that param with

        @addressees = split('\0' $q->param($m_EmailGroups))

        or whatever.

        But it also sounds like you only ever expect them to choose one addressee? In that case you need Radio Buttons, not checkboxes, and you don't need to process it at all.



        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D