Help with PHP script

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

DelphiRulezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
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
karoldvlCommented:
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
DelphiRulezAuthor Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

DelphiRulezAuthor Commented:
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
karoldvlCommented:
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
DelphiRulezAuthor Commented:
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
karoldvlCommented:
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
DelphiRulezAuthor Commented:
well, there is something in my fields

0
DelphiRulezAuthor Commented:
here is a screenshot of the form

0
karoldvlCommented:
Please post the source of your mail form.
0
DelphiRulezAuthor Commented:
here is a screenshot of the form

screenshot.jpg
0
DelphiRulezAuthor Commented:
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
karoldvlCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DelphiRulezAuthor Commented:
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
DelphiRulezAuthor Commented:
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
DelphiRulezAuthor Commented:
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
karoldvlCommented:
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
DelphiRulezAuthor Commented:
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
karoldvlCommented:
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
DelphiRulezAuthor Commented:
>>>Are you indeed getting the message if there are no form fields passed?

YES!
0
karoldvlCommented:
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
DelphiRulezAuthor Commented:
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
DelphiRulezAuthor Commented:
all i know is that its PHP 5.0, id have to call my HOST to find out the other info
0
karoldvlCommented:
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
DelphiRulezAuthor Commented:
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
DelphiRulezAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.