0

I want to be able to collect all the data from the form of my site that is already live and send it to my email.

I based myself in this question here and adjusted my php based on that. However, when I click on submit button, the fields get empty but when I check my email I receive nothing.

How can I collect the data from the site and send to my email?

My code looks like this:

<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
    </div>
    <div class="form-group">
    <select name="situation" id="situation">
        <option value="Unemployed">Unemployed</option>
        <option value="Employed">Employed</option>
    </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

    <?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
            $subject = $_POST['subject'];
            $situation = $_POST['situation'];
            $from = 'sidney@web2web.co.za'; 
            $to = 'sidney@web2web.co.za'; 
            $subject = '$subject';

            $body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";

            // set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers optional/headers 
            $headers .= "From:$from";

            if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
            }
        }   
    ?>
 </form>
user agent
  • 2,762
  • 6
  • 29
  • 74
  • Try `SMTP` mailer [Reference](https://github.com/PHPMailer/PHPMailer). – Jaydeep Mor Jun 22 '17 at 07:14
  • Check if your mail function sends the mail correctly. Set an `echo $result` behinde your `if(mail(...))`. Run the script on your local maschine? If yes set up your mail server, to send mails or look for an test mail server, to simulate the mail transfer. – Richard Jun 22 '17 at 07:15
  • I just noticed that the code actually works, but it takes too long for me to receive the email. The only thing I see in the email is that all is printed in one line, meaning that the \n is being ignored – user agent Jun 22 '17 at 07:45

3 Answers3

0

PHP requires an installed and running mail system. The program to be used is defined by the configuration settings in the php.ini file.

赵加兴
  • 34
  • 4
0

First you need change your PHP mail configuration:

  1. Open your php.ini file.
  2. Search [mail function].
  3. Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
  4. Save the php.ini file.
  5. Restart your server.

You can use PHPMailer For mail

this is probably the world's most popular code for sending email from PHP!

0

Your Code is perfect. i checked it after some small changes, Like you put $subject in single quotation marks which is wrong , and you also forgot to print the output which is $result . other wise your code is perfect.

Run this code, if it shows error then please enable SEND MAIL option to your server.

<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
    </div>
    <div class="form-group">
    <select name="situation" id="situation">
        <option value="Unemployed">Unemployed</option>
        <option value="Employed">Employed</option>
    </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

    <?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
            $subject = $_POST['subject'];
            $situation = $_POST['situation'];
            $from = 'sidney@web2web.co.za'; 
            $to = 'sidney@web2web.co.za'; 
            $subject = $subject;

            $body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";

            // set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers optional/headers 
            $headers .= "From:$from";

            if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
            }
            echo $result;
        }   
    ?>
Sunil Rajput
  • 898
  • 9
  • 19