Home > database >  PHP contact form configuration
PHP contact form configuration

Time:05-10

Hi guys I run my website and it all work except contact us form. HTML

<div ></div>
             <form  action="mail.php" method="post">

              <div >
              
              <input type="text" name="fname"  placeholder="First name" required>
              </div>

              <div >
              
              <input type="text" name="lname"  placeholder="Last name" required>
              </div>

              <div >
              
              <input type="email" name="email"  placeholder="Email" required>
              </div>

              <div>
              
              <textarea  name="message" rows="6" maxlength="3000" placeholder="Your message" required></textarea>
              </div>

              <div>
              <button type="submit" id="fcf-button" >Send</button>
              <div>

             </form>

PHP

<?php
 if(isset($_POST['message']) == false) {     //  If there's no message
    echo "Uh oh. Looks like you didn't actually include a message, friend.<br><br>";
    die();   
}


$destination = "[email protected]";       //  Put your email address here
$subject = "Message from your alfidomain.com!";   //  Fill in the subject line you want your messages to have
$fromAddress = "[email protected]";   //  Fill in the email address that you want the messages to appear to be from
                                                                //  Use a real address to reduce the odds of getting spam-filtered.
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = str_replace("\n.", "\n..", $_POST['message']);   //  Prevents a new line starting with a period from being omitted

$message = "First Name: ". $fname ."\n Last Name: ". $lname ."\n Email: ". $email ."\n Message: ".$message."\n";

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();

mail($destination, $subject, $message, implode("\r\n", $headers));

Thanks for your message:
echo $message; ?>
Go back home ?>

As you see I tried to get message from [email protected] and received it in [email protected]. but it didn't work, I received nothing in my inbox. is there anything to do with my gmail and my hosted email.

CodePudding user response:

  1. Your email is not being sent. You can try to capture the error that happened via php's last error function, after sending the email : print_r(error_get_last());

CodePudding user response:

Firstly, ensure that your email service provider makes allowance for the use of less secure apps, and make sure you have this feature enabled (Be sure to disable it later). My 'from' email was a Gmail account where non-secure apps can be enabled at this address: https://myaccount.google.com/lesssecureapps?pli=1

Secondly, ensure that your local mail server is correctly set up. If you are using XAMPP follow the directions here: https://www.geeksforgeeks.org/how-to-configure-xampp-to-send-mail-from-localhost-using-php/

Next, name the button on your form in order to detect the form submission, I named the button 'submit'. Then in mail.php use this code

<?PHP
if(isset($_POST['submit']) && empty($_POST['message'])) {     
//  If there's no message
echo "Uh oh. Looks like you didn't actually include a 
message, friend.<br><br>";
die();   
}

$destination = "[email protected]";
$subject = "Message from your alfidomain.com!";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$fromAddress=$email;
$message = str_replace("\n.", "\n..", $_POST['message']);   
//  Prevents a new line starting with a period from being omitted

$message = "First Name: ". $fname ."\n Last Name: ".$lname ."\n Email: ". $email ."\n Message: ".$message."\n";

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $fromAddress;
$headers[] = "Subject: " . $subject;
$headers[] = "X-Mailer: PHP/".phpversion();

mail($destination, $subject, $message, implode("\r\n", 
$headers));

// mail($to,$subject,$msg,$headers);
echo "Email successfully sent.";
?>

If you are not Abdelali make sure you set $destination to "[email protected]"

  • Related