[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Logs for PHP form

Posted on 2013-06-18
8
Medium Priority
?
160 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 111

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 84

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 500 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 84

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 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