Jump to content



Recommended Posts

Hello everyone
I would like you to help me with this PHP file please because when I test it online it gives me an error after
$siteOwnersEmail how should I fill in the field?
Thanks for your help

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.";
     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



Link to comment
Share on other sites

It seems like the variable $siteOwnersEmail is used, but its value is not defined in your provided code. You need to set the email address where you want to receive the form submissions. Add the following line at the beginning of your script, before the if($_POST) line:

$siteOwnersEmail = 'your-email@example.com';

Replace 'your-email@example.com' with the actual email address where you want to receive the form submissions.

Additionally, there's a small error in your code. You're using $Name in your message concatenation, but the variable is actually $name (lowercase). Update the line to:

$message .= "Email from: " . $name . "<br />";

After making these changes, your script should work better.

Edited by requinix
  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.