Jump to content

basic form_mail not working on new server


nnns

Recommended Posts

Hello!

 

I don't have a clue about php really...

 

I use a simple form_mail script which has always worked fine on various web sites but it doesn't work on a new site I'm working on.  I'm getting 'undefined index' errors in every text field.  I'm wondering if it needs tweaking to work with php5?

 

Here's the script in its entirety... pretty basic.

 

<?php 
// modify the two lines below 
$emailAddress = "fred@site.co.uk";  // your email address 
$thankyouPage = "http://www.site.co.uk";  // put in a url (or relative page) to go to a Thank You page 
// modify the two lines above 
session_start(); 
if (!empty($_POST)) { 
  foreach ($_POST as $key=>$value) { 
      $_POST[$key] = stripslashes($_POST[$key]); 
    $_POST[$key] = htmlspecialchars($_POST[$key],ENT_QUOTES); 
  } 
} 
if (isset($_POST['send']) AND isset($_SESSION['msgCount'])) { 
  if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i",$_POST['email'])) $alert = "You have entered an invalid email address."; 
  if ($_SESSION['msgCount'] >= "3") $alert = "Only 3 messages can be sent per session."; 
  if (empty($alert)) { 
    $_SESSION['msgCount']++; 
    putenv('TZ=EST5EDT'); // eastern time 
    $headers  = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $headers .= "From: <".$_POST['email'].">\r\n"; 
    $message = "<table cellpadding='5' border='1'>"; 
    foreach ($_POST as $key => $value) 
      if (!preg_match("(^send)",$key)) { 
        $value = wordwrap($value,65,"<br />"); 
        $message .="<tr><td><b>$key</b></td><td>$value</td></tr>"; 
      } 
    $message .= "</table>";     
    $message .= "<br />Time of the message: ".date(" F d  h:ia")."<br />"; 
    $message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."<br />"; 
    $message .= "Hostname: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."<br />"; 
    $subject = $_SERVER['HTTP_HOST']." Message"; 
    mail($emailAddress,$subject,$message,$headers); 
    if (!empty($thankyouPage)) { 
      header('location: '.$thankyouPage); 
      die(); 
    } 
    unset($_POST); 
    $alert = "Your message has been sent."; 
  } 
} 
if (!isset($_SESSION['msgCount'])) $_SESSION['msgCount'] = 0; 
?> 
<html> 
<head> 
</head> 
<body> 
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"> 
<div style="text-align: center; width: 500px; margin: 50px auto 0px auto; border: solid 1px black;"> 
<br> 
Send us a message<br><br> 
your name<br><input type="text" style="width: 330px;" name="name" value="<?php echo $_POST['name']; ?>" maxlength="50"><br><br> 
your email address<br><input type="text" style="width: 330px;" name="email" value="<?php echo $_POST['email']; ?>" maxlength="50"><br><br> 
your message<br><textarea name="content" style="width: 330px; height: 100px;" rows="6" cols="80"><?php echo $_POST['content']; ?></textarea> 
<br><br> 
<input type="submit" name="send" value="submit"> 
<br /><br />  
</div> 
</form> 
<?php if (isset($alert)) echo "<script type='text/javascript'>alert('$alert');</script>";  ?> 
</body></html> 

Link to comment
Share on other sites

Well, I've been given some advice on a different forum which has removed all the error messages in the text boxes (all I had to do was add some @ symbols before some of the variables).  So now it looks like it works... except the email doesn't come through!  I've checked and send_mail is allowed on the server.

 

Here's the new modified code (and by the way I have ensured it goes to my email address - I just changed it here for obvious reasons):

<?php
// modify the two lines below
$emailAddress = "fred@website.co.uk";  // your email address
$thankyouPage = "http://www.website.co.uk";  // put in a url (or relative page) to go to a Thank You page
// modify the two lines above
session_start();
if (!empty($_POST)) {
  foreach ($_POST as $key=>$value) {
  	$_POST[$key] = stripslashes($_POST[$key]);
    $_POST[$key] = htmlspecialchars($_POST[$key],ENT_QUOTES);
  }
}
if (isset($_POST['send']) AND isset($_SESSION['msgCount'])) {
  if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i",$_POST['email'])) $alert = "You have entered an invalid email address.";
  if ($_SESSION['msgCount'] >= "3") $alert = "Only 3 messages can be sent per session.";
  if (empty($alert)) {
    $_SESSION['msgCount']++;
    putenv('TZ=EST5EDT'); // eastern time
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: <".$_POST['email'].">\r\n";
    $message = "<table cellpadding='5' border='1'>";
    foreach ($_POST as $key => $value)
      if (!preg_match("(^send)",$key)) {
        $value = wordwrap($value,65,"<br />");
        $message .="<tr><td><b>$key</b></td><td>$value</td></tr>";
      }
    $message .= "</table>";    
    $message .= "<br />Time of the message: ".date(" F d  h:ia")."<br />";
    $message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."<br />";
    $message .= "Hostname: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."<br />";
    $subject = $_SERVER['HTTP_HOST']." Message";
    mail($emailAddress,$subject,$message,$headers);
    if (!empty($thankyouPage)) {
      header('location: '.$thankyouPage);
      die();
    }
    unset($_POST);
    $alert = "Your message has been sent.";
  }
}
if (!isset($_SESSION['msgCount'])) $_SESSION['msgCount'] = 0;
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<div style="text-align: center; width: 500px; margin: 50px auto 0px auto; border: solid 1px black;">
<br>
Send us a message<br><br>
your name<br><input type="text" style="width: 330px;" name="name" value="<?php echo @$_POST['name']; ?>" maxlength="50"><br><br>
your email address<br><input type="text" style="width: 330px;" name="email" value="<?php echo @$_POST['email']; ?>" maxlength="50"><br><br>
your message<br><textarea name="content" style="width: 330px; height: 100px;" rows="6" cols="80"><?php echo @$_POST['content']; ?></textarea>
<br><br>
<input type="submit" name="send" value="submit">
<br /><br /> 
</div>
</form> 
<?php  if (isset($alert)) echo "<script type='text/javascript'>alert('$alert');</script>";  ?>
</body></html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.