Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Php form sending copy to form email address but not to us?

Featured Replies

Hi

 

 

We have finally got a PHP Mail() form working after changing hosts, well I say working.

 

I've filled out the form to test it with my own email and on submission it successfully sends me a copy, but doesn't sent the business the original email submission. I've included the relevant code below can anyone tell what I'm missing please?

 

$myFile = "log.txt";
 $log = 'Availability Check ============================'."\r\n".'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Phone: '.$phone."\r\n".'Number of Cats: '.$cats."\r\n".'Arrival Date: '.$Adate."\r\n".'Collection Date: '.$Ddate."\r\n".'Days: '.$fullDays."\r\n".'Message:'."\r\n".$message."\r\n".'Heard About Us From: '.$hearabout."\r\n".'';
 $log = str_replace('&','&',$log);
 $fh = fopen($myFile, 'a') or die("can't open file");
 $stringData = "$log\n";
 fwrite($fh, $stringData);
 fclose($fh);
 $to	  = 'cats@cats.co.uk';
 //$to	  = 'john@petadmin.com';
 $subject = 'Enquiry from: '.$name;
 $message = 'Availability Check ============================'."\r\n".'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Phone: '.$phone."\r\n".'Number of Cats: '.$cats."\r\n".'Arrival Date: '.$Adate."\r\n".'Collection Date: '.$Ddate."\r\n".'Days: '.$fullDays."\r\n".'Message:'."\r\n".$message."\r\n".'Heard About Us From: '.$hearabout."\r\n".'';
 $message = str_replace('&','&',$message);
 $headers = 'From: donotreply.cats@petadmin.com' . "\r\n" .
  'Reply-To: '.$email.'' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();  mail($to, $subject, $message, $headers);
 $Usermessage = 'Thank you for checking availability with cats; the information below has been sent to us. We will contact you to discuss the details or alternatively, please feel free to call us.'."\r\n".$message;
 $subject = 'Checking availability with cats.co.uk';  if (!empty($email)) {
  mail($email, $subject, $Usermessage, $headers);
 }

--

 

I need the form to be sent to cats@cats.co.uk but it just isn't doing it for some reason?

 

Please any help would be great.

 

Ash

Where is the $email variable being created?

 

You're trying to send the mail to the $email variable but I can't see what that variable contains, if anything. Make sure that variable is set.

 

Lyndsey.

  • Author

This is the hole code for the form.

 

<script type="text/javascript" src="datepickercontrol.js"></script>
<link type="text/css" rel="stylesheet" href="datepickercontrol_bluegray.css">
<?php
$name = '';
$email = '';
$phone = '';
$cats = '';
$message = '';
$hearabout = '';
$error = '';
$Adate = '';
$Ddate = '';
$date_parts1 = '';
$date_parts2 = '';
$start_date = '';
$end_date = '';
$fullDays = '';
$today = date('Y-m-d');
//Has Form Been Submitted?
if (isset($_POST['action']) && ($_POST['submitted']== 'yes')) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $cats = $_POST['cats'];
  $message = $_POST['message'];
  $Adate = $_POST['Adate'];
  $Ddate = $_POST['Ddate'];
 $date_parts1=explode("/", $Adate);
