This question already has an answer here:
I am trying to send the content in this form to an email. However, it isn't working and when I submit it gives me an internal error. Here is the code that runs when I am submitting the form.
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['first_name']; // required
$job = $_POST['job']; // required
$company = $_POST['company']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$psn = $_POST['psn']; // required
$un = $_POST['un']; // required
$weight = $_POST['weight']; // not required
$pieces = $_POST['pieces']; // required
$dgpacked = $_POST['dgpacked']; // required
$address = $_POST['address']; // required
$readytime = $_POST['readytime']; // required
$deliverytype = $_POST['deliverytype']; // required
if(IsInjected($email))
{
echo "Bad email value!";
exit;
}
$email_from = 'c.oswald@live.com';//<== update the email address
$email_subject = "New Quote Request submission";
$email_body = "You have received a new message from the $name with $company.
".
"Here is the message:
$message".
$to = "c.oswald@live.com";//<== update the email address
$headers = "From: $email_from
";
$headers .= "Reply-To: $email
";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: home.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;
}
}
?>
If there is anything more simple, please help me out, I just need something simple enough to understand but not so basic I will have security or injection problems.
</div>
For email verification, you can easily use FILTER_VALIDATE_EMAIL. I think that is better than using your own function.
I have not found any other mistake in your code, so I think that it is your function. Otherwise it could lead to a server misconfiguration, then I need the error log entry for investigation. If you use MAMP on your Mac, you can find the error log at /Applications/MAMP/logs/php_error.log.
For mailing purposes, I can recommend PHPMailer to you.
(In the first statement
if(!isset($_POST['submit']))
an exit; is missing. After the header-redirect, also an exit; should take place. Furthermore, you should check that all necessary parameters are passed by the client.)
This will happen due to the following reasons:
Unhanded error in Code, Syntax errors and Explicit Codes.
For Sending email the best php class is : https://github.com/PHPMailer/PHPMailer
It is the latest PHPMailer Class.
After downloading this file, you can see mail.php file. There you have to change the username, password, recipients and message etc.