Search code examples
htmlcssemailsendmailcontact-form

"Send" Button linked to contact form


I have a contact form in my webpage with 3 areas of name, e-mail and text (no subject), and a send button underneath the form. What should I do to make the button work? I want the user to write their Name (which would eventually become the subject of the e-mail), their e-mail and the text they want to send and I want to receive the e-mail from them with their name as a subject, their e-mail and the text they would have written as the body of the e-mail. Plus, I don't want the button to open the users e-mail, like outlook etc. I want it to happent silently. So the outcome would be the user writes the text in the contact form, presses send and then I get the e-mail.

Thank you a lot, in advance!!!

Rafael.


Solution

  • First you have to learn php to make it work. You can not send mail using only form design. Also, you have to upload your files on the server. Sending mail requires SMTP, which will not work on localhost.

    HTML:

    <form action="mail_handler.php" method="
    FullName: <input type="text" name="full_name"><br>
    Email: <input type="text" name="email"><br>
    Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    

    mail_handler.php

     <?php 
        if(isset($_POST['submit'])){
            $to = "[email protected]"; // this is your Email address
            $from = $_POST['email']; // this is the sender's Email address
            $full_name = $_POST['full_name'];
            $subject = "Form submission";
            $subject2 = "Copy of your form submission";
            $message = $full_name . " wrote the following:" . "\n\n" . $_POST['message'];
            $message2 = "Here is a copy of your message " . $full_name . "\n\n" . $_POST['message'];
    
            $headers = "From:" . $from;
            $headers2 = "From:" . $to;
            mail($to,$subject,$message,$headers);
            mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
            echo "Mail Sent. Thank you " . $full_name . ", we will contact you";
            }
        ?>
    

    I suggest you to watch some tutorial before implementing it. You need to understand php before implementing it.