This question already has an answer here:
Setup Apache, PHP on DebianMM tuts contact formmsmtp instructions
I'm set out to learn how to submit a form on my localhost. I can send the mail with msmtp in terminal, and I've sent a test email with php terminal command and received it.
The code listed below, after validating the forms, sends a success message as if a email were sent but it doesn't go anywhere, I never receive an email.
There were some additional references I didn't include to setting up gmail to work, with an app password for this sort of thing and I believe one change in the php.ini file under sendmail function as well as permissions.
It took a while to setup apache, mkdir and enable with a symlink, and I think regardless if I cant get this script to work I think it was a good investment of time. but it took a lot of time and I still need some sort of way to submit forms.
Other avenues that I may be able to make work would be flask or jquery/ajax. Any recommendations? also i get the feeling that storing user information in a gmail account is sort of tacky where does information usually get posted to and can I mock up something similar on a localhost
it would be very nice to get this php form to work and then start with a new modern approach.
so once again, form says it posts data but I never receive the memo(email)
thanks!
<?php
if ( mail ( 'msippel415@gmail.com', 'Test mail from localhost', 'Working Fine.' ) ){
echo 'Mail sent ';
}
else{
echo 'Error. Please check error log.';
}
?>
So here is my attempt to follow mmtuts tutorial. I took a lot of time to look over his code and I have a better understanding than if I would have merely copied and pasted however I dont have a lot of experience with php or programming in general. When the localhost would allow me to view the forms I would comment out the php_process form which I included and troubleshoot from there.
I followed up with a subsequent video of his, a more basic form and included after I was completely exhausted and I got a slightly better understanding of the basics of php but there were lots of holes in those files as well.
cat form.php
<?php include('form_process.php'); ?>
<link rel="stylesheet" href="form.css" type="text/css">
<div class="container">
<form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<h3>Quick Contact</h3>
<h4>Contact us today, and get reply with in 24 hours!</h4>
<fieldset>
<input placeholder="Your name" type="text" tabindex="1" name="name" value="<?= $name ?>" autofocus>
<span class="error"><?= $name_error ?></span>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2">
<span class="error"><?= $email_error ?></span>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number" type="text" name="phone" value="<?= $phone ?>" tabindex="3">
<span class="error"><?= $phone_error ?></span>
</fieldset>
<fieldset>
<input placeholder="Your Web Site starts with http://" type="text" name="url" value="<?= $url ?>"tabindex="4">
<span class="error"><?= $url_error ?></span>
</fieldset>
<fieldset>
<textarea placeholder="Type your Message Here...." type="text" name="message" value="<?= $message ?>" tabindex="5"></textarea>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
<div class ="success"><?= $success; ?></div>
</form>
the included form_process.php
<?php
// define variables and set to empty values
$name_error = $email_error = $phone_error = $url_error = "";
$name = $email = $phone = $message = $url = $success = "";
//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name"]);
//check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required";
} else {
$email = test_input($_POST["email"]);
//check if e-mail only contains letters and whitespace
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
}
}
if (empty($_POST["phone"])) {
$phone_error = "Phone number is required";
} else {
$phone = test_input($_POST["phone"]);
//check is email is well-formed
if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
$phone_error = "Invalid phone number";
}
}
if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone))
if (empty($_POST["url"])) {
$url_error = "";
} else {
$url = test_input($_POST["url"]);
// check if URL address syntax is valid (this regular expression sllows dashes in the URL)
if (preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$url)){
$url_error = "Invalid URL";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
$to = 'msippel415@gmail.com';
$subject = 'Contact Form Submit';
if ( mail ($to, $subject, $message)){
$success = "Message sent, thank you for contacting us!";
$name = $email = $phone = $message = $url = '';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
thanks, Im looking forward to solving this issue so I can get back to learning other things.