Solved

Logs for PHP form

Posted on 2013-06-18
8
149 Views
Last Modified: 2013-10-21
I have a PHP form with a submit button which will be sent as email to xxx@abc.com after click the submit button. I would like to have log or alert message whenever there is a mail problem or server down. Now when a customer submit the form and a message showed sending successful to her but in actual the mail was not reach into the xxx@abc.com mailbox and we do not know where the mail has gone and we even do not know there is a failure of the form. How am I going to create an alert to myself or log in order to know the failure of the form?
0
Comment
Question by:swpui
  • 4
  • 3
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Sorry, but you're looking for a unicorn here.  Email is unbelievably complicated.  There are so many things that can go wrong with form-to-email, that you may never find the culprit(s) that cause your trouble.  Example: Is the server properly configured to send email?  Are the internal DNS pointers correct?  Are the SPF records accurate?  Is the server at an IP address that is in a blacklist block?  Is there a spam filter at the recipient end?  Has the recipient whitelisted the sending domain?  If the server is down, how will the server be able to tell you it's down?  Things like that can make email a poor choice for communications.

Form-to-email scripts are notorious for being unreliable, getting hacked, etc.  This code will illustrate the principles of a simple form-to-email script.  You could try tailoring it to your needs.

<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";


// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // DISABLED ON THE SERVER SIDE
    var_dump($_POST);
    die(' DISABLED');

    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }

    // PRODUCE THE THANK-YOU PAGE
    echo '<p>THANK YOU</p>' . PHP_EOL;
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;


// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '"'                // KEEP DOUBLE QUOTES
    . "'"                // KEEP SINGLE QUOTES
    . '@&+:?_.,/\-'      // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

Best of luck with it, ~Ray
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I agree with Ray.  And if you use the PHP mail() function, the Only indication you get is that your email was sent to the local mail server.  There is Nothing that will tell you whether it was sent or received.
0
 

Author Comment

by:swpui
Comment Utility
What about something like whenever there is user keying in the form and press submit button, the form will automatically been saved a copy no matter the email was successful or not?
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
I do that with order forms, I save a copy of the order email in a database.  It's only 'automatic' because I did the programming to make it happen.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:swpui
Comment Utility
I also make a database to keep whatever customer submit, it works. Please provide me a way to get an alert mail to send to me whenever the customer press the submit button and the email did not reach us? This is to eliminate me from everyday or every second must log into the database to check data, furthermore if the data is available in my database, I also would not know whether it was a successful/fail mail.
0
 

Author Comment

by:swpui
Comment Utility
How to know whether the mail has failed to send to that specific address after the customer had press the submit button? I need an alert!!
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're not going to get an alert after the initial function request.  It is simply not available.  It's not available on regular email for that matter although if you are sending from an email address that can receive email NDRs (Non-Delivery Reports), you might get those.
0
 

Author Closing Comment

by:swpui
Comment Utility
this is the only way
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now