Reputation: 21150
I have a PHP email script on my website that potential clients use to contact me. Got it from Stack and tweaked it a little (has a honeypot).
PHP CODE
<?php
$robotest = $_POST['robotest']; //just testin' for robots
$recipient = "[email protected]"; //recipient
$email = ($_POST['email']); //senders e-mail adress
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
$Name = ($_POST['name']); //senders name
$mail_body = ($_POST['comments']); //mail body
$subject = "Image Consulting Inquiry"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header); //mail command :)
} else {
print "You've entered an invalid email address!";
}
?>
FORM HTML CODE:
<form name="contactform" id="contactform" method="post" action="/contact/" _lpchecked="1">
<ul class="form-block">
<!-- HONEYPOT -->
<li class="on-no-robots" style="height:0px; text-indent:-9999px; font-size:0px; overflow:hidden;">
<label>Humans Don't Submit This!! If you can see this, you don't have CSS, and you scare me. This is just here to filter out automated comments!</label>
<input name="robotest" id="robotest" type="text" />
</li>
<!-- HONEYPOT -->
<li class="third">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" class="required" />
</li>
<li class="third">
<label for="email">Email Address</label>
<input type="email" name="email" id="email" value="" class="required email" />
</li>
<li class="third">
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone" value="" />
</li>
</ul>
<h3>How can we help you?</h3>
<ul class="form-block">
<li class="full">
<textarea name="comments" id="comments" class="required"></textarea>
</li>
<li>
<input id="submitButton" type="submit" value="Talk to us" onclick="_gaq.push(['_trackEvent', 'Conversions', 'Contact', 'Contact Form']);" />
</li>
</ul>
</form>
I also have been using Google Analytics' event tracking on the "submit" button of the form, as follows:
<input id="submitButton" type="submit" value="Talk to us" onclick="_gaq.push(['_trackEvent', 'Conversions', 'Contact', 'Contact Form']);" />
I can test an email and it works fine. However, looking at my analytics, there are a few days where people have clicked the "send" button (triggering the analytics event) but I've not received an email. This may be because they haven't filled out the form correctly, but I'm not sure. There is nothing in my Gmail spam filter.
Is there any way to (a) check with my host to see if there's any 'trapped' email that hasn't been sent correctly, and (b) tweak the code so that these false positives (if they are false positives) don't occur?
Obviously the most simple cause would be people just clicking the "submit" button for fun without having entered anything, thus triggering the analytics event. Each of these conversions is worth around $1500 so I'd like to be as certain as possible that they're not genuine conversions that aren't getting emailed to me!
Cheers guys.
Upvotes: 0
Views: 320
Reputation: 3070
Modify your script like this. Test it out and see what prints.
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
$ret = mail($recipient, $subject, $mail_body, $header); //mail command :)
var_dump($ret);
If you get 0 / false etc, mail() is not enabled on your server. If you see true / 1 etc, most likely your mail is going to spam. If you do not get anything printed on the page (or rather "You've entered an invalid email address!" printed... Overlooked that sorry), that means it is just not entering this loop
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
Tell me what you see?
Upvotes: 1
Reputation: 4872
To see any 'trapped' emails that haven't been delivered, you'd probably have to check your mail server (get onto your host).
as for ensuring your mail function is working correctly, you could always create your own catch for when it fails, something like:
if (!mail($recipient, $subject, $mail_body, $header))
{
/*For when mailing fails*/
/*Log contents somewhere*/
}
Perhaps when it fails store a log in a db with it's contents for future review? You could always check your error log for more clues. Perhaps the mail server being unstable/down at that particular time
Upvotes: 3