Jump to content

Email form


Html

Recommended Posts

If you post the form you want to use we can show you how to use PHP to process the form and send the email. There is also a tutorial on the w3schools site about PHP Forms, and you can check the PHP tips sticky thread in this forum for another description about how to process forms. Sending email is easy, there is a mail function that you can use to do that once you have all the form info.

Link to comment
Share on other sites

<form action=".php" method="post"><br>Name:<input type="text" name="name"<br><br>Commens:<br><textarea name="text" rows="8" cols="30"></textarea><br><input type="submit"></form>

Link to comment
Share on other sites

In the PHP script that processes that, you can access name using $_POST['name'] and the comments field using $_POST['text'], since those are the names that you gave to the elements. You can check if those are empty in the script, and build your email message. Then you can use the mail function to send the mail. Here are two examples from php.net of sending a simple mail, and another one with extra headers.

<?php// The message$message = "Line 1\nLine 2\nLine 3";// In case any of our lines are larger than 70 characters, we should use wordwrap()$message = wordwrap($message, 70);// Sendmail('caffinated@example.com', 'My Subject', $message);?>

<?php$to	  = 'nobody@example.com';$subject = 'the subject';$message = 'hello';$headers = 'From: webmaster@example.com' . "\r\n" .	'Reply-To: webmaster@example.com' . "\r\n" .	'X-Mailer: PHP/' . phpversion();mail($to, $subject, $message, $headers);?>

http://www.php.net/manual/en/function.mail.phpTry out a couple things and see how it goes, make sure to also check the PHP forms resources I mentioned earlier.

Link to comment
Share on other sites

It's probably more intuitive then you think, the mail function is pretty simple to use (thankfully we don't need to worry about the actual mail transfer behind the scenes). All you need to send an email is the subject, the To address, and the message. Optionally you can add the From address and whatever else (the second example does that).The subject and the To address will probably remain the same, so all you need to worry about is building the message. You can get the form values using the $_POST array, and from there you just need to build the message string. You can use the \n character for a newline.

$message = "Name: {$_POST['name']}\nComments: {$_POST['text']}";

The first example above also uses the wordwrap function to make sure lines aren't too long, some mail agents have problems with long lines for whatever reason.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...