Send Email With PHP and MySQL

This PHP snippet receives the member ID from a form or from a link and retrieves the wanted data from the database, sends the e-mail and ends with one of two options.
Author: SimplytheBest.net Price: Free GPLv2 Type: PHP,MySQL

In this snippet, you receive the member ID from a form or from a link and retrieve the wanted data from the database, and send the e-mail using Sendmail.

<?
if (isset($_REQUEST["
id"]) && ($_REQUEST["id"]<>"")) {
$
data1=@mysql_query("SELECT * FROM your_table WHERE id=".$_REQUEST["id"]);
$
data=mysql_fetch_array($data1);
$
from =mymail@mydomain.com;
$
to = $data["email_field"];
$
user = $data["name_field"];
$
subject = "Enter your subject line here";  /*or if it comes from the table*/  $subject = $data["subject_field"];
$
header="From:" . $from . "\r\n " ."Reply-To:". $from ;
$
body="Hello ". $user .",\n\nFirst line\n\nYour message goes here, \nbest regards,\nyour_name and such\n";
mail ($
to,$subject,$body,$header);

Open a new window with a message, such as:

echo "Your e-mail has been sent";

Or redirect to another page:

header("Location: "."gotopage.php?msg=".urlencode("Your e-mail has been sent"));
exit();

End the IF loop. If the IF statement was not true, the script terminates.
//endif
?>

NOTES
  1. $_REQUEST["id"] can be $_POST["id"] if the input comes from a form that uses the POST method.
  2. ["id"] can also be coded as ['id'] or even [id]. For precise details see the PHP manual.
  3. There are an unlimited number of possibilities on building the data to be sent. You can build up the message, the header, or include even other variables such as f.e. BCC in the mail function.

F.e. the header (you can use header or headers) can contain many things such as:

$headers = "MIME-Version: 1.0\r\n"; //define the mime type
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
//define the character set
$headers .= "User-Agent: ".$this->agent."\r\n"; //define the user agent
$headers .= "Host: ".$this->host."\r\n"; //define the host
$headers .= "Accept-encoding: gzip\r\n"; //define the gzip format encoding
$headers .= "Referer: ".$this->referer."\r\n"; //define the referer
$headers .= "Return-Path: ".contact_email ."\r\n"; //define a return path

and some more:

$headers .= "Content-Type: application/$file_type";
$headers .= "Content-Disposition: attachment; filename=
$file_name.$ext";
$headers .= "Pragma: no-cache";
$headers .= "Expires:
0";

Or build up the message f.e.:

$getres=mysql_fetch_array(mysql_query("SELECT * FROM customers WHERE id='".$id."' "));
if ($_REQUEST['
cd'] == 'yes'){
$body="
Reseller ".$getres['company']." has just ordered ".$quantity." CDs of ".$product['description']." for a total amount of ".$total."";
} else {
$body="
Reseller ".$getres['company']." has just ordered ".$product['description']." for ".$getcust['first_name']."&nbsp;".$getcust['last_name']."";
}


Want to donate a little to SimplytheBest.net?