Re: Multiple Send

by "Louise Dade" <louise.dade(at)ntlworld.com>

 Date:  Fri, 15 Feb 2002 12:55:11 -0000
 To:  <hwg-techniques(at)mail.hwg.org>
 References: 
  todo: View Thread, Original
Hi Annique,

> Does anyone know of a script (CGI preferably) that will sendmail to
> different recipients based on the selections in a form?
>
> For example these choices in a drop down:  broker would go to
broker(at)xyz.com
>
> employment would go to hr(at)xyz.com

Yes this is possible, but must be done carefully!  It is not advisable to
hardcode the email address into your actual form select box because it
leaves your script wide open to abuse from spammers (they could use your
form directly and just insert their own "to" details from their spam list).
That's why it's never advisable to have the "to" field as an option the user
can type in, or as a hidden field in the form.

Here is the good news!  There is a way of allowing users to select a "to" by
altering an ordinary formail (Matt's is as good as any, just make sure you
have the latest version which checks for referers) using indirect selection.

What this means is:

1.  In your html form, make a select box where each recipient has a
numerical value.

<select name="recipient">
<option value="0">Sales</option>
<option value="1">Marketing</option>
<option value="2">Webmaster</option>
</select>

2.  In the cgi script create an array which will contain your list of email
address.  I'm using an array here because it is easiest for people to edit
if they need to change the script, but it could also be done with hashes
(associative arrays) or "if X then do Y".

# Array of recipients - must in the same order as your form select list.
@recipients = (
    "sales(at)your-url.com",
    "marketing(at)your-url.com",
    "webmaster(at)your-url.com"
)

Then your "to" field in sendmail can be selected by the following code.

# Grabs the number of the selected recipient from the form
$recipient = $input{'recipient'};

# Uses that number to select the corresponding person out of the recipients
array
$to = $recipients[$recipient];

So for example, if "Sales" was selected from the html form, it's value is
"0".  So our "to" field becomes $recipients[0] which is the first email
address in our recipients array.  Arrays start at zero, so it is important
that in your form you start numbering choices from zero.

That's the very basic scripting, and it might need to be edited a little for
variable names etc, in order to work with whatever cgi script you decide to
use.

Louise
================================
www.classical-webdesigns.co.uk
www.classical-webdesigns.co.uk/falco/ - the Roman sleuth!
www.qmt.org.uk - The Queen Mother Theatre

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA