View Full Version : Out of my depth on PHP formmail?

10-26-2004, 04:02 AM
I am trying to set up an email newsletter subcription form for the boss's site. I am uanble to get to grips with the feedback form, sendmail php script using formmail script.

I am now in the dwang and would appreciate any help if possible.

The newsletter block is on


and the feedback form is at:

Cheers :confused:

10-26-2004, 02:50 PM
Depending on what you're doing with the form:


Check that out. All you really need in the formmail.php or whatever script you're sending the form data too is to have the mail() function and the form variables.

Is that all you are doing is sending the form data via email?

10-28-2004, 10:51 AM
If all you're doing is taking values from an email form and then emailing them to a certain web address, then it's easy. I managed it no problem and I'd never even heard of PHP 20 posts ago. Here's a simple example. Put the two files in the same directory, or alter the path in your form.


// Sets up the form to process the results using 'sendmail.php. Alter the path
// if 'sendmail.php' is in a different directory.

<form method="post" action="sendmail.php">

// Supplies a hidden value that will tell you what page the user came from.
// Not essential, only useful if you have more than one form like this.

<input type="hidden" name="page" value="Name of the page the form is on." class="textform"/>

// The actual form. Add extra inputs if you want more fields.

<td><input type="text" name="name" size="40" class="textform"/></td>
<td>Email Address</td>
<td><input type="text" name="email" size="40" class="textform"/></td>
<td><textarea name="notes" rows="10" cols="40" class="textform"></textarea></td>
<td align="right"><input type="reset" value="Clear"/></td>
<td><input type="submit" value="Send Info"/></td>


OK, so put that in your XHTML page, editing the style to match your page. It will send the values 'page' (which might not be necessary), 'name', 'email' and 'notes' to 'sendmail.php'. Adding more fields is easy enough, just put more inputs in with the relevant details.

Now the code for sendmail.php.


// Puts the user's email address in the variable $email.

$email = $_REQUEST['email'] ;

// Puts the message together. The bits in the "" are actual text, the other bits
// will insert the details from the form. \n is a line break.

$message = "Page:";
$message .= $_REQUEST['page'];
$message .= "\nName:";
$message .= $_REQUEST['name'];
$message .= "\nNotes:";
$message .= $_REQUEST['notes'];

// Sends the email to 'target@address.com' with a subject of 'Subject Of
// Email'. Inserts the message in the email. Says it's from the user's email
// address.

mail( "target@address.com", "Subject Of Email",
$message, "From: $email" );

// Redirects the user to www.yoursite/thanks.html.

header( "Location: http://www.yoursite/thanks.html" );

OK, so obviously you have to change a few things there to reflect your site and your email address.

.= function in PHP means 'add this to what's already there' so
$message .= $_REQUEST['notes']; means 'add 'notes' to the end of $message'. So if you've added new fields to your form in the XHTML you need to add them here too. For example, if there was an input in your form named 'location', you'd need to alter the bit that generates the message in the PHP to this:

$message = "Page:";
$message .= $_REQUEST['page'];
$message .= "\nName:";
$message .= $_REQUEST['name'];
$message .= "\nNotes:";
$message .= $_REQUEST['notes'];
$message .="\nLocation:";
$message .= $_REQUEST['location'];

So say some one fills in the form like this:

Name Me
Email me@me.com
Notes Hello world!
Location Somewhere

this would send an email to the address you specify that looks like his:

Name : Me
Email : me@me.com
Notes : Hello world!
Location : Somewhere

Hope this helps. It's the first bit of PHP advice I've given out. Let me know if it works!