Solved

Logs for PHP form

Posted on 2013-06-18
8
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39256065
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 83

Expert Comment

by:Dave Baldwin
ID: 39257218
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
ID: 39280141
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 39280205
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
 

Author Comment

by:swpui
ID: 39283674
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
ID: 39283824
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 83

Expert Comment

by:Dave Baldwin
ID: 39284799
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
ID: 39587108
this is the only way
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 80
How do I add date to text file name 15 28
How can I use javascript variable in mysql query 21 40
How do I init a PHP page jusing JQuery? 8 29
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

732 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