Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Need help with PHP mail script [was: Can someone please help me?]

New Here ,
Oct 30, 2009 Oct 30, 2009

I'm trying to collect data from a form and email it.  I'm using form2mail.php and the problem is that the email is not collecting the form info and it has the same email address in the From: and To: lines. I'm really stuck and would appreciate any help.

Here is the HTML code:

<!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=iso-8859-1" />
<title>Contact Us</title>
<style type="text/css">
<!--
.style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style2 {
font-size: 14px;
color: #000000;
}
body {
background-color: #FFFFFF;
}
body,td,th {
color: #CC3300;
}
.style3 {font-size: 14px; font-weight: bold; }
.style6 {font-size: 18px}
.style7 {font-size: 16px}
.style8 {font-size: 16px; font-family: Verdana, Arial, Helvetica, sans-serif; }
.style9 {font-size: 16px; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; }
.style10 {color: #000000}
-->
</style>
</head>

<body>
<div align="center"><img src="nav2.jpg" alt="nav bar" width="698" height="91" border="0" usemap="#Map2" />
  <map name="Map2" id="Map2">
  <area shape="rect" coords="560,9,684,38" href="accessories.html" />
<area shape="rect" coords="456,8,548,38" href="contact.html" />
<area shape="rect" coords="305,8,435,40" href="photog.html" />
<area shape="rect" coords="187,9,283,39" href="services.html" />
<area shape="rect" coords="81,10,167,39" href="aboutus.html" />
<area shape="rect" coords="5,10,68,39" href="index.html" />
</map>
  <map name="Map" id="Map">
    <area shape="rect" coords="9,9,69,39" href="index.html" />
    <area shape="rect" coords="83,11,165,39" href="aboutus.html" />
    <area shape="rect" coords="182,9,285,38" href="services.html" />
    <area shape="rect" coords="436,14,560,37" href="contact.html" />
    <area shape="rect" coords="563,14,682,38" href="accessories.html" />
  </map>
</div>
<p> </p>
<form id="TheForm" name="TheForm" action="form2mail.php" method="post">
  <p align="center" class="style2">P<span class="style1">lease fill out form below for a &quot;free no obligation quote&quot; then click submit.</span></p>
  <p align="center" class="style3">(*Required Information)</p>
  <div align="center">
    <pre><strong><span class="style8">*Contact Name</span> </strong><input name="name" type="text" id="name" />
<span class="style8"><strong>
Business Name </strong></span><input name="bn" type="text" id="bn" />

<span class="style8"><strong>*Phone Number <input type="text" name="first" size="3" onFocus="this.value='';"
    onKeyup="checkNumber(this.value); autoTab(this, document.TheForm.second);" maxlength="3" value="###" /> - <input type="text" name="second" size="3" onFocus="this.value='';" onKeyup="checkNumber(this.value); autoTab(this, document.TheForm.third);" maxlength="3" value="###" /> - <input type="text" name="third" size="4" onFocus="this.value='';" onKeyup="checkNumber(this.value); autoTab(this, document.TheForm.fourth);" maxlength="4" value="####"/> </strong></span>

<strong><span class="style1">*</span><span class="style8">Email Address</span> <input name="email" type="text" id="email" />     </strong> </pre>
    <label><span class="style9">*Re-enter to confirm</span>
    <input name="emx" type="text" id="emx" /></label><br /><br /><span class="style9">
<label></label>
    </span>
    <p><span class="style9">*Best time to call </span>
      <select name="name1[]" multiple size="1" >
        <option>8am-9am</option>
        <option>9am-10am</option>
        <option>10am-11am</option>
        <option>11am-12pm</option>
        <option>12pm-1pm</option>
        <option>1pm-2pm</option>
        <option>2pm-3pm</option>
        <option>3pm-4pm</option>
        <option>4pm-5pm</option>
        <option>5pm-6pm</option>
        <option>6pm-7pm</option>
        <option>7pm-8pm</option>
      </select>
      <br />
      <br />
      <span class="style9">Type of Location</span>
      <select name="name2[]" multiple size="1" >
        <option>Residential</option>
        <option>Commercial</option>
      </select>
      <br />
      <br />
        <span class="style1"><br />
        <strong><br />
          <span class="style6">*Type of Services Requested:</span></strong><br />
        </span><strong><span class="style10">(check all that apply)</span><br />
            </strong><br />
            <span class="style7"><span class="style1"><strong>Janitorial cleaning</strong></span>
            <input type="checkbox" name="checkbox[]" value="checkbox" multiple/>
            <br />
            </span><strong><br />
              <span class="style8">Mobile Auto Detailing</span>
            <input type="checkbox" name="checkbox2[]" value="checkbox" multiple/>
            <br />
            <br />
              </strong><span class="style9">Moving/Hauling</span>
      <input type="checkbox" name="checkbox3[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">Pressure washing</span>
      <input type="checkbox" name="checkbox4[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">Window washing</span>
      <input type="checkbox" name="checkbox5[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">Window Tinting</span>
      <input type="checkbox" name="checkbox6[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">Boat cleaning</span>
      <input type="checkbox" name="checkbox7[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">RV cleaning</span>
      <input type="checkbox" name="checkbox8[]" value="checkbox" multiple/>
      <br />
      <br />
              <span class="style9">Motorcycle cleaning</span>
      <input type="checkbox" name="checkbox9[]" value="checkbox" multiple/>
      <br />
      <br />
      <br />
      <br />
      <input name="SB"  type="button" class="style9" value="Submit" onClick="sendOff();">
    </p>
  </div></label>

  <script language="JavaScript1.2">

// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header

var good;
function checkEmailAddress(field) {

// Note: The next expression must be all on one line...
//       allow no spaces, linefeeds, or carriage returns!
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org) |(\..{2,2}))$)\b/gi);

