I want to add a function to the end of my included file (code below) that will email the POST data that it validates. I have tried different ways to do this but to no avail? Everything is validating no problem but that is all! I basically have a form that validates and does nothing apart from that!
I just need a function to be added that will email the POST data:
<?php
// define variables and set to empty values
$addDateErr = $nameErr = $emailErr = $subjectErr = $messageErr = $questionErr = "";
$addDate = $name = $email = $subject = $message = $WebSearch = $SocialMedia = $WordOfMouth = $Other ="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//Date
if (empty($_POST["addDate"]))
{$comment = "";}
else
{$comment = test_input($_POST["addDate"]);}
//Name
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and white space allowed";
}
}
//Email
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
}
//Subject
if (empty($_POST["subject"]))
{$comment = "";}
else
{$comment = test_input($_POST["subject"]);}
//Message
if (empty($_POST["message"]))
{$messageErr = "A message is required";}
else
{$comment = test_input($_POST["message"]);}
//Question
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
} else {
$menuVar = "----------";}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
You probably will want some sort of library that does the job of sending the e-mail for you.
Here are some:
You can use this to send emails with php:
mail("youremail@uptoyou.com", "Subject", "Message", "From:sender@test.com");
In your case you could do something like:
mail("receiver@test.com", $_POST["subject"], "Message:" . $_POST["message"] . "Question:" . $_POST['Question'], "From:" . $_POST["email"]);
You need to assign all your validated form inputs to different variables; for some reason, you reused $comment
for several of them. Once you have them in variables, you can do something like this:
$mail_body = <<<EOM
Name: $name
Email: $email
Subject: $subject
Date: $date
Question: $question
Message: $message
EOM ;
mail("youraddress@yourdomain.com", "Form submission", $mail_body, "From: someaddress@yourdomain.com");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.