0

I'm receiving the this message in the error logs from my contact form and need help to fix this so it works. Currently it's not sending anything.

PHP Notice: Undefined variable: message in .../send-email.php on line 25

Here's my php file (replaced email with generic to display here, is currently set to mine):

<?php

// Replace this with your own email address
$to = 'contact@mysite.co';

function url(){
  return sprintf(
    "%s://%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_NAME']
  );
}

if($_POST) {

   $name = trim(stripslashes($_POST['name']));
   $email = trim(stripslashes($_POST['email']));
   $subject = trim(stripslashes($_POST['subject']));
   $contact_message = trim(stripslashes($_POST['message']));

   
    if ($subject == '') { $subject = "Contact Form Submission"; }

   // Set Message
   $message .= "Email from: " . $name . "<br />";
    $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= nl2br($contact_message);
   $message .= "<br /> ----- <br /> This email was sent from your site " . url() . " 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";

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

    if ($mail) { echo "OK"; }
   else { echo "Something went wrong. Please try again."; }

}

?>

Tried googling for an answer but can't find anything I'm able to utilize/understand.

  • Where is line 25? What have you tired to resolve the problem? Where are you stuck? – Nico Haase Jun 22 '23 at 20:31
  • @NicoHaase line 25 is: `$message .= "Email from: " . $name . "
    ";` I am not good with php and got this form as a template to use, but there were no other details than 'replace with your email' at the top. So I'm not sure what the issue is or what to change it to.
    – Tiffani P Jun 22 '23 at 20:33
  • 2
    probably this line ``$message .= "Email from:`` here you're trying to append an undefined variable ``$message`` to $message !! – OMi Shah Jun 22 '23 at 20:36
  • Welcome to Stack Overflow. This is a notice message, it shouldn't block the workflow. You have to declare the variable (with `=`) before appending a string to it (with `.=`). See: warning: https://3v4l.org/3DkI6 and no warning: https://3v4l.org/Hek0K – A.L Jun 22 '23 at 20:37
  • This was closed as a duplicate but those other posts don't help me. – Tiffani P Jun 22 '23 at 20:42
  • 2
    `$message .=` is incorrect for initializing. You need to use `=` to initialize, then `.=` can be used. Unclear how the duplicates don't help you. Please clarify where you are stuck. – user3783243 Jun 22 '23 at 20:45

0 Answers0