I have been learning PEAR and how to use some of its functions to send form data via the Mail.php in PEAR. To try this you will need to insure you have PEAR installed in your server and know the exact location. If you have your php.ini in the root of your site then it will be easy.
Example: include("../usr/bin/pear/Mail.php"); here is the exact address to the pear file if you php.ini file is located in the server root
Example: include("Mail.php"); here is the exact address to the pear file if you php.ini file is located in the site root
I am also including a method to save data into a database since this really makes the form data more usable later.
First you create your Form: (save as form.php or include in your webpage)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pear mailer example</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="processor.php">
<label>Name<br />
<input type="text" name="name" />
</label>
<p>
<label>Email<br />
<input type="text" name="email" />
</label>
</p>
<p>
<label>Subject<br />
<input type="text" name="textfield" />
</label>
</p>
<p>
<label>Message<br />
<textarea name="message" cols="30" rows="5"></textarea>
</label>
</p>
</form>
</body>
</html>
Second you need to create a config.php file to connect to your DB:
<?php
$db_host = "your host";$db_user = "user";$db_pass = "pass";$db_name = "db name";$db_table = "table";
?>
Finally we are going to create the guts:(save this as processor.php)
<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
include("config.php");///DB config file
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
//saving all to database before sending mail
$query = "INSERT into `".$db_table."` (name,email,subject,message) VALUES ('" . $_POST['name'] . "','" . $_POST['email'] . "','" . $_POST['subject'] . "','" . $_POST['message'] . "')";
mysql_query($query);
mysql_close($link);
include ("Mail.php");//../usr/bin/pear/ here is the exact address to the pear file
///////////////////pear smtp include data///////////////
$from = "From: <info@yoursite.com>";
$to = "Your Title here <info@yoursite.com>";
$subject = "your subject here";
$body = "Form data:
Name: " .$_POST['name']."
email: ".$_POST['email']."
subject: ".$_POST['subject']."
message: " .$_POST['message']."<br>your Company moto";
//this you can actually create a seperate file for and use the require_once statement
$host = "mail.yoursite.com";/// ex: mail.yoursite.com
$username = "user id";
$password = "password";
/////Sending everything through the Pear functions
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
include("confirm.php");
?>
Now we create the confirm.php for redirection to page if everything went successfully:
<?php
echo "<script type=\"text/javascript\">";
echo "window.location=\"http://yoursite.com/your_page.html\";</script>";
?>
Notice I used java here since the header redirection function will not work.