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.
[code=php]$email_content = “Name: $user_Namen”;
$email_content .= “Email: $user_Emailnn”;
$email_content .= “Project Budget:n$user_Projectn”;
$email_content .= “Project ETA:n$$user_ProjectETAn”;
$email_content .= “Project Details:n$$messagen”;
full code:
[code=php]<?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_Email, FILTER_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.” . “rn” .
‘Reply-To: ‘.$user_Email.” . “rn” .
‘X-Mailer: PHP/’ . phpversion();
$email_content = “Name: $user_Namen”;
$email_content .= “Email: $user_Emailnn”;
$email_content .= “Project Budget:n$user_Projectn”;
$email_content .= “Project ETA:n$$user_ProjectETAn”;
$email_content .= “Project Details:n$$messagen”;
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:
[code=php] $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
[code=php]$sentMail = @mail($to_Email, $subject, $user_Project, $user_ProjectETA, $user_Message .’ -‘.$user_Name $headers);
sadly it doesn’t work.
any ideas?