Charas-Project

Off-Topic => Really Old Stuff => Archive => PHP => Topic started by: Shackles on April 23, 2009, 09:34:03 PM

Title: Site E-Mail Form
Post by: Shackles on April 23, 2009, 09:34:03 PM
Hey, I was wondering... I have been trying to make a form on my site, with just an input box that you type your e-mail, and another one for the message. Then a button to click to send it as an e-mail to me.

If you could show me how or make it for me it would be awesome!  :heart:
Title: Re: Site E-Mail Form
Post by: Osmose on April 25, 2009, 06:27:03 AM
Well, first you have to be sure that whoever your host is has PHP enabled for you; if you have a free host, this is not so likely.

The following also assumes you know HTML and are not an idiot (AKA you can look at the PHP and figure out what it's doing).

You have your form built in html:

Code: [Select]
...
<form name="wooForm" action="emailSubmit.php" method="post">
Email: <input type="text" name="email" /><br />
Message: <textarea name="msg"></textarea><br />
<br />
<input type="submit" name="submit" value="Email" />
</form>
...

As you can see, action points to emailSubmit.php, which is the PHP script that will send the email, and method defines how the message is sent ("get" sends the data in the URL, "post" sends it in the HTTP headers).

emailSubmit.php will probably look something like this:
Code: [Select]
<?php

// Probably not recommended for anything but a personal site
// this is easily exploited, there's no filtering, etc.

// Just send the email
$sent mail("youremail@something.com""Website Email from " $_POST['email'], $_POST['msg']);

// Tell the user if the email was accepted for sending or not
if ($sent) {
    echo 
"Mail sent successfully!";
} else {
    echo 
"We're sorry, your message could not be sent.";
}

?>


As you can see, the mail function takes three parameters (at minimum, see the PHP Documentation (http://www.php.net/manual/en/function.mail.php) for details):

It returns whether the email was accepted to be sent or not. Be aware that being accepted for sending doesn't mean it will actually reach its destination.

Like I said in the comments, this is easily exploitable, and if you're actually serious about making a good website, there's precautions that need to be made. You may be better off using a fancier email script (http://www.dagondesign.com/articles/secure-php-form-mailer-script/) (NOTE: I have not checked that script out, its just a starting point).