1

i'm using a normal php script for recive mail from my website directly to my personal mail. When i try to test and send from contact form a mail to me, output always said: "Something went wrong, please try again" like if there's no validation. It's my first time that i try to incorporate a php script so probably i'm missing something very oblivious.

Here's the code:

  <?php

$siteOwnersEmail = 'Myemail@gmail.com';


if($_POST) {

   $name = trim(stripslashes($_POST['contactName']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check Name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name.";
    }
    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address.";
    }
    // Check Message
    if (strlen($contact_message) < 15) {
        $error['message'] = "Please enter your message. It should have at least 15 characters.";
    }
   // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }


   // Set Message
   $message .= "Email from: " . $name . "<br />";
    $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

        if ($mail) { echo "OK"; }
      else { echo "Something went wrong. Please try again."; }
        
    } # end if - no validation error

    else {

        $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
        
        echo $response;

    } # end if - there was a validation error

}

?>
Sirol
  • 33
  • 4
  • The webserver could refuse sending mails with from_address domain different from domain running the script. Try to find error log to get details about the error. – Luckyfella Apr 10 '22 at 10:34
  • Make sure that you [show all errors and warnings](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) while developing. I see things in your code that should at least throw notices/warnings, like `$message .= "Email from: " . $name . "
    ";`. You can't append to a non existing variable. You should get "Undefined variable $message" there, which also means that there could be more issues and warnings that could help you locate the problem.
    – M. Eriksson Apr 10 '22 at 10:46
  • 1
    Another example where you should get warning/notice: `if (!$error)`. If no errors were found, that variable will also be undefined and throw undefined variable. – M. Eriksson Apr 10 '22 at 10:49

1 Answers1

1

PHP mail function send email without SMTP authentication. It would be more accurate to send e-mail with SMTP authentication. I can suggest you to check the link below.

https://github.com/PHPMailer/PHPMailer

Cryptograph
  • 46
  • 1
  • 5