-1

I am a noob when it comes to PHP, and barely above that when it comes to html and css. I have a simple form for potential customers to fill out, hit send, and it should send me an email. There are a multitude of how-to's on this subject and I used several to come up with my code. Initially I had limited success in the sense that I could get the email to send, but it wasn't grabbing any of the variables (name, email addy, phone, etc). I was receiving blank emails in my inbox from an unknown sender. I fixed the syntax so that it began grabbing the form data, but now the email never arrives. I know that it is getting the variables because after you hit send, the data appears on a "results page" (i have since removed that feature, but made no other changes).

I have checked my SPAM folder. It's sending to a gmail address, and I understand gmail is forgiving when it comes to delivering suspect email. I have submitted my php code to http://phpcodechecker.com/ and it comes back clean. The server that hosts my site is running php 5.2, and an SMTP service.

Would anyone be willing to review my code and point out where I went wrong?

 <?php
/* Variables */
    $emailSubject = 'Quote request';
    $webMaster = 'myaccount@gmail.com';

/* Data */
    $email = $_POST['email'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $box = $_POST['box'];


    $body = <<<EOD
<br><hr><br>
Email: $email <br>
Name: $name <br>
Phone: $phone <br>
Request: $box <br>
EOD;

    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($webMaster, $emailSubject, $body, $headers);

/*Results*/

    $Results = <<<EOD
<html>
<head>
<title>Email Sent!</title>
<style type ="text/css">
body {
    background-color: #0252aa;
    font-family: Verdona, Arial, Helvetica, sans-serif;
    font-size: 14px;
    color: white;
    }   
div {
    padding-left: 50px; padding-top: 40px;
}
</style>
</head>
<div>
    <div align="left">Thank you for your email.</div>
</div>
</body>
</html>
EOD;
echo "$Results";
?>

This is the relevant html:

 <form class="form" method="post" action="email2.php">
            <p class="name">               
              <input type="text" name="name" id="name" placeholder="Luke Santos">
                <label for="name">Name</label>
            </p>
             <p class="email">               
              <input type="email" name="email" id="email" placeholder="LukeSantos@example.com">
                <label for="email">Email</label>
            </p>
                    <p class="phone">               
              <input type="text" name="phone" id="phone" placeholder="(262) 555-1212">
                <label for="phone">Phone</label>
            </p>

            <p class="box">
              <textarea name="box" placeholder="What can we help you with?"></textarea>
            </p>                
            <p class="submit">
                <input type="submit" value="Send">
                <input type="hidden" name="submitted" value="TRUE" />
            </p>
           </form>  
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Reason: You're not checking for empty fields. People can just click submit, then BAM... sent with nothing. Probably even ignored by some mailers because of it. – Funk Forty Niner Aug 20 '15 at 18:25
  • You should also place this `EOD; echo "$Results";` inside your body/html, not outside. It's just good coding form ;-) – Funk Forty Niner Aug 20 '15 at 18:26
  • if(isset(checkhere){ send here if set } – Joshua Nightingale Aug 20 '15 at 18:33
  • `i(!empty(field)){...}`... better. Including `if(isset(submit)){...}` and naming it. – Funk Forty Niner Aug 20 '15 at 18:33
  • Thanks Fred -ii- , the only empty emails I received were from me testing. They were empty because I had a syntax problem (Using $POST instead of $_POST in my php). But I should add the check for empty, I agree. I will move the EOD; to inside as you suggest. I will need to study the if(isset... I don't know how to use that. – Todd Wollin Aug 21 '15 at 13:40

1 Answers1

0

I can't see a problem in your code, causing this behavior... You could try a few more things:

  • Check, if $success is really true *, e. g. like that: if ($success !== true) { die("Could not send mail. :("); } I would put this line straight after that one with $success = ....

  • Enable error reporting (error_reporting(E_ALL); -- Warning: Not for production use) and see if it says something. Put it on top of your script as the first line.

  • If you have access, check your mail log and server log / php log
  • Anyway, PHPs mail function is quite rudimental. In the past, I have successfully avoided problems like empty mails in some clients (like Outlook) using the SwiftMailer extension. Perhaps, you should give it a try.

*If success is true, unfortunately that doesn't mean your mail was sent correctly, but just that is was accepted for delivery by the service trying to send your mail afterwards. So there's no way in PHP to verify, that your mail was really sent (and/or received). :/

And Fred -ii- is right, if some of the fields (esp. email) are empty, things might go wrong.

And btw, "Verdona" should possibly be "Verdana". ;)

ahuemmer
  • 1,653
  • 9
  • 22
  • 29
  • Thank you ahuemmer. I think the downside to having my site hosted by someone else is not being able to reach the log files. I can request that of the host, but I'm betting the answer is no. Definitely meant to use Verdana. LOL Being such a noob as I am... Can you elaborate on how I go about checking if $success is true? – Todd Wollin Aug 21 '15 at 13:44
  • Sure, I edited mit original post accordingly. Hth! – ahuemmer Aug 22 '15 at 08:46