简体   繁体   中英

HTML Form won't send email on submit

I've developed an iOS app and rails backend server. While I took some HTML classes back in university, it's been a long while since I've touched any.

I purchased a 'template' website to be my application landing page. I've tweaked it to be what I want, but I'm having 1 issue with the Contact/Form Submission page. When I press send, I do not receive an email at the intended email address. I'm not sure if this is an issue with the code (I'm guessing not, I would think this highly rated template would have had something like this correct), or if I need to set up something with my domain that I currently haven't done as I wouldn't know about it.

Here's the relevant code...

index.html

<form action="javascript:;" method="post">
    ...
    <input type="submit" class="button white" value="Send &#x2192;" />
</form>

send_email.php

<?php

    // Replace this with your own email address
    $to="example@gmail.com";

    // Extract form contents
    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    // Validate email address
    function valid_email($str) {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

    // Return errors if present
    $errors = "";

    if($name =='') { $errors .= "name,"; }
    if(valid_email($email)==FALSE) { $errors .= "email,"; }
    if($message =='') { $errors .= "message,"; }

    // Send email
    if($errors =='') {

        $headers =  'From: FluidApp <no-reply@fluidapp.com>'. "\r\n" .
                    'Reply-To: '.$email.'' . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
        $email_subject = "Website Contact Form: $email";
        $message="Name: $name \n\nEmail: $email \n\nWebsite: $website \n\nSubject: $subject \n\nMessage:\n\n $message";

        mail($to, $email_subject, $message, $headers);
        echo "true";

    } else {
        echo $errors;
    }

?>

However, nothing is being sent to my email "example@gmail.com".

What might I be missing here?

Maybe replace

<form action="javascript:;" method="post">

with

<form action="send_email.php" method="post">

That should do it, assuming the ... in your HTML snippet contains the right variables for the form submission- name , email , website , subject , message , etc.

EDIT: OP figured it out.

Place the <form action="send_email.php" method="post">

and remove <form action="javascript:;" method="post"> <form action="javascript:;" method="post">

it will work perfect your send_email.php is good

place all 2 files in same folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM