Skip to main content
Known Participant
November 22, 2009
Question

PHP - generat email from a form??

  • November 22, 2009
  • 1 reply
  • 1075 views

Hello,

I am designing a form.

Here is the link:

http://www.simplecelebrations.info/contact3.php

For the most part it works. Except for the self generated email function with the little bit of php script I've added.

<?php

$to = "thomas@reelcreative.tv";

$subject = $_POST['subject'];

$body = $_POST['comments'];

$headers = "From:relay-hosting.secureserver.net " . $_POST['emailAddress'] . "\n";

$mailSent = mail($to,$subject,$body,$headers);

?>

This script works. Except I want to add more information from the self generated email.. I want all the information from the form to display in the email I'd receive when a form is submitted.

It seems like there are a maximum of 5 parameters allowed. Is that correct? It doesn't seem right? -But why if I add an additional parameter like 'emailAddress' I get a line of code that shows:

<?php

$to = "thomas@reelcreative.tv";

$subject = $_POST['subject'];

$body = $_POST['comments'];

$headers = "From:relay-hosting.secureserver.net " . $_POST['emailAddress'] . "\n";

$emailAddress= $_POST['emailAddress'];

$mailSent = mail($to,$subject,$body,$emailAddress,$headers);

?>

/home/content/r/e/e/reelcompanies/dead.letter... Saved message in /home/content/r/e/e/reelcompanies/dead.letter

I've no idea where this error comes from? I gist know it the straw that breaks the Camel's back. Because when I remove this one additional parameter this line of code which shouldn't be, goes away.

How can I make my PHP code display all the fields from my form?

I've tried:

<?php

if (array_key_exists('***FormButton***', $_POST)) {

  $to = 'mailto:thomas@reelcreative.tv';

  $subject = $_POST['subject'];

$body = $_POST['comments'];

$headers = "From:relay-hosting.secureserver.net " . $_POST['emailAddress'] . "\n";

$mailSent = mail($to,$subject,$body,$headers);

}

?>

Which works - except when I add the 6th parameter to be displayed?

Actually correction - It works but I gee the line of code:

/home/content/r/e/e/reelcompanies/dead.letter... Saved message in /home/content/r/e/e/reelcompanies/dead.letter

Or something similar? - What is this how do I make it go away?

Thank you!

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
November 23, 2009

thomastables wrote:

This script works. Except I want to add more information from the self generated email.. I want all the information from the form to display in the email I'd receive when a form is submitted.

It seems like there are a maximum of 5 parameters allowed. Is that correct? It doesn't seem right?

Yes, it's right. You can't just add extra parameters to a PHP function and expect it to work. The syntax for mail is as follows:

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

The first three are required. The last two are optional. The whole body of the message must go in the third parameter as a single string.

So, there's your answer: combine all the form fields into a single string, using the combined concatenation operator:

$message = 'Name: '. $_POST['name'] . "\n";

$message .= 'Address: ' . $_POST['address'] . "\n";

$message .= 'Comments: ' . $POST['comments'];

By the way, putting the email address directly in the headers, as you are doing, is extremely insecure. You should filter it first, to make sure it's not attempting to inject spoof headers into your email and turn your form into a spam relay. If you are using PHP 5, you can do it like this:

if (filter_input(INPUT_POST, 'emailAddress', FILTER_VALIDATE_EMAIL)) {

  $headers = 'From: ' . $_POST['emailAddress'];

} else {

  $headers = null;

}

Known Participant
November 24, 2009

Hello,

I just wanted to Thank You.

The form works per your suggestions.

All the information is showing in the generated email.

I seriously don't how to immplement the filter.

Here is my current Code:

How and where do input the code the filter you mnetioned?

<?php

$to = "thomas@reelcreative.tv";

$subject =

$_POST['subject'];

$message .= 'firstName: ' . $_POST['firstName'] .

"\n";

$message .= 'lastName: '. $_POST['lastName'] . "\n";

$message .=

'emailAddress: ' . $_POST['emailAddress'] . "\n";

$message .= 'phoneNumber:

' . $_POST['phoneNumber'] . "\n";

$message .= 'newCustomer: ' .

$_POST['newCustomer'] . "\n";

$message .= 'Online: ' . $_POST['Online'] .

"\n";

$message .= 'instore: ' . $_POST['instore'] . "\n";

$message .=

'myspace: ' . $_POST['myspace'] . "\n";

$message .= 'facebook: ' .

$_POST['facebook'] . "\n";

$message .= 'twitter: ' . $_POST['twitter'] .

"\n";

$message .= 'referral: ' . $_POST['referral'] . "\n";

$message .=

'eventdescription: ' . $_POST['eventdescription'] . "\n";

$message .=

'Venue: ' . $_POST['Venue'] . "\n";

$message .= 'eventservices: ' .

$_POST['eventservices'] . "\n";

$message .= 'dancers: ' . $_POST['dancers']

. "\n";

$message .= 'models: ' . $_POST['models'] . "\n";

$message .=

'photog: ' . $_POST['photog'] . "\n";

$message .= 'guests: ' .

$_POST['guests'] . "\n";

$message .= 'duration: ' . $_POST['duration'] .

"\n";

$message .= 'comments: ' . $_POST['comments'] . "\n";

$headers =

"From:relay-hosting.secureserver.net " . $_POST['emailAddress'] .

"\n";

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

?>

Thank You very much

jon8
Inspiring
November 24, 2009

make your life simple.  take a look at the following extensions:

php mail: http://cmiwebstudio.com/adobe_extensions/dreamweaver_easy_php_mailer

php mail with pear mail and mail mime: http://cmiwebstudio.com/adobe_extensions/dreamweaver_easy_php_mailer_pear

it automates the entire thing.  simply fill in the mail server information, and it does the rest, emailing all of your form fields (no matter how many or what types).