www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP Email Input

  1. #1
    Join Date
    Jul 2014
    Posts
    3

    PHP Email Input

    I'm trying to get my form submission to submit, but when I do, I get an error from my server side: Could not send mail! Please check your PHP mail configuration.

    The error lies in this area, but I'm not entirely sure how to go about fixing it.

    PHP Code:
    $email_content "Name: $user_Name\n";
    $email_content .= "Email: $user_Email\n\n";
    $email_content .= "Project Budget:\n$user_Project\n";
    $email_content .= "Project ETA:\n$$user_ProjectETA\n";
    $email_content .= "Project Details:\n$$message\n"

    full code:

    PHP Code:
    <?php
    if($_POST)
    {
        
    $to_Email       ""//Replace with recipient email address
        
    $subject        'Project Submissions'//Subject line for emails
        
        
        //check if its an ajax request, exit if not
        
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        
            
    //exit script outputting json data
            
    $output json_encode(
            array(
                
    'type'=>'error'
                
    'text' => 'Request must come from Ajax'
            
    ));
            
            die(
    $output);
        } 
        
        
    //check $_POST vars are set, exit if any missing
        
    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userProject"]) || !isset($_POST["userProjectETA"]) || !isset($_POST["userMessage"]))
        {
            
    $output json_encode(array('type'=>'error''text' => 'Some fields may have been left blank, please go back and correct them!'));
            die(
    $output);
        }

        
    //Sanitize input data using PHP filter_var().
        
    $user_Name        filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
        
    $user_Email       filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
        
    $user_Project       filter_var($_POST["userProject"], FILTER_SANITIZE_NUMBER_INT);
        
    $user_ProjectETA      filter_var($_POST["userProjectETA"], FILTER_SANITIZE_NUMBER_INT);
        
    $user_Message     filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
        
        
    //additional php validation
        
    if(strlen($user_Name)<2// If length is less than 4 it will throw an HTTP error.
        
    {
            
    $output json_encode(array('type'=>'error''text' => 'Your name is too short, please add more characters!'));
            die(
    $output);
        }
        if(!
    filter_var($user_EmailFILTER_VALIDATE_EMAIL)) //email validation
        
    {
            
    $output json_encode(array('type'=>'error''text' => 'Please enter a valid email address!'));
            die(
    $output);
        }
        
        if(
    strlen($user_Project)<2//check emtpy message
        
    {
            
    $output json_encode(array('type'=>'error''text' => 'The project budget msut contain numbers only!'));
            die(
    $output);
        }    
        if(
    strlen($user_ProjectETA)<5//check emtpy message
        
    {
            
    $output json_encode(array('type'=>'error''text' => 'Please format the project ETA like 01/01/2000!'));
            die(
    $output);
        }    
        if(
    strlen($user_Message)<15//check emtpy message
        
    {
            
    $output json_encode(array('type'=>'error''text' => 'Your message is too short, please add more characters to the message!'));
            die(
    $output);
        }
        
        
    //proceed with PHP email.
        
    $headers 'From: '.$user_Email.'' "\r\n" .
        
    'Reply-To: '.$user_Email.'' "\r\n" .
        
    'X-Mailer: PHP/' phpversion();
        
        
    $email_content "Name: $user_Name\n";
        
    $email_content .= "Email: $user_Email\n\n";
        
    $email_content .= "Project Budget:\n$user_Project\n";
        
    $email_content .= "Project ETA:\n$$user_ProjectETA\n";
        
    $email_content .= "Project Details:\n$$message\n";
        
        if(!
    $sentMail)
        {
            
    $output json_encode(array('type'=>'error''text' => 'Could not send mail! Please check your PHP mail configuration.'));
            die(
    $output);
        }else{
            
    $output json_encode(array('type'=>'message''text' => 'Thanks <strong>'.$user_Name .'</strong> for your project submission! '));
            die(
    $output);
        }
    }
    ?>
    I replaced it with this code:
    PHP Code:
     $sentMail = @mail($to_Email$subject$user_Message .'  -'.$user_ProjectETA$headers); 
    and it submitted successfully, however I'm trying to get all fields to send to an email.

    I even tried
    PHP Code:
    $sentMail = @mail($to_Email$subject$user_Project$user_ProjectETA$user_Message .'  -'.$user_Name $headers); 
    sadly it doesn't work.

    any ideas?

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    878
    What does this mean:

    "and it submitted successfully, however I'm trying to get all fields to send to an email."

    If your mail statement executed cleanly, what else do you want to do?

    AND please stop using @ in front of your commands. If there is a problem you DON'T want to suppress the error!!! Ever!

  3. #3
    Join Date
    Jul 2014
    Posts
    3
    Quote Originally Posted by ginerjm View Post
    What does this mean:

    "and it submitted successfully, however I'm trying to get all fields to send to an email."

    If your mail statement executed cleanly, what else do you want to do?

    AND please stop using @ in front of your commands. If there is a problem you DON'T want to suppress the error!!! Ever!
    well i replace the above with this;

    $sentMail = @mail($to_Email, $subject, $user_Message .' -'.$user_ProjectETA, $headers);
    i try adding in like $user_Name etc and it won't submit.. it's like it only submit 2 of those "$user_" fields.

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    878
    1 - Is php error checking turned on?
    2 - WHY DO YOU SUPPRESS ERRORS WITH THE @ SIGN, but then try to capture a return value from the 'mail' call? Take off the @ and see what error message you get out of your statement.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,504
    Shouldn't this
    PHP Code:
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest'
    be

    PHP Code:
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest'
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Jul 2014
    Posts
    3
    no.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    878
    Turn On PHP Error Checking.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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