$date_parts2=explode("/", $Ddate);
$start_date=@gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=@gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
$fullDays = $end_date - $start_date + 1;
  //$Aday = $_POST['Aday'];
  //$Amonth = $_POST['Amonth'];
  //$Ayear = $_POST['Ayear'];
  //$Adate = $Ayear.'-'.$Amonth.'-'.$Aday;
  //$readAdate = $Aday.'/'.$Amonth.'/'.$Ayear;
  //$Cday = $_POST['Cday'];
  //$Cmonth = $_POST['Cmonth'];
  //$Cyear = $_POST['Cyear'];
  //$Cdate = $Cyear.'-'.$Cmonth.'-'.$Cday;
  //$readCdate = $Cday.'/'.$Cmonth.'/'.$Cyear;
  $hearabout = $_POST['hearabout'];
  //Error messages
  //Is value entered?
  if (empty($name)) {
 $error .= "<li>Please enter your name</li><br />";
  }
  if (empty($email)) {
 $error .= "<li>Please enter your email address</li><br />";
  }
  if (empty($phone)) {
 $error .= "<li>Please enter your phone number</li><br />";
  }
  if (empty($cats)) {
 $error .= "<li>Please enter the number of cats</li><br />";
  }
  if (empty($message)) {
 $error .= "<li>Please enter your message</li><br />";
  }
  //if ($Adate < $today) {
 //$error .= "<li>Sorry, your arrival date is in the past</li><br />";
  //}
  //if ($Ddate < $today) {
 //$error .= "<li>Sorry, your collection date is in the past</li><br />";
  //}
  //if ($Ddate < $Adate) {
 //$error .= "<li>Sorry, your collection date is before your arrival date</li><br />";
  //}

 $name = htmlspecialchars($name);
 $name = stripslashes($name);
 $email = htmlspecialchars($email);
 $email = stripslashes($email);
 $phone = htmlspecialchars($phone);
 $phone = stripslashes($phone);
 $cats = htmlspecialchars($cats);
 $cats = stripslashes($cats);
 $message = htmlspecialchars($message);
 $message = stripslashes($message);
  //If there are no errors
  if ($error == ''){
 $myFile = "log.txt";
 $log = 'Availability Check ============================'."\r\n".'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Phone: '.$phone."\r\n".'Number of Cats: '.$cats."\r\n".'Arrival Date: '.$Adate."\r\n".'Collection Date: '.$Ddate."\r\n".'Days: '.$fullDays."\r\n".'Message:'."\r\n".$message."\r\n".'Heard About Us From: '.$hearabout."\r\n".'';
 $log = str_replace('&','&',$log);
 $fh = fopen($myFile, 'a') or die("can't open file");
 $stringData = "$log\n";
 fwrite($fh, $stringData);
 fclose($fh);
 $to	  = 'cats@cats.co.uk';
 //$to	  = 'john@petadmin.com';
 $subject = 'Enquiry from: '.$name;
 $message = 'Availability Check ============================'."\r\n".'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Phone: '.$phone."\r\n".'Number of Cats: '.$cats."\r\n".'Arrival Date: '.$Adate."\r\n".'Collection Date: '.$Ddate."\r\n".'Days: '.$fullDays."\r\n".'Message:'."\r\n".$message."\r\n".'Heard About Us From: '.$hearabout."\r\n".'';
 $message = str_replace('&','&',$message);
 $headers = 'From: donotreply.cats@petadmin.com' . "\r\n" .
  'Reply-To: '.$email.'' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();
 mail($to, $subject, $message, $headers);
 $Usermessage = 'Thank you for checking availability with Tannercats; the information below has been sent to us. We will contact you to discuss the details or alternatively, please feel free to call us.'."\r\n".$message;
 $subject = 'Checking availability with cats.co.uk';
 if (!empty($email)) {
  mail($email, $subject, $Usermessage, $headers);
 }
 //Message
 echo '
 <br /><table align="center" width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#66CC00">
  <tr>
   <td align="center" bgcolor="#E7FFCE">
   <span class="success"><br />The submission was a success!<br />We will review your message and get back to you as soon as possible.<br />
   </span></td>
  </tr>
 </table><br />';
 //Delete $_POST
 $name = '';
 $email = '';
 $phone = '';
 $cats = '';
 $message = '';
 $Adate = '';
 $Ddate = '';
 //$Aday = date("d");
 //$Amonth = date("m");
 //$Ayear = date("Y");
 //$Cday = date("d");
 //$Cmonth = date("m");
 //$Cyear = date("Y");
  }
  else {
 //Format error message
 echo '
 <br><table align="center" width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#EC0000">
  <tr>
   <td align="left" bgcolor="#FFFFFF">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td><span class="error">There has been an error:<br />'.$error.'</span></td>
 </tr>
   </table>
   </td>
  </tr>
 </table>';
  }
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="add">
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="5">
  <tr>
 <td width="34%" align="right"><span class="texttitle">Your Name:</span></td>
 <td align="left">
 <input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" maxlength="250" />
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Your Email:</span></td>
 <td width="31%" align="left">
 <input name="email" type="text" id="email" value="<?php echo $email; ?>" size="50" maxlength="250" />
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Phone Number:</span></td>
 <td width="31%" align="left">
 <input name="phone" type="text" id="phone" value="<?php echo $phone; ?>" size="50" maxlength="250" />
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Number of Cats:</span></td>
 <td width="31%" align="left">
 <input name="cats" type="text" id="cats" value="<?php echo $cats; ?>" size="50" maxlength="250" />
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Arrival Date:</span></td>
 <td width="31%" align="left">
    <input type "text" name="Adate" id="DPC_Adate" value="<?php echo $Adate; ?>">
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Departure Date:</span></td>
 <td width="31%" align="left">
    <input type "text" name="Ddate" id="DPC_Ddate" value="<?php echo $Ddate; ?>">
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">Your Message:</span></td>
 <td width="31%" align="left">
    <textarea name="message" cols="40" rows="5"><?php echo $message; ?></textarea>
 </td>
  </tr>
  <tr>
 <td width="34%" align="right"><span class="texttitle">How did you hear about us?:</span></td>
 <td width="31%" align="left">
   <select name="hearabout" id="hearabout">
  <option value="">Please Select...</option>
  <option value="Leaflet on Car" <?php if($hearabout == 'Leaflet on Car') { echo 'selected'; }?>>Leaflet on Car</option>
  <option value="Website" <?php if($hearabout == 'Website') { echo 'selected'; }?>>Website</option>
  <option value="Leaflet in Shop" <?php if($hearabout == 'Leaflet in Shop') { echo 'selected'; }?>>Leaflet in Shop</option>
  <option value="Friend" <?php if($hearabout == 'Friend') { echo 'selected'; }?>>Friend</option>
  <option value="Recommendation" <?php if($hearabout == 'Recommendation') { echo 'selected'; }?>>Recommendation</option>
  <option value="Vehicle Signage" <?php if($hearabout == 'Vehicle Signage') { echo 'selected'; }?>>Vehicle Signage</option>
  <option value="Google Search" <?php if($hearabout == 'Google Search') { echo 'selected'; }?>>Google Search</option>
  <option value="Yell.com" <?php if($hearabout == 'Yell.com') { echo 'selected'; }?>>Yell.com</option>
  <option value="One Chronicle" <?php if($hearabout == 'One Chronicle') { echo 'selected'; }?>>One Chronicle</option>
  <option value="Parish Magazine" <?php if($hearabout == 'Parish Magazine') { echo 'selected'; }?>>Parish Magazine</option>
  <option value="TannerCats Client" <?php if($hearabout == 'TannerCats Client') { echo 'selected'; }?>>TannerCats Client</option>
  <option value="Pedigree Pens Website" <?php if($hearabout == 'Pedigree Pens Website') { echo 'selected'; }?>>Pedigree Pens Website</option>
  <option value="Catteries.org.uk" <?php if($hearabout == 'Catteries.org.uk') { echo 'selected'; }?>>Catteries.org.uk</option>
   </select>
    </td>
  </tr>
   <tr>
 <td valign="top"> </td>
 <td align="left" valign="top">
 <input type="hidden" name="submitted" value="yes">
 <input type="submit" name="action" id="action" value="Submit" class="signin" /></td>
 </tr>
</table>
</form>
<br />

I don't see what's wrong with it. Maybe someone else could take a look?

 

If you're not receiving any errors at all, check the Junk/Spam folder to see if it's gone there instead. Also ensure that there are no issues with the mailbox itself with regards to receiving mail.

 

Lyndsey.

I also looked and was waiting for someone else as I couldn't see anything wrong.

 

You commented out //$to = 'john@petadmin.com'; and substituted $to = 'cats@cats.co.uk'; but is that correct?

Edited by Wickham

I looked at this for a while and failed to see a problem just like others here.

 

Does it work correctly if you try to send mail to: john@petadmin.com ?

 

Could be some sort of mail relaying filter on the mailserver??

 

As Lyndsey said make sure the $email variable does actually contain a value.

  • Author

I've have tried this just with a yahoo email address and it does'nt send to this either, I'm really foxed.

 

' As Lyndsey said make sure the $email variable does actually contain a value.'

 

where would I find this to check?

  • Author

For crying out loud now its won't send to the submitter email either, what the hell, it definitely did this earlier.

I've have tried this just with a yahoo email address and it does'nt send to this either, I'm really foxed.

 

' As Lyndsey said make sure the $email variable does actually contain a value.'

 

where would I find this to check?

 

I don't think the $email variable is your issue as that is related to the email sent out to the user, not to your business.

 

So, when you submit the form, it sends an email to the email address entered by the user, but not to the email address defined in the $to variable is that correct?

  • Author

Lyndsey yes that was exactly the problem though now it appears to not want to send anything, and the only thing I've changed is the $to

 

I'm pretty good at XHTML but PHP is something I really struggle to get my head around.

 

My host GoDaddy say that they have PHP 5.1 I thinking running, I presume this is fine?

 

The domain is held elsewhere and the DNS points to GoDaddy, but I think all this is irrelevant?

 

arrrggghh..............

Edited by Acknowledged74

  • Author

UM its sending to the email the person who fills in the form puts in to the email field, so confirmation that their form has been sent and a copy of the form itself. This shows the sender as donotreply.tannercats@petadmin.com

 

But nothing comes through to the

$to = 'cats@cats.co.uk';

//$to = 'john@petadmin.com';

 

;

 

addresses at all.

Are you sure cats@cats.co.uk is a real email address? Who receives it and where? You should be getting a bounce if the address doesn't exist, so perhaps you're not looking in the right place to receive it. Should $to be cats@petadmin.com which is the same domain as the From domain. Godaddy is notorious for making web forms difficult.

 

If the email to $email (the person submitting the form) is being sent, so far so good and the sender should be donotreply.cats@petadmin.com because that's what you have coded in $headers. I can only suggest moving the mail function which has $to after the one that works where it wil be after the if (!empty($email)) { code. Retype it, don't copy/paste because some strange things sometimes happen if you copy. So part of the code would be

...............
$headers = 'From: donotreply.cats@petadmin.com' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$Usermessage = 'Thank you for checking availability with Tannercats; the information below has been sent to us. We will contact you to discuss the details or alternatively, please feel free to call us.'."\r\n".$message;
$subject = 'Checking availability with cats.co.uk';
if (!empty($email)) {
mail($email, $subject, $Usermessage, $headers);
mail($to, $subject, $message, $headers);
}
//Message ...........etc.

Edited by Wickham

  • Author

That is a very good idea.

 

Mmmm nothing was sent to the $to email address, how strange.

 

Maybe its the actually email address thats the problem.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.