1

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!

Karanvir.S.G
  • 70
  • 1
  • 11
  • 2
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – M. Eriksson Jun 20 '20 at 11:46
  • 2
    Do you have an email server installed and is PHP configured to see it? – RiggsFolly Jun 20 '20 at 11:54
  • @RiggsFolly erm... I don't think so; what is this email server you speak of? – Karanvir.S.G Jun 20 '20 at 13:13
  • @MagnusEriksson THANKS! This is awesome, I got to the Check the server's mail logs bit, but I'm not really sure what this server is – Karanvir.S.G Jun 20 '20 at 13:14
  • 2
    Then that woudl explain why email does not get sent. the `mail()` function does not actually send mail it just passes it to a mail server – RiggsFolly Jun 20 '20 at 13:14
  • @RiggsFolly That makes sense! So, i've got the data I want to be sent and I have it in a form that would be accepted by this server, do you know how I would go about setting up the mail sever? – Karanvir.S.G Jun 20 '20 at 13:42
  • 3
    Might be easier to use the library `phpMailer` – RiggsFolly Jun 20 '20 at 13:43
  • 2
    Are you doing this on Windows or Linux – RiggsFolly Jun 20 '20 at 13:44
  • @RiggsFolly I'm working on a windows machine at the moment. I've just checked out the the phpMailer git, I'm going to try the example and change up the important bits. Any advice? – Karanvir.S.G Jun 20 '20 at 13:52
  • 1
    Only to read the documentation – RiggsFolly Jun 20 '20 at 13:54
  • @RiggsFolly Thanks for pointing me in the right direction! It looks like the phpMailer has more functionality than I need. I'm going to carry down the mail() route and see if I can figure out whats going on – Karanvir.S.G Jun 20 '20 at 14:03

1 Answers1

1

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

Eric Qvarnström
  • 779
  • 6
  • 21
  • 1
    that would make sense! I don't have a mail sever or anything like that, can I just use my normal g-mail address? I've gone through that documentation but it doesn't offer much advice in terms of setting up said server. I think the mail() function is probs the best thing for what I want to do, but I'm not too sure where to go from here. This really is my first use of PHP in a project so this is all very new to me, pleas eli5! MUCH MUCH APPRECIATED! – Karanvir.S.G Jun 20 '20 at 14:48
  • @Karanvir.S.G. I understand! I would do as RiggisFolly mentioned above; Take a look at phpmailer, as far as I understand It can be used to send emails via SMTP. That would enable you to use your Gmail for example :) – Eric Qvarnström Jun 20 '20 at 15:00