Forum Moderators: coopster
I am trying to make a drop down menu for people to choose which one of our departments will receive the form.
I discovered this code to create the drop down menu.
<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>
So I tested it with the rest of the code. When I preview the form before clicking submit, it comes up displaying the email address it is going to be submitted to, but it's never sent to the specified inbox.
Do I need to put code in the 'process' file? If so, what code do I need to put in to make the form go through properly?
mail($to,$subject,$txt,$headers);
}//if isset
?>
</body>
</html>
then in "process.php" you would have this code
<?php
//if they click submit
if(isset($_POST['submit'])){
$to = $_POST['recipient'];
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
}//if isset
?>
You will notice in the html form tag I added onsubmit="process.php" that's how you tell it to execute a file when the user clicks submit. However, you can just use the code I posted all in one file if you want. As long as the file extension is .php.
<form name="myform" method="post" onsubmit="process.php">
You will notice in the html form tag I added onsubmit="process.php" that's how you tell it to execute a file when the user clicks submit.
It may work in some browsers... but the "onSubmit" attribute is a Javascript event; when the form is submitted, the browser will execute whatever Javascript code is in the HTML form's attribute "obSubmit".
To tell the form to use a separate file to "send the data to", you should specify the HTML form tag as:
<form name="myform" method="post" action="process.php"> The correct attribute is "action". You can specify any URL as the value (in other words, you can send the form data to any page or website).
Now for the original poster... I assume you have a setup where you have a HTML page with a form, and a separate PHP file which is used to process it? Assuming you have your HTML form sending the form data to the right PHP file, in order to send the email to the "selected" email address, you would need to do the following:
(1) retrieve the form field's value for where to send it [$sendTo = $_REQUEST['recipient'];];
(2) make sure the PHP function 'mail()' is being called to send the email; and be sure that the "recipient" value is passed as the proper argument to the function, such as: mail($sendTo,$subject,$message,$headers);
Assuming this processor script is already coded, it should already have this function in it. You just need to add/modify the code so that you "retrieve" the 'recipient' value from the form data, and then set it as the "to" argument in the PHP mail function.
Hopefully that makes sense.
<select name="recipient" size="1">
<option value="email@domain.com" selected>001</option>
<option value="email@domain.com">002</option>
</select>
This exposes these email addresses to spammers, and you'll have more offers for Rolex watches, diplomas, and v***ra than you'll know what to do with.
Do this instead. In your form.php,
<select name="recipient" size="1">
<option value="1" selected>001</option>
<option value="2">002</option>
</select>
Then in your process.php, right at or near the top of the script, create an associative array connecting the numbers with the addresses:
$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);
The rest is as andrew describes, with one small change. You need to reference the submitted "recipient" as the key of the $emails array:
$to = $emails[$_POST['recipient']];
//$to is now "address1@domain.com" if 1 is selected
This keeps your email addresses out of public view.
I used this in my form:
<select name="recipient" size="1">
<option value="1" selected>001</option>
<option value="2">002</option>
</select>
and then...
$to = $emails[$_POST['recipient']];
//$to is now "address1@domain.com" if 1 is selected
I tested it on my website and everything worked fine. I went back to test it again and then when I clicked submit it said I had an error and wouldn't let me proceed? I don't get why it would have worked and then stop working?
I then put this in the process file:
$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);
I then put this in the process file:
$emails = Array (
1 => 'address1@domain.com',
2 => 'address2@domain.com'
);
Sorry if this is a stupid question, but did you change the email address values to your real email addresses?
A 500 on a PHP script generally means some other function within the script is kicking the 500 - and I'm guessing that might be the mail routines.
Do you know if PHP error reporting is in for your server? It's off by default as a security issue, hackers can glean a lot about a server's configuration by causing a script to error.
First stop is your error logs. If you have command line /SSH access,
tail /path/to/domain/error_log
Will give you the latest logs. Otherwise you can get to it via any control panel you have.
Next put this at the top of your script - but DO NOT LEAVE IT THERE when it's deployed live:
error_reporting(E_ALL);
ini_set("display_errors", 1);
I took andrew's code and whipped something up. Copy the entire code below, paste in "mailer.php." It has been tested with real email addresses. Upload and test.
Note how the entire deal - form and all - is in one file. Study the comments, and do not use it as is for a live application. Once you get a handle on what's happening, explore the term email injection php on the search engine of your choice.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype on one line -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Mailer Example</title>
</head>
<body>
<!-- CAUTION: This is for a simple mailer example only
and has no input filtering, no error checking, and is likely
subject to email injection! -->
<?php
// It's standard to put all configurable variables right
// at the top of your script, where you can find them.
// Define these first. Note how the form is output.
$emails = Array (
1 => 'tech@example.com',
2 => 'assist@example.com'
);
// Don't put the email addresses in the response
// or the form. Use this array.
$persons = Array (
1 => 'John Doe',
2 => 'Mary Doe'
);
// Who it's to
$to = $emails[$_POST['recipient']];
// If there's no email entered, you need a from.
$default_from = 'no-reply@example.com';
// Who it's from
$from = (isset($_POST['from']))?$_POST['from']:$default_email;
$subject = "Test email to " . $persons[$_POST['recipient']];
// If it's been submitted, process
if (isset($_POST['submitButton'])) {
$txt = ($_POST['msg'] != '')?$_POST['msg']:'No message was entered';
// This overwrites "$txt" but puts the
// message stored in $txt at the end.
// Note how it's got $txt after "Message." This is
// the beauty of how variables are interpolated first,
// then stored in the variable on the left, opposite of how we
// read them.
$txt = "
From: $from
subject: $subject
Message: $txt
";
$headers = "From: $from\r\n";
mail($to,$subject,$txt,$headers);
$response = '
<h1>Email Successfully Sent</h1>
<p>The email was successfully sent to ' .
$persons[$_POST['recipient']] . '.</p>';
print $response; // or echo
}
// If it's not been submitted, output the form
else {
$form = '
<form name="myform" id="myform" method="post" action="mailer.php">
<p><label for="recipient">Select Recipient:</label>
<select name="recipient" id="recipient">
<!-- don\'t need selected in first list item -->
';
foreach ($emails as $key=>$value) {
$form .= '<option value="' . $key . '">' . $persons[$key] . "</option>\n";
}
$form .= '
</select></p>
<p><label for="from">Your Email:</label>
<input type="text" name="from" id="from" size="25" value=""></p>
<p><label for="msg">Message:</label>
<textarea name="msg" id="msg" rows="3" cols="25">
This is a test email. This is a test email.
This is a test email. This is a test email.
</textarea></p>
<!-- note change of submit name, never use possible reserved words -->
<input type="submit" name="submitButton" id="submitButton" value="submit">
</form>
</body>
</html>
'; //End form
print $form;
}
?>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
So, I can take this code, put it in dreamweaver and then just add the form components I need
[IMO]
For the love of [whatever deity you follow] do not edit this in Dreamweaver. DW is a WYSIWYG html editor, it is not really designed to modify scripts. Use a powerful text editor - I still use HomeSite, if you have the original disks for DW before Adobe took over Dreamweaver, you will find it on the disks. If you don't have anything else, copy and paste the script into notepad.
[/IMO]
then add email injection php
I was suggesting you search google for these terms, learn what email injection is, then learn how to prevent it, and implement it in your script.
For now, just copy and paste the example program into notepad, save it as "mailer.php" (note the action of the form.) Change the email addresses to your emails. Upload and test.
Yes, it's an all-in-one-file, form and script. Test it out, study it, see cause and effect when you run it. Baby steps to coding. :-)