Well now i'm gonna show how to make a email form, but without
Email: field, cuz you will select a member who is in your database and then you can send an email to him/her. Let's start!
connect.php
PHP Code:
<?php
$host = "localhost";
$db_user = "YourUsername";
$db_pass = "YourPassword";
$db = "YourDatabase";
$connect = mysql_connect($host, $db_user, $db_pass);
if(!$connect){
die("Can not connect to database: ".mysql_error());
}
$select = mysql_select_db($db, $connect);
if(!$select){
die("Can not select a database: ".mysql_error());
}
?>
now let's make this form..
index.php
PHP Code:
<?php
include("connect.php");
// Display your email form
function form(){
echo "<form action='?act=send' method='post'>
<table width='400' border='1'>
<tr>
<td width='50%'>Send an email to:
<select name='users' size='1'>
<option selected></option>";
// Make a dropdown menu where are all your users with their emails
$item = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($item)){
echo "<option value='".$row['email']."'>".$row['username']."</option>";
}
echo "</select>
</td>
</tr>
<tr>
<td>";
echo "Your name: <input type='text' name='from' size='30'>
</td>
</tr>
<tr>
<td>";
echo "Subject: <input type='text' name='subject' size='30'><br>
</td>
</tr>
<tr>
<td>"
."Message:<br><textarea cols='50' rows='10' name='text'></textarea>
</td>
</tr>
<tr>
<td>";
echo "<input type='submit' value='Send'>
</td>
</tr>
</table>
</form>";
}
function send(){
// Let's collect our info
$email = $_REQUEST['users'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['text'];
$from = $_REQUEST['from'];
// If something is not filled then let's display errors
if(empty($email)){
die("Please enter a person who will get this email!");
}
if(empty($subject)){
die("Please enter a subject!");
}
if(empty($message)){
die("Please enter your message!");
}
if(empty($from)){
die("Please enter your name!");
}
// If everything is okay let's send that email
mail($email, $subject, $message,"From: ".$from);
echo "Thank you! <a href='index.php'>Go back</a>";
}
switch($act){
default;
form();
break;
case "send";
send();
break;
}
?>
Have fun!