www.webdeveloper.com
Results 1 to 4 of 4

Thread: mail() function, crazy results, problems

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    mail() function, crazy results, problems

    I recently built a contact form for a client and am putting the data into a table and then sending the data to 3-5 recepients using the mail function.

    the data is being put into a table no problem whatsoever. but with the mail() function i am getting crazy, mixed results.

    ex: I get the mail right away at all 3 of my mail accounts (on diff hosts). my client is not getting them at all, and a test subject, received an email at today at 2:20 a.m. that was sent on Friday afternoon!

    i don't know what to make of this?

    are there any know issues with this function?, or any alternatives to using it.
    the client is on a commercial shared hosting provider. i'll include the code just in case it's on my end.

    PHP Code:
    <?php
    if (isset($_POST['submit'])) {
        echo 
    "<p>submitted</p>"

    $name $_POST['name'];
    $email $_POST['email'];
    $subject $_POST['subject'];
    $message $_POST['message'];
    $date date("m/d/y");

    $mail_to_r1 'recipient1@yahoo.com';
    $mail_to_r2 'recipient2@yahoo.com';
    $mail_to_r3 'recipient3@yahoo.com';
    $esubject 'New Mail from Contact Form';
    $emessage  'From: ' $name "\n";
    $emessage .= 'Email: ' $email "\n";
    $emessage .= 'Subject: ' $subject "\n";
    $emessage .= "Message:\n" $message "\n\n";

    //    $name = $_POST['name'];
        
        
    if ($name != "" && $name != "Name:") {
                
    $name filter_var($_POST['name'], FILTER_SANITIZE_STRING);
                if (
    $name == "") {
                    
    $errors .= 'Please enter a valid name.<br/><br/>';
                    echo 
    $errors;
                }
            } else {
                
    $errors .= 'Please enter your name.<br/>';
                 echo 
    $errors;
            }
            
        if (
    $email != "" && $email != 'Email:') {
            
    $email filter_var($emailFILTER_SANITIZE_EMAIL);
            if (!
    filter_var($emailFILTER_VALIDATE_EMAIL)) {
                    
    $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
                }
            } else {
                
    $errors .= 'Please enter your email address.<br/>';
            }
                
        if (
    $subject != "" && $subject != 'Subject:') {
                
    $subject filter_var($subjectFILTER_SANITIZE_STRING);
                if (
    $subject == "") {
                    
    $errors .= 'Please enter a Subject.<br/>';
                }
            } else {
                
    $errors .= 'Please enter a Subject.<br/>';
            }    
            
        if (
    $message != "" && $message != 'Message:') {
                
    $message filter_var($messageFILTER_SANITIZE_STRING);
                if (
    $message == "") {
                    
    $errors .= 'Please enter a Message.<br/>';
                }
            } else {
                
    $errors .= 'Please enter a Message.<br/>';
            }        

    //echo "This is the $name";
        
    $connection mysql_connect("localhost""root""password");

        if(!
    $connection) {
            die(
    "Database connection failed: " mysql_error());
        } 
    //else {echo "<p>connection successful</p>";}
        
        //2. Select a database to use
        
    $db_select mysql_select_db("database"$connection);

        if(!
    $db_select) {
            die(
    "Database selection failed: " mysql_error());
        }  
    //else {echo "<p>connection successful</p>";}

    $query "INSERT INTO contact_form (name, email, subject, message, date) VALUES ('$name', '$email', '$subject', '$message', '$date')";

        if(!
    $errors) {
            
    mail($mail_to_r1$esubject$emessage);
            
    mail($mail_to_r2$esubject$emessage);
            
    mail($mail_to_r3$esubject$emessage);
            
    header("Location: http://www.whatever.com/sent");
        } else {
            
    //echo "<p>There were errors</p>";
            //echo $errors;
            
    header("Location: http://www.whatever.domain.com/email-error.php");
        }
    }  
    // END IF SUBMIT
    ?>

  2. #2
    Join Date
    Jun 2010
    Posts
    16
    If you're on a unix, linux, or an osx system try sending the email using the "mail" command line program. Type "man mail" for command options. That would isolate whether it is your particular application thats is having problems, or if it is your client's server that is having problems with the email or the domain from which you are sending it from.
    Last edited by dukesdemise; 05-07-2012 at 07:36 PM.
    SCJP 2, SCWCD 5, ZCE 5.3

  3. #3
    Join Date
    Mar 2011
    Posts
    1,137
    I see where the script sends three Emails, which sounds like they're your 3 Email accounts, but I don't see anywhere it sends the message to your client. Did you try changing one of the Email addresses to your client's address?

    It's (barely) possible that three calls to mail() in a single script could cause the script to exceed the server's time limit. It would be better to include a 'bcc:' field in the header to send the message to multiple addresses with a single call to mail().

    It's not that unusual for Email generated by a script to be slow to arrive. Mail servers often classify such messages as SPAM or borderline SPAM and hold them in their queue for later processing. Similarly, hosting services often queue outgoing Email if they get busy or a single account is sending a lot of messages.

    I expect that with a few minor changes and some patience, you'll get this all to work for you.
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Mar 2010
    Posts
    281
    thanks, i will try both

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles