[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Help with PHP script

Posted on 2010-04-08
26
Medium Priority
?
225 Views
Last Modified: 2012-05-09
PHP 5.X

I am a novice when it comes to writing PHP script. I am trying to create a basic email form and build upon it as i learn more.

I have four fields:

name = fldName

email = fldEmail

phone = fldPhone

engine = fldEngine


I have attached a code snipplet of my PHP script

The script executes, but when i receive the email (email address in script is not real email address), all i receive is the following:

I receive the Name the subject line
I receive the Email in the body of the email

I dont get a phone or a engine anywehere in the email


Question1: Why is the Name showing in the subject ?

Question 2: Why don't i get Phone or Engine in the body?

Question 3: How can I make my from say something other than my IP's email address.

thanks
<?php 

$name = $_REQUEST['fldName'] ; 

$email = $_REQUEST['fldEmail'] ; 

$phone = $_REQUEST['fldPhone'] ; 

$engine = $_REQUEST['fldEngine'] ; 

mail("janedoe@anywhere.com", "Name: $name", "Email: $email", "Phone: $phone", "Engine: $engine"); 

echo "Thank you for using our mail form";

?>

Open in new window

0
Comment
Question by:DelphiRulez
  • 16
  • 9
26 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 30120927
Hi DelphiRulez,

the parameters you pass have to be in this order:

email-address, subject, message, additional headers (optional).

So I guess you should do it like this:

> mail( "janedoe@anywhere.com", "Subject ...", "Message ...", "Name: $name" . "\r\n" . "Email: $email" . "\r\n" . "Phone: $phone" . "\r\n" . "Engine: $engine");

Thus the subject of the mail should be 'Subject ...', the mail's body should be 'Message ...'


BTW, I don't know if these additional header entries like 'Name', 'Email' a.s.o. can be chosen free or have to belong to any set of standard-headers. But, at least I found in PHP docu that a 'From:' header must be given.

Here you can find further info about 'mail' command: http://www.php.net/manual/en/function.mail.php

Hope that helps,

ZOPPO
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30120934
The syntax for mail is as follows:
bool  mail     (  string  $to    ,  string  $subject    ,  string  $message    [,  string  $additional_headers    [,  string  $additional_parameters   ]] )

Check PHP manual for clarification.

You would probably want something like this:
mail("janedoe@anywhere.com", "My mail form subject", "Name: $name\nEmail: $email\nPhone: $phone\nEngine: $engine");
0
 

Author Comment

by:DelphiRulez
ID: 30122497
Zoppo:

mail("janedoe@anywhere.com", "Parts & Accessories", "Message:", "Name: $name"."\r\n"."Email: $email"."\r\n"."Phone: $phone"."\r\n"."Engine: $engine");


This does not cause an error, and I get the email with the correct subject and message in body,

but none of my actual form fields are in the email

"Name: $name".
"Email: $email"
"Phone: $phone"
"Engine: $engine"

Also, i didn't see any response to my third question
 

0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 

Author Comment

by:DelphiRulez
ID: 30122725
karoldvl:

using your suggestion:

mail("janedoe@anywhere.com", "Parts & Accessories", "Message:", "Name: $name\nEmail: $email\nPhone: $phone\nEngine: $engine");


I get the exact same results as I described above with Zoppo's answer
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30123047
If you're not seeing name and so on, then probably it's not populated.

Check:
echo $name;

Whole script would be useful here.

As to question #3, the following snippet should work:

$to      = 'janedoe@anywhere.com';
$subject = 'Subject of the message';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: example@example.com' . "\r\n"
         . 'Reply-To: example@example.com' . "\r\n";

mail($to, $subject, $message, $headers);

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 30123706
karoldvl:

your code work most of the way, the actual values from the fields where not there. The Body of the email looked like this below:

Name:
Email:
Phone:
Engine:


here is the code i used : using your example above

<?php

$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);

echo "Thank you for using our mail form";

?>

0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30123995
How does your whole page look like (mail form)?

If you're not seeing the content of those variables at this point, then there's nothing to be sent.
0
 

Author Comment

by:DelphiRulez
ID: 30124141
well, there is something in my fields

0
 

Author Comment

by:DelphiRulez
ID: 30124287
here is a screenshot of the form

0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30124298
Please post the source of your mail form.
0
 

Author Comment

by:DelphiRulez
ID: 30124362
here is a screenshot of the form

screenshot.jpg
0
 

Author Comment

by:DelphiRulez
ID: 30124785
Please find attached, the code to my form:

As you can tell, my script above only refers to 4 fields in the form. This is because I was trying to get the script to work properly before filling in the rest of the fields.
<td height="306"><form id="form1" name="form1" method="post" action="sendmail.php">
      <p>&nbsp;</p>
      <table width="100%" border="0">
        <tr>
          <td><p>NAME
            </p>
            <p>
              <input type="text" name="fldName" id="fldName" />
            </p></td>
          <td><p>EMAIL
            </p>
            <p>
              <input type="text" name="fldEmail" id="fldEmail" />
            </p></td>
          <td><p>PHONE
            </p>
            <p>
              <input type="text" name="fldPhone" id="fldPhone" />
            </p></td>
          </tr>
        <tr>
          <td><p>ENGINE
            </p>
            <p>
              <select name="fldEngine" id="fldEngine">
                <option>Johnson</option>
                <option>Mercury</option>
                <option>Evinrude</option>
                <option>Chrysler</option>
              </select>
            </p></td>
          <td><p>YEAR
            </p>
            <p>
              <input type="text" name="fldYear" id="fldYear" />
            </p></td>
          <td><p>HORSEPOWER
            </p>
            <p>
              <input type="text" name="fldHorsepower" id="fldHorsepower" />
            </p></td>
          </tr>
        <tr>
          <td><p># CYLINDERS</p>
            <p>
              <input type="text" name="fldCylinders" id="fldCylinders" />
            </p></td>
          <td><p>MODEL # </p>
            <p>
              <input type="text" name="fldModel" id="fldModel" />
            </p></td>
          <td><p>SERIAL #
            </p>
            <p>
              <input type="text" name="fldSerial" id="fldSerial" />
            </p></td>
          </tr>
      </table>
      <p>Please list the description of parts needed, or part numbers if known</p>
      <table width="100%" border="0">
        <tr>
          <td><label>Part 1
            <input type="text" name="fldPart1" id="fldPart1" />
          </label></td>
          <td><label>Part 4
            <input type="text" name="fldPart4" id="fldPart4" />
          </label></td>
          <td><label>Part 7
            <input type="text" name="fldPart7" id="fldPart7" />
          </label></td>
        </tr>
        <tr>
          <td><label>Part 2
            <input type="text" name="fldPart2" id="fldPart2" />
          </label></td>
          <td><label>Part 5
            <input type="text" name="fldPart5" id="fldPart5" />
          </label></td>
          <td><label>Part 8
            <input type="text" name="fldPart8" id="fldPart8" />
          </label></td>
        </tr>
        <tr>
          <td><label>Part 3
            <input type="text" name="fldPart3" id="fldPart3" />
          </label></td>
          <td><label>Part 6
            <input type="text" name="fldPart6" id="fldPart6" />
          </label></td>
          <td><label>Part 9
            <input type="text" name="fldPart9" id="fldPart9" />
          </label></td>
        </tr>
      </table>
      <p>Comments / Questions / Additional Parts     </p>
      <p>
        <textarea name="fldComments" id="fldComments" cols="80" rows="5"></textarea>
      </p>
      <p>
        <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
        <input type="reset" name="btnSubmit2" id="btnSubmit2" value="Reset" />
      </p>
    </form></td>

Open in new window

0
 
LVL 6

Accepted Solution

by:
karoldvl earned 2000 total points
ID: 30125349
Please check if this works.

This is rudimentary. For production use you would need some form of validation.

<?php

if (isset($_POST['fldName'])) $name = $_POST['fldName'];
if (isset($_POST['fldEmail'])) $email = $_POST['fldEmail'];
if (isset($_POST['fldPhone'])) $phone = $_POST['fldPhone'];
if (isset($_POST['fldEngine'])) $engine = $_POST['fldEngine'];

$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);

echo "Thank you for using our mail form";

?>

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 30126101
karoldvl:

I tried your code above, and the code below (attached)

and both of them look like they process, since I get the echo message, but, i never receive the email, from eith your code above, or my code below

<?php

$name = $_REQUEST['fldName'] ; 

$email = $_REQUEST['fldEmail'] ; 

$phone = $_REQUEST['fldPhone'] ; 

$engine = $_REQUEST['fldEngine'] ; 

$to      = 'holmesshanea@yahoo.com'; 
$subject = 'Parts & Accessories'; 
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n" 
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine; 
$headers = 'From: geolobos@yahoo.com' . "\r\n" 
         . 'Reply-To: geolobos@yahoo.com' . "\r\n"; 
 
mail($to, $subject, $message, $headers);

echo "Thank you for using our mail form";

?>

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 30126236
I change the script back to:

<?php
 
if (isset($_POST['fldName'])) $name = $_POST['fldName'];
if (isset($_POST['fldEmail'])) $email = $_POST['fldEmail'];
if (isset($_POST['fldPhone'])) $phone = $_POST['fldPhone'];
if (isset($_POST['fldEngine'])) $engine = $_POST['fldEngine'];
 
$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);
 
echo "Thank you for using our mail form";
 
?>


and i get the email , but its missing the field data again
0
 

Author Comment

by:DelphiRulez
ID: 30126281
oops, sorry, i meant to say, i changed the script back to:

<?php
 
$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);
 
echo "Thank you for using our mail form";
 
?>

and i get the email , but its missing the field data again
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30129763
I'm not sure. The same construct is working OK on my side.

Please see what's the output of this script (sendmail.php) when you submit the form:

<?php

print_r($_POST);
 
if (isset($_POST['fldName'])) $name = $_POST['fldName'];
if (isset($_POST['fldEmail'])) $email = $_POST['fldEmail'];
if (isset($_POST['fldPhone'])) $phone = $_POST['fldPhone'];
if (isset($_POST['fldEngine'])) $engine = $_POST['fldEngine'];
 
$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n"
         . 'Phone: '.$phone."\r\n".'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";

echo 'TO: '.$to;
echo 'SUBJECT: '.$subject;
echo 'MESSAGE: '.$message;
echo 'HEADERS: '.$headers;
 
mail($to, $subject, $message, $headers);
 
echo "Thank you for using our mail form";
 
?>

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 30130052
I received no email, and this was displayed in my browser

Array ( [fldName] => shane holmes [fldEmail] => holmesshanea@yahoo.com [fldPhone] => 315-393-1551 [fldEngine] => Mercury [fldYear] => [fldHorsepower] => [fldCylinders] => [fldModel] => [fldSerial] => [fldPart1] => [fldPart4] => [fldPart7] => [fldPart2] => [fldPart5] => [fldPart8] => [fldPart3] => [fldPart6] => [fldPart9] => [fldComments] => [btnSubmit] => Submit ) TO: holmesshanea@yahoo.comSUBJECT: Parts & AccessoriesMESSAGE: Name: shane holmes Email: holmesshanea@yahoo.com Phone: 315-393-1551 Engine: MercuryHEADERS: From: geolobos@yahoo.com Reply-To: geolobos@yahoo.com Thank you for using our mail form
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30131685
I don't know. Everything seems fine. This looks like a problem with the mailer. Are you indeed getting the message if there are no form fields passed? Please check if you have some error messages in your mailbox/mailer log.

As a last resort, please see what's the result of this script:

<?php

$name   = isset($_POST['fldName'])   ? $_POST['fldName']   : '';
$email  = isset($_POST['fldEmail'])  ? $_POST['fldEmail']  : '';
$phone  = isset($_POST['fldPhone'])  ? $_POST['fldPhone']  : '';
$engine = isset($_POST['fldEngine']) ? $_POST['fldEngine'] : '';

$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'Name: '.$name."\n"
		 . 'Email: '.$email."\n"
         . 'Phone: '.$phone."\n"
         . 'Engine: '.$engine;
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";

if (mail($to, 'Mail with content', $message, $headers)) {
	echo '#1 Mail successfully sent';
} else {
	echo '#1 Problems sending mail';
}

if (mail($to, 'Mail without content', 'Test', $headers)) {
	echo '#2 Test mail successfully sent';
} else {
	echo '#2 Problems sending test mail';
}

echo "Debug: \n\n";

print_r($_POST);

echo 'TO: '.$to;
echo 'SUBJECT: '.$subject;
echo 'MESSAGE: '.$message;
echo 'HEADERS: '.$headers;

?>

Open in new window

0
 

Author Comment

by:DelphiRulez
ID: 30134926
>>>Are you indeed getting the message if there are no form fields passed?

YES!
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30135397
This is bizarre. And what's the output of the latest script?

What are your server specs (OS, PHP version, Mail Transfer Agent)?
I'm slowly running out of ideas. Maybe someone will be able to look at it from a fresh perspective.
0
 

Author Comment

by:DelphiRulez
ID: 30135503
i received this:

#1 Mail successfully sent#2 Test mail successfully sentDebug: Array ( [fldName] => Shane Holmes [fldEmail] => holmesshanea@yahoo.com [fldPhone] => 518-624-9818 [fldEngine] => Evinrude [fldYear] => [fldHorsepower] => [fldCylinders] => [fldModel] => [fldSerial] => [fldPart1] => [fldPart4] => [fldPart7] => [fldPart2] => [fldPart5] => [fldPart8] => [fldPart3] => [fldPart6] => [fldPart9] => [fldComments] => [btnSubmit] => Submit ) TO: holmesshanea@yahoo.comSUBJECT: Parts & AccessoriesMESSAGE: Name: Shane Holmes Email: holmesshanea@yahoo.com Phone: 518-624-9818 Engine: EvinrudeHEADERS: From: geolobos@yahoo.com Reply-To: geolobos@yahoo.com

but i didn't get an email

yes, If i use this script:

<?php

$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'message';
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);

echo "Thank you for using our mail form";

?>


I get an email  with the words "Parts & Accessories" in the subject line and "message" in the body
0
 

Author Comment

by:DelphiRulez
ID: 30135556
all i know is that its PHP 5.0, id have to call my HOST to find out the other info
0
 
LVL 6

Expert Comment

by:karoldvl
ID: 30137567
It's weird that you didn't get even the "empty" mail from the latest script.

This line:
if (mail($to, 'Mail without content', 'Test', $headers)) {

is practically equivalent to your script:
$to      = 'holmesshanea@yahoo.com';
$subject = 'Parts & Accessories';
$message = 'message';
$headers = 'From: geolobos@yahoo.com' . "\r\n"
         . 'Reply-To: geolobos@yahoo.com' . "\r\n";
 
mail($to, $subject, $message, $headers);


And everything shows that the message was delivered to the local mail system, so it's probably from this point that you experience some problems.

What's left is to check all spam/junk folders, look for error messages and ask the host to verify the logs if the messages are sent/what's the reason for dropping.

Replacing this line:
if (mail($to, 'Mail with content', $message, $headers)) {

with:
if (mail($to, 'Mail with content', $message, $headers, '-fgeolobos@yahoo.com')) {

could help debugging (you should get errors on your geolobos account), but this depends on the configuration of the host (may not work in some cases).



0
 

Author Comment

by:DelphiRulez
ID: 30137823
i said......i get the emails

if i use a script without the form fields, i get the emails.

if i use a script with the form fields, i get the emails, and the field text, but not the data

never mind, i will try and figure this out somewhere else, this thread has gotten too long and out of control

i'll be back to display the answer when i solve it (im busy doing something else right now)

0
 

Author Comment

by:DelphiRulez
ID: 30306808
Solved!
<?php

$name = $_POST['fldName'];
$phone = $_POST['fldPhone'];
$email = $_POST['fldEmail'];
$engine = $_POST['fldEngine'];
$year = $_POST['fldYear'];
$horsepower = $_POST['fldHorsepower'];
$cylinders = $_POST['fldCylinders'];
$model = $_POST['fldModel'];
$serial = $_POST['fldSerial'];
$part1 = $_POST['fldPart1'];
$part2 = $_POST['fldPart2'];
$part3 = $_POST['fldPart3'];
$part4 = $_POST['fldPart4'];
$part5 = $_POST['fldPart5'];
$part6 = $_POST['fldPart6'];
$part7 = $_POST['fldPart7'];
$part8 = $_POST['fldPart8'];
$part9 = $_POST['fldPart9'];
$comments = $_POST['fldComments'];


$to      = 'janedoe@yahoo.com'; 
$subject = 'Parts & Accessories'; 

$message = 'Name: '.$name."\r\n".'Email: '.$email."\r\n".'Phone: '.$phone."\r\n".'Engine: '.$engine."\r\n".'Year: '.$year."\r\n".'Horsepower: '.$horsepower."\r\n".'Cylinders: '.$cylinders."\r\n".'Model: '.$model."\r\n".'Serial: '.$serial."\r\n".'Part1: '.$part1."\r\n".'Part2: '.$part2."\r\n".'Part3: '.$part3."\r\n".'Part4: '.$part4."\r\n".'Part5: '.$part5."\r\n".'Part6: '.$part6."\r\n".'Part7: '.$part7."\r\n".'Part8: '.$part8."\r\n".'Part9: '.$part9."\r\n".'Comments: '.$comments; 

$headers = 'From: janedoe@yahoo.com' . "\r\n" 
         . 'Reply-To: janedoe@yahoo.com' . "\r\n"; 
 
mail($to, $subject, $message, $headers);

echo "Thank you for using our mail form";

?>

Open in new window

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question