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 23, 2009

Hi Thanks for your answer.

Here is what I still don' get.

This script works for me:

<?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);

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

$additional_params);

?>

As soon as I try to add the code below for security I get an error:

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

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

} else {

  $headers = null;

}

Where or how can I implement this code?

I am sorry I don;t have much experience other than what I am reading.

And many times there seems to be more than one way to do something.

Which is great if you know how.

Thanks in advance for your patience.

I still have a few more questions. I thought attempting to add this IF

statement would be a good place to start.