Page 1 of 1

Submitting Form Data to Multiple E-mails based on Form Entry

Posted: Tue Apr 27, 2010 5:01 pm
by mossberg
I'm a PHP newbie. I'm trying to figure out how to send a PHP online form to multiple email addresses based on what is selected (or entered) in a form.

For example, all completed form information will be sent to a single e-mail address - recipient@domain.com. But in the form, I have a section where people must select a particular varsity sport if they are interested in playing college athletics. If they select golf from the dropdown, the golf coach will also receive the form data by e-mail upon form submission. The same form data would be sent to recipient@domain.com and golfcoach@domain.com. Essentially, I have a list of sports and their associated coach e-mail addresses using a drop down. If no sport is chosen, then the e-mail only gets sent to recipient@domain.com.

Can anyone point me to some URLs that cover how to do this in PHP or offer any advice on how to approach this? I apologize for being a little vague here. I'm hoping to identify some online resources to learn how to do this.

Thanks for your help.

Mossberg

Re: Submitting Form Data to Multiple E-mails based on Form E

Posted: Tue Apr 27, 2010 6:21 pm
by mecha_godzilla
You could do this in a few ways I guess, but I'll give you some pointers and some example code and this might be enough to get you started.

Let's start with the basics:

You want to send the same form details to one or two recipients, based on the options that someone selects in your form. Because you are always sending one copy of the details to the same address (receipient@domain.com) you can set this as a variable in your script. To determine whether you need to send a copy to any other address you check the value of one of the drop-down menus in your web form (I'm assuming it's a drop-down menu and not a multiple selection field) and you use this to work out the other address to send the same details to. If all the email addresses are within the same domain then you can just send the 'golf' or 'soccer' part of the email address to your script and recreate the full address from it.

As an example, in your HTML code (where your form is) you could have something that looks like:

Code: Select all

<p>My favorite sport is:</p>

<select name="sport_choice">
	<option value="" selected>I don't like sport</option>
	<option value="golf">Golf</option>
	<option value="soccer">Soccer</option>
	<option value="tennis">Tennis</option>
</select>
To work out what email address to send the second email to, you have two options:

1. If the value of sport_choice is not NULL (or if sport_choice isn't empty) you can concatenate the value stored in sport_choice with another value stored in your script - you could call this $domain_name or something similar - to recreate the full email address. The code would look something like:

Code: Select all

$sport_choice = $_POST['sport_choice']
$domain_name = 'domain.com';
	
if ($sport_choice != "" || $sport_choice != NULL) {
	$sport_address = $sport_choice . '@' . $domain_name;
}
The problem with this script is that it can generate the wrong email addresses if a malevolent or mischievous user decides to send you some values from their own form. For example, if they set the sport_choice value to "john_doe" then your message - which might contain information other than the form details - will be forwarded to them as well because the full address is resolved to "john_doe@domain.com" (this assumes that they have an email address within the same domain of course.)

2. An alternative (and safer) option would be to use a switch statement to test what the value of sport_choice was and then set a specific value based on that. This is what the code looks like:

Code: Select all

$sport_choice = $_POST['sport_choice'];
	
switch ($sport_choice) {
	case 'golf':
		$sport_address = 'golf@domain.com';
		break;
	case 'soccer':
		$sport_address = 'soccer@domain.com';
		break;
	case 'tennis':
		$sport_address = 'tennis@domain.com';
		break;
	default:
		$sport_address = "";
}
You could then use the value of $sport_address to test whether to send the second email or not. Again, here's how that might look in your script (using PHP's mail function):

Code: Select all

$send_to_address = 'recipient@domain.com';
$subject = 'Details from my web form';
$do_not_reply_address = 'do_not_reply@domain.com';

$headers = "From: My Web Form <" . $do_not_reply_address . ">\r\n";
if ($sport_choice != "" || $sport_choice != NULL) {
	$headers .= "CC: " . $sport_address . "\r\n";
}
$headers .= "Reply-To: " . $do_not_reply_address . "\r\n";
$headers .= "Return-path: " . $do_not_reply_address;
	
$message = "MESSAGE:\r\n\r\n";
$message .= "Your message goes here";

mail($send_to_address, $subject, $message, $headers);
The function of the script should be obvious (and will hopefully work for you) but, in essence, if the value of $sport_choice is empty or NULL then the CC: address doesn't get added to the mail header. I'm not sure exactly what would happen on the server if the CC: was there but no email address was specified but it would be bad practice to do this as the mail could end up anywhere (although probably in the postmaster@ or info@ address if this is the main "drop box" for the account).

In addition to the code that I've already included, you need to make sure you validate (sanitise) the form input as well to avoid any misuse of the form, but get what you want to do working first then you can get some help on that if you need it.

I think that's covered everything but if you have any questions please let me know.

HTH,

Mecha Godzilla