简体   繁体   中英

E-mail contact form data with PHP

I have a html form and I want to take the data that is entered and send it to myself in an e-mail, I'm not at all familiar with PHP but after some Googleing it seemed to be the way to go.

I'm not too sure what's not quite working, but any insight would be awesome!!

HTML:

    <section class="contact" id="contact">
        <div class="container">
            <div class="section-heading">
                <h1 data-aos="fade-right" data-aos-delay="150">Contact</h1>
                <h6 data-aos="fade-left" data-aos-delay="150">Contact Me</h6>
            </div>
            <form method="post" name="contact_form" action="contact-form-handler.php" data-aos="fade-up" data-aos-delay="200" onsubmit="return false">
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" placeholder="Enter Your Name..." required>

                <label for="name">Email:</label>
                <input type="email" id="email" name="email" placeholder="Enter Your E-mail..." required>

                <label for="number">Contact Number:</label>
                <input type="number" id="number" name="number" placeholder="Enter Your Contact Number...">
                
                <label for="message">Message:</label>
                <textarea name="subject" id="subject" cols="10" rows="10" placeholder="Enter Your Messgage..."></textarea>
                <input type="submit" value="Submit" onclick="sendContact();">
                


            </form>
            <?php include 'contact-form-handler.php';?>
        </div>

PHP: [separate file in same directory called contact-form-handler.php]

<?php
if(!empty($_POST["submit"])) {
    $name = $_POST["name"];
    $email = $_POST["number"];
    $subject = $_POST["email"];
    $content = $_POST["subject"];

    $toEmail = "admin@phppot_samples.com";
    $mailHeaders = "From: " . $name . "<". $email .">\r\n";
    if(mail($toEmail, $subject, $content, $mailHeaders)) {
        $message = "Your contact information is received successfully.";
        $type = "success";
    }
}
?>

Again, any kinda advice is very appreciated!

Everything seems fine in your HMTL. But I think that you misunderstood the PHP part about sending an email.

The "FROM:" field in your header should be the address that you own in the mail server, see the example below.

Also, setting the content type and charset is recommended:)

<?php
$mailHeaders = "Content-type:text/html;charset=UTF-8" . "\r\n";

if(!empty($_POST["submit"])) {
    $name = $_POST["name"];
    $email = $_POST["number"];
    $subject = $_POST["email"];
    $content = $_POST["subject"];

    $toEmail = "admin@phppot_samples.com";
    $mailHeaders .= "From: <Your@DomainName.com>\r\n";
    if(mail($toEmail, $subject, $content, $mailHeaders)) {
        $message = "Your contact information is received successfully.";
        $type = "success";
    }
}
?>

Furthermore, I would recommend to read threw the documentation:) https://www.php.net/manual/en/function.mail.php

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