For some reason my forms have stopped working and I cannot get themt o send any emails. I have checked the server logs and I can see on form submition it throws the following error.

PHP Notice: Undefined variable: email_message on line 21,

    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
$name = $_POST['name'];
$department = $_POST['department'];
$question = $_POST['question'];

$email_to = '';
$email_from = '';

//possible html email??

$email_subject = "Principal Question\n\n";
  function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  $email_message .= "<b>Name:</b> ".clean_string($name)."\n<br>";
  $email_message .= "<b>Department:</b> ".clean_string($department)."\n<br>";
  $email_message .= "<b>Question:</b> ".clean_string($question)."\n<br>";
  // create email headers

$headers = "Content-type: text/html\r\n";
$headers .= 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
  @mail($email_to, $email_subject, $email_message, $headers);
  @mail($email_from, $email_subject, $email_message, $headers);

  if( !empty( $_POST ) ) {
    header( "Location: thankyou.html" ) ; exit ;

Can anyone help?


To resolve the notice change:

$email_message .= 


$email_message = 

only on line 21.

I doubt this will solve your issue though.

I think he is supposed to use .= because he is adding to the already existing value of $email_message

If i had this problem, id check the form code first. Can we see that?

Member Avatar

I think he is supposed to use .= because he is adding to the already existing value of $email_message

There is no evidence of this. It may be, but let's wait for the OP to reply. Second guessing is fruitless.

@tobyITguy, I agree with pritaeas. There was no string represent $email_message so the first one must not be concatenated.

Sorry for the delay, please see the form code below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HR Question</title>

<div align="center">
  <p align="center">This is a tool to enable you to ask questions or  submit ideas.</p>
<form name="questionform" method="post" action="send_form_email.php">

<table width="800">


 <td width="329" valign="top">


 <td width="459" valign="top">

  <input  type="text" name="name" maxlength="50" size="50"> 




 <td valign="top"">


 <td valign="top"><select name="department" id="department">
   <option value="Anon" selected="selected">Anon</option>
    <option value="Business Support">Business Support</option>
    <option value="Care">Care</option>
    <option value="Catering">Catering</option>
    <option value="Domestic">Domestic</option>
    <option value="Education">Education</option>
    <option value="Estates">Estates</option>
    <option value="Families">Families</option>
    <option value="Medical">Medical</option>
    <option value="Property Services">Property Services</option>
    <option value="Psychology">Psychology</option>




  <td valign="top">

    Question / Idea:</td>

  <td valign="top">

    <textarea  name="question" maxlength="99999" cols="50" rows="10"></textarea>


  <td colspan="2" style="text-align:center">

    <input type="submit" value="Submit">   <a href="email_form.php"></a>



<div align="center">

I have tried removing the . from line 21 and no joy.

Does it produce the same error

Remove the @ before mail and see if you get an error. The function returns false if it fails, so check that too.