View Full Version : Email Setup and send to multiple people chosen in form

08-26-2006, 06:13 PM
I have a form designed that people complete, and can choose who they want the results to be emailed to. I think I have the script written correctly (for the most part) so it would work, but am not sure how to get the names you select from the drop down list, into the recipients. Do I need to set up a field in the DB to accomodate these. If so, can I have all the names go in one field, seperated by a comma, as there could be 15 - 20 names.

When developing the mail script, and working on the "FROM" section, does it have to be a valid email address, or can it just be a name? I have a query written to get the email address, but I am having troubles getting it to work, because of where I need to put quotation marks, etc., it doesn't like it.

any suggestions, what I have is below.

//Assign the Headers
$headers = "FROM:" mysql_query(Select Email from employees WHERE Name='$tech')"\r\n";
$subject = "Subject: Work Order Number ".$_POST['Work']." has been submitted\r\n";
$headers = "Content-Type: text/plain;\r\n charset=iso-8859-1\r\n";
$recipients = "";
$body = "Work Order Number".$_POST['Work'];
"Description of Problem:".$_POST['Issue'];
"Possible Resolution:".$_POST['Resolve'];
"Assistance Required:".$_POST['Assist'];
"Level of Severity:".$_POST['Severe'];
"The Complete Work Order can be viewed by visiting http://www.xxx.ca/GE/index.htm";

//Mail the Notification to recipients
mail($from, $recipients, $body, $headers);


08-26-2006, 07:33 PM
If it were me, I'd normalize the database by creating a new table for the email addresses and link it with a foreign key. Then I'd build the $recipients variable with a foreach loop.

08-26-2006, 07:41 PM
No database is needed for omething like this. there are two ways of doing it, one would be too add a drop down box and set the values to be the email address, as such:

<select size="1" name="to">
<option value="admin@mysite.com">Admin</option>
<option value="webmaster@mysite.com">Webmaster</option>

then in your PHP code you would do this:

$recipients = $_POST['to'];

Only thing about this is that a bot could index that page and spam those email address. An alternitive would be to store the emails in the php code like so:


<select size="1" name="to">
<option value="admin">Admin</option>
<option value="webmaster">Webmaster</option>


$recipients = $_POST['to'];
if ($recipients == "admin") {
$recipients = "admin@mysite.com";
} else if ($recipients =="webmaser") {
$recipients = "webmaster@mysite.com";

As for the from header im not sure, only way to find out for shore is to try it. I think i answerd what your looking for, if not let me know ;)

08-26-2006, 07:43 PM
Oh and by the way you are not understanding how queries work if you think that imbedding a call to the mysql_query() function in a variable assignment will work. Maybe this will help:


Step 1: Connect to the database (I assume you have this working fine)

Step 2: Perform the query using the mysql_query() function
This function requires a string containing your query text.
This function returns a resource on success, "false" on failure. A resource is a special variable, holding a reference to an external resource.

Step 3: Fetch data from your query's resource using one of the following functions:

There are more, but those are the main fetchers.

Can you see how you are misusing mysql_query() now?

08-26-2006, 08:03 PM
Hi There

thanks for the input, it is appreciated. I was trying to follow this through from a book I have, but have possibly misread how it works.

1. The people these people would be choosing to email to would be employees, hence they could change and this is why they are in a DB. The employee table is a separate table from the one where the rest of the form results go to.

2. I am getting the idea for the query's, but one question. If the form where the data is inputted, example tech name, work order #, etc., is the same form where the email is being generated from, would it still not work the way I have it set up. I am questioning this because i just reread the section from Beginning PHP5 and MySQL. Again, since the form being completed, and the email are the same, would the $_POST[] work, or will I need to have it stored in the DB first, then generate an email as a seperate step.

It is also my understanding that by using \n\r will reduce the chances of spam email and BOTS getting the addresses. Is this true? Is it also safer to keep the email addresses out of the actual script and have them pulled from a DB?

I guess it is back to the drawing board for this portion of the form.

Thanks for the input


08-27-2006, 01:37 PM
its not "safer" to get them from the database rather straight from your php file, infact it wouldnt make any difference saftey wise. As long as they are not on the main form itself then the bots wont be able to get the email address. Although there are some bots that can fill in the form and submit it, theres a topic on this just recently, you might want to read it for some solutions against this.