I need help with my email form php code

Hi, this is the first thing I’ve tried with php so I’m a bit confused. I found some tutorials online and took different elements from them all for my email form, and also tried to do that with the code but the frankencode doesnt work. I want each different thing on the drop down menus to be able to go to a different email address. Right now theyre all set on the same email so I can test it.
The website is here


and heres my code

<?php $mailto = $_POST['me']; if($mailto == 'info') { $subject = "Question/Comment"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $email = "[email protected]"; }elseif($mailto == 'complaints'){ $email = "[email protected]"; echo "Email has been submitted to $to!"; mail($to, $subject, $body); }; ?>

Hi there,

Firstly, where you’ve put $to I think you were supposed to put $email - $to hasn’t been declared.
I have gone ahead and made changes to your code as I saw fit.

$type = ($_POST[‘me’] != “select”) ? $_POST[‘me’] : “info”;

    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];
    $email = "[email protected]";

    if($type == 'info')
        $subject = "Question/Comment";
        $body = "    Name: ".$name_field."\n
                    Email: ".$email_field."\n
                    Message: ".$message;
    if($type == 'complaints')
        $subject = "Complaint";
        $body = $name_field." (".$email_field.") has made a complaint:\n\n

    echo "Email has been submitted!"; //You may not want to tell them what your email address is!

    mail($email, $subject, $body);


If you have any questions or problems with the code, just let me know although all I’ve really done is shift bits about.

thanks for your help, however im still having problems. i tested it and got an email in my spam folder that may have been it, but it didnt include any information i put in the fields or the message. i also have several different emails that i want to use, one for each subjuct. heres my html code.



Subject: Select Questions/Comments Complaints


For Clients



Subject: Select Support General


Hi there, check out the code below:


<?php error_reporting(E_ALL); if(isset($_POST['form_sub'])) { $type = ($_POST['me'] != "select") ? $_POST['me'] : "info"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; /* * Create a more specific set of emails */ $emails = array( "info" => "[email protected]", "complaints" => "[email protected]", "support" => "[email protected]", "general" => "[email protected]" ); $email = $emails[$type]; /* * Or simply... */ $email = $type."@host.com"; if($type == 'info') { $subject = "Question/Comment"; $body = " Name: ".$name_field."
Email: ".$email_field."
Message: ".$message; } if($type == 'complaints') { $subject = "Complaint"; $body = $name_field." (".$email_field.") has made a complaint:

".$message; } $status = mail($email, $subject, $body, "From: [email protected]; \r\n Content-Type: text/html; charset=ISO-8859-1;"); echo ($status) ? "Email has been sent!" : "There was a problem in sending the email."; } ?>


That’s the php (you’ll notice I added some header information in the mail() function - this is only because I personally prefer HTML emails, but you may want to remove that if you are thinking of viewing them on a phone).

In terms of the HTML all I did was remove this from the form tag:


And changed the submit button to:

<input type="submit" name="form_sub" />

Give that a go, and let me know what happens.

Sponsor our Newsletter | Privacy Policy | Terms of Service