This question already has an answer here:
So I have a simple php file that gets data from a form and is supposed to email me. It works on my own system when I was testing it on localhost. But, when I deployed it on ubuntu using apache2, it didn't work. the file may not be pretty, my first attempt to email with php, but i've included the php file below. I know it gets to the mail() method and fails, it activates the (!$mail) conditional, but I can't ever print $mail or any errors so I have no clue what is wrong. any ideas? The cluster of echoes was my attempt to print some kind of error message with no luck. Also, I actually send it to my email address, I just changed it for this example
<?php
if(!isset($_POST['submit'])){
echo "error; you need to submit the form!";
}
$visitor_name = $_POST['name'];
$visitor_message = $_POST['message'];
//incase the email isn't provided
if(empty($_POST['email'])){
$visitor_email = 'n/a';
} else {
$visitor_email = $_POST['email'];
}
//incase the phone isn't provided
if(empty($_POST['phone'])){
$visitor_phone = 'n/a';
} else {
$visitor_email = $_POST['email'];
}
//incase the phone isn't provided
if(empty($_POST['phone'])){
$visitor_phone = 'n/a';
} else {
$visitor_phone = $_POST['phone'];
}
if(empty($visitor_name) || empty($visitor_message))
{
echo "Name and message are mandatory!";
exit;
}
//a function created below for security purposes
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
// **************************** CODE FOR EMAIL BODY BELOW *****************************************
$email_body = '<html><body>';
$email_body .= "<h2> You've recieved a new message from: $visitor_name, they need a building </h2>";
$email_body .= '<h4> Here is the message: </h4>';
$email_body .= "<p> $visitor_message </p>";
$email_body .= "<h4> Their contact info is below</h4>";
$email_body .= "<ul> <li> email: $visitor_email </li>";
$email_body .= "<li> phone: $visitor_phone </li></ul>";
$email_body .= '</body></html>';
// **************************** END OF CODE FOR EMAIL BODY ****************************************
$to = 'j@example.com';
$subject = "Building Form Submission: $visitor_name Needs a building
";
$headers = "From: building-form@ArchitectureAdvertisingWebsite.com
";
$headers .= 'MIME-Version: 1.0' . "
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
if($visitor_email != 'n/a'){
$headers .= "Reply-To: $visitor_email
";
}
$mail = mail($to, $subject, $email_body, $headers);
print_r ($mail);
echo "end test";
if (!$mail){
echo "Message not sent, there was an error. Please contact Jerrod at .....";
$errorMessage = error_get_last();
echo "There was an error: $errorMessage";
echo "Below the error is printed : ";
print_r(error_get_last());
} else {
echo "Message sent";
header('Location: end.html');
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(
+)',
'(+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
</div>