0

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

<div class="border"></div>
             <form class="contact-form" action="mail.php" method="post">

              <div class="fisrtthree">
              
              <input type="text" name="fname" class="contact-form-text" placeholder="First name" required>
              </div>

              <div class="fisrtthree">
              
              <input type="text" name="lname" class="contact-form-text" placeholder="Last name" required>
              </div>

              <div class="fisrtthree">
              
              <input type="email" name="email" class="contact-form-text" placeholder="Email" required>
              </div>

              <div>
              
              <textarea class="contact-form-text" name="message" rows="6" maxlength="3000" placeholder="Your message" required></textarea>
              </div>

              <div>
              <button type="submit" id="fcf-button" class="contact-form-btn">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 = "#@gmail.com";       //  Put your email address here
$subject = "Message from your website!";   //  Fill in the subject line you want your messages to have
$fromAddress = "#@domain.com";   //  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 #@domain.com and received it in mygmail@gmail.com. but it didn't work, I received nothing in my inbox. is there anything to do with my gmail and my hosted email.

  • Likely unrelated to actual issue but why `iso-8859-1` instead of utf8. You should use phpmailer or swift mailer. PHP's native mail function is poor, there is no error reporting with it. Also note `Returns true if the mail was successfully accepted for delivery, false otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.` so see what `mail()` returns but generally is useless. – user3783243 May 09 '22 at 18:23
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community May 09 '22 at 19:41
  • Do you get any errors in your error log? You dont need to set header['subject'], its a param of mail. – Grumpy May 09 '22 at 19:44

2 Answers2

0
  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());
Vishnu Raj
  • 136
  • 9
0

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 = "fihriabdelali@gmail.com";
$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 "youremail@domain.com"