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,

<?php
if(!isset($_POST['submit']))
{
    //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 = 'me@me.com';
$email_from = 'me@me.com';

//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);

?>
<?php
  if( !empty( $_POST ) ) {
    header( "Location: thankyou.html" ) ; exit ;
  }
?>

Can anyone help?

Thanks

To resolve the notice change:

$email_message .= 

to:

$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 for diafol

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HR Question</title>
</head>

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

<table width="800">

<tr>

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

  Name:</td>

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

  <input  type="text" name="name" maxlength="50" size="50"> 
  (optional)

 </td>

</tr>

<tr>

 <td valign="top"">

  Department:</td>

 <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>
 </select> 
 (optional)

 </td>

</tr>

<tr>

  <td valign="top">

    Question / Idea:</td>

  <td valign="top">

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

    </td>  
</tr>
<tr>

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

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

    </td>

</tr>

</table>
</form>
<div align="center">
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</div>
</body>
</html>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.