if (goodEmail){
   good = true
} else {
   alert('Please enter a valid e-mail address.')
   field.focus()
   field.select()
   good = false
   }
}

function autoTab(startPoint,endPoint){
if (startPoint.getAttribute&&startPoint.value.length==startPoint.getAttribute("max length"))
endPoint.focus();
}

function checkNumber(phoneNumber){
var x=phoneNumber;
var phoneNumber=/(^\d+$)|(^\d+\.\d+$)/
if (phoneNumber.test(x))
testResult=true

else{
alert("Please enter a valid number.")
phoneNumber.focus();
phoneNumber.value="";
testResult=false
}

return (testResult)
}

function sendOff(){
   namecheck = document.TheForm.name.value   
   if (namecheck.length <1) {
      alert('Please enter your name.')
      return
   }        
   good = false
   checkEmailAddress(document.TheForm.email)
   if ((document.TheForm.email.value ==
        document.TheForm.emx.value)&&(good)){
      // This is where you put your action
      // if name and email addresses are good.
      // We show an alert box, here; but you can
      // use a window.location= 'http://address'
      // to call a subsequent html page,
      // or a Perl script, etc.
      window.location= 'form2mail.php';

   }    
   if ((document.TheForm.email.value !=
          document.TheForm.emx.value)&&(good)){
          alert('Both e-mail address entries must match.')
   }
}
</script>
</form>
<p> </p>
</body>
</html>

and here is the form2mail.php:

<?php
# You can use this script to submit your forms or to receive orders by email.
$MailToAddress = "chuck@chucksmobile.com"; // your email address
$redirectURL = "http://www.chucksmobile.com/thankyou.html"; // the URL of the thank you page.
$MailSubject = "[Customer Contact Info]"; // the subject of the email
$sendHTML = FALSE; //set to "false" to receive Plain TEXT e-mail
$serverCheck = FALSE; // if, for some reason you can't send e-mails, set this to "false"

# copyright 2006 Web4Future.com =================== READ THIS ===================================================

# If you are asking for a name and an email address in your form, you can name the input fields "name" and "email".
# If you do this, the message will apear to come from that email address and you can simply click the reply button to answer it.

# To block an IP, simply add it to the blockip.txt text file.
# CHMOD 777 the blockip.txt file (run "CHMOD 777 blockip.txt", without the double quotes)
# This is needed because the script tries to block the IP that tried to hack it

# If you have a multiple selection box or multiple checkboxes, you MUST name the multiple list box or checkbox as "name[]" instead of just "name"
# you must also add "multiple" at the end of the tag like this: <select name="myselectname[]" multiple>
# you have to do the same with checkboxes

/*****************************************************************

Web4Future Easiest Form2Mail (GPL).
Copyright (C) 1998-2006 Web4Future.com All Rights Reserved.
http://www.Web4Future.com/
This script was written by George L. & Calin S. from Web4Future.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

*****************************************************************/

# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ===================================================
$w4fver =  "2.2";
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'] == "" ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
//function blockIP
function blockip($ip) {
$handle = @fopen("blockip.txt", 'a');
@fwrite($handle, $ip."\n");
@fclose($handle);
}
$w4fx = stristr(file_get_contents('blockip.txt'),getenv('REMOTE_ADDR'));
if ($serverCheck) {
if (preg_match ("/".str_replace("www.", "", $_SERVER["SERVER_NAME"])."/i", $_SERVER["HTTP_REFERER"])) { $w4fy = TRUE; } else { $w4fy = FALSE; }
} else { $w4fy = TRUE; }
if (($w4fy === TRUE) && ($w4fx === FALSE)) {
$w4fMessage = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html>\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body><font face=3Dverdana size=3D2>";
if (count($_GET) >0) {
reset($_GET);
while(list($key, $val) = each($_GET)) {
  $GLOBALS[$key] = $val;
  if (is_array($val)) {
   $w4fMessage .= "<b>$key:</b> ";
   foreach ($val as $vala) {
    $vala =stripslashes($vala);
    $vala = htmlspecialchars($vala);
    if (trim($vala)) { if (stristr($vala,"Content-Type:") || stristr($vala,"MIME-Version") || stristr($vala,"Content-Transfer-Encoding") || stristr($vala,"bcc:")) { blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
    $w4fMessage .= "$vala, ";
   }
   $w4fMessage .= "<br>\n";
  } 
  else {
   $val = stripslashes($val);
   if (trim($val)) { if (stristr($val,"Content-Type:") || stristr($val,"MIME-Version") || stristr($val,"Content-Transfer-Encoding") || stristr($val,"bcc:")) { blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
   if (($key == "Submit") || ($key == "submit")) { } 
   else {  if ($val == "") { $w4fMessage .= "$key: - <br>\n"; }
     else { $w4fMessage .= "<b>$key:</b> $val<br>\n"; }
   }
  }
} // end while
}//end if
else {
reset($_POST);
while(list($key, $val) = each($_POST)) {
  $GLOBALS[$key] = $val;
  if (is_array($val)) {
   $w4fMessage .= "<b>$key:</b> ";
   foreach ($val as $vala) {
    $vala =stripslashes($vala);
    $vala = htmlspecialchars($vala);
    if (trim($vala)) { if (stristr($vala,"Content-Type:") || stristr($vala,"MIME-Version") || stristr($vala,"Content-Transfer-Encoding") || stristr($vala,"bcc:")) {blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }   
    $w4fMessage .= "$vala, ";
   }
   $w4fMessage .= "<br>\n";
  } 
  else {
   $val = stripslashes($val);
   if (trim($val)) { if (stristr($val,"Content-Type:") || stristr($val,"MIME-Version") || stristr($val,"Content-Transfer-Encoding") || stristr($val,"bcc:")) {blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
   if (($key == "Submit") || ($key == "submit")) { } 
   else {  if ($val == "") { $w4fMessage .= "$key: - <br>\n"; }
     else { $w4fMessage .= "<b>$key:</b> $val<br>\n"; }
   }
  }
} // end while
}//end else
$w4fMessage .= "<font size=3D1><br><br>\n Sender IP: ".$ip."</font></font></body></html>";
    $w4f_what = array("/To:/i", "/Cc:/i", "/Bcc:/i","/Content-Type:/i","/\n/");
$name = preg_replace($w4f_what, "", $name);
$email = preg_replace($w4f_what, "", $email);
if (!$email) {$email = $MailToAddress;}
$mailHeader = "From: $name <$email>\r\n";
$mailHeader .= "Reply-To: $name <$email>\r\n";
$mailHeader .= "Message-ID: <". md5(rand()."".time()) ."@". ereg_replace("www.","",$_SERVER["SERVER_NAME"]) .">\r\n";
$mailHeader .= "MIME-Version: 1.0\r\n";
if ($sendHTML) {
  $mailHeader .= "Content-Type: multipart/alternative;";  
  $mailHeader .= "  boundary=\"----=_NextPart_000_000E_01C5256B.0AEFE730\"\r\n";    
}
$mailHeader .= "X-Priority: 3\r\n";
$mailHeader .= "X-Mailer: PHP/" . phpversion()."\r\n";
$mailHeader .= "X-MimeOLE: Produced By Web4Future Easiest Form2Mail $w4fver\r\n";
if ($sendHTML) {
  $mailMessage = "This is a multi-part message in MIME format.\r\n\r\n";
  $mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";
  $mailMessage .= "Content-Type: text/plain;   charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";  
  $mailMessage .= trim(strip_tags($w4fMessage))."\r\n\r\n";  
  $mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";  
  $mailMessage .= "Content-Type: text/html;   charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";  
  $mailMessage .= "$w4fMessage\r\n\r\n";  
  $mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730--\r\n";  
}
if ($sendHTML === FALSE) {
  $mailHeader .= "Content-Type: text/plain;   charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";  
  $mailMessage .= trim(strip_tags($w4fMessage))."\r\n\r\n";  
}

if (!mail($MailToAddress, $MailSubject, $mailMessage,$mailHeader)) { echo "Error sending e-mail!";}
else { header("Location: ".$redirectURL); }
} else { echo "<center><font face=verdana size=3 color=red><b>ILLEGAL EXECUTION DETECTED!</b></font></center>";}
?>

Thanks in advance,

Glenn

[Subject line edited by moderator to indicate nature of request]

TOPICS
Server side applications
2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2009 Oct 30, 2009

When asking for help, please use a meaningful subject line. You should also realize that most people will not have the time or patience to trawl through hundreds of lines of code to find a problem for someone else.

However, the main problem is easy to spot. You have misunderstood the instructions regarding select menus and checkboxes. You need to add the empty square brackets and multiple only when you want users to be able to select multiple options from a select menu. Also, your use of checkboxes is completely wrong.

The "name" attribute of each form element is what identifies the form input. Use meaningful name for your select menus, such as "calltime" and "location" (names should not have any spaces or special characters). Do not add square brackets and multiple if you want only one item to be selected from each menu.

For your checkboxes, give each checkbox the name options[], and put the value of the checkbox in the value attribute like this:

RV cleaning<input type="checkbox" name="options[]" value="RV cleaning" />

There might be other problems, but that should give you a start.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2009 Oct 30, 2009

Hi,

Thank you so much for your help. I have made the changes you suggested and am still getting the same response. I tried setting $serverCheck to TRUE in the Form2Mail.php and am receiving the following error "Illegal Execution Detected". I've been a Mainframe Programmer for the last 27 years, so I'm able to pick up programming languages fairly quick, but I have to admit I'm a little stumped with PHP code. Any more help you could give me would be greatly appreciated.

Thank You,

Glenn L. Gallo

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 31, 2009 Oct 31, 2009

Using PHP to process an online form and send the input by email is very simple. The mail() function takes a minimum of three arguments, namely: the address the mail is being sent to, the subject line, and the body of the email as a single string. The reason some people use scripts like form2mail.php is because they don't have the knowledge or patience to validate the user input to make sure it's safe to send.

Rather than attempt to trawl through your complex form looking for the problems, I would suggest starting with a couple of simple tests.

First of all, create a PHP page called mailtest.php containing the following script:

<?php

$to = 'me@example.com'; // use your own email address

$subject = 'PHP mail test';

$message = 'This is a test of the mail() function.';

$sent = mail($to, $subject, $message);

if ($sent) {

  echo 'Mail was sent';

} else {

  echo 'Problem sending mail';

}

?>

Save the script, upload it to your server, and load it into a browser. If you see "Mail is sent", you're in business. If not, it probably means that the hosting company insists on the fifth argument being supplied to mail(). This is your email address preceded by -f. Change the line of code that sends the mail to this:

$sent = mail($to, $subject, $message, null, '-fme@example.com');

Obviously, replace "me@example.com" with your own email address.

If this results in the mail being sent successfully, you will need to adapt the code in form2mail.php to accept the fifth parameter. You need to change the following line, which is a few lines from the end of the script:

if (!mail($MailToAddress, $MailSubject, $mailMessage,$mailHeader))

Change it to this:

if (!mail($MailToAddress, $MailSubject, $mailMessage,$mailHeader, '-fme@example.com'))

Again, use your own email address instead of "me@example.com".

Once you have determined whether you need the fifth argument, test form2mail.php with a very simple form like this:

<form id="form1" name="form1" method="post" action="form2mail.php">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label for="email">Email:</label>
    <input type="text" name="email" id="email" />
  </p>

  <p>
    <label>
      <input type="checkbox" name="options[]" value="boat cleaning" id="options_0" />
      Boat cleaning</label>
    <br />
    <label>
      <input type="checkbox" name="options[]" value="RV cleaning" id="options_1" />
      RV cleaning</label>
    <br />
    <label>
      <input type="checkbox" name="options[]" value="motorcycle cleaning" id="options_2" />
      Motorcycle cleaning</label>
  </p>
  <p>
    <input type="submit" name="send" id="send" value="Submit" />
  </p>

</form>

If that works, you can start to make the form more complex and add back your JavaScript validation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 31, 2009 Oct 31, 2009

I really appreciate all of your help! I was able to use your form and everything worked correctly. What I think is the problem is my onsubmit is calling the javascript to do edit checks. Within the javascript, I do a window.location to form2mail.php and I KNOW that is wrong! So I'm not sure on how to exit out of the javascript and submit the form to form2mail.php. Any suggestions? I REALLY appreciate your help.

Thank You,

Glenn L. Gallo

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 31, 2009 Oct 31, 2009

Hi,

Never mind. I FINALLY figured it out!! Thanks again for all of your help.

Thank You,

Glenn L. Gallo

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 01, 2009 Nov 01, 2009
LATEST

you can save yourself a lot of trouble by using one of these two extensions for dreamweaver.

http://www.cmiwebstudio.com/adobe_exchange.php

It does all of this for you automatically without hand coding.  Auto labels, collects, and distributes your form data.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines