Hello, mhffs:
I have created a base story mailer script that can be adapted to
your table, but in any case, don't hesitate to request for any
clarification to make it fit your needs. To download it, click the
following link:
http://www.qadram.com/storymailer.zip
It includes ADOdb, a database abstraction layer I usually use to
access databases, this way, you can change the script to use another
database server (i.e PostgreSQL, Oracle,...) by just changing a single
line of code.
ADOdb
http://phplens.com/lens/dl/adodb290.zip
This is the table I used to design the script:
CREATE TABLE `articles` (
`article_id` int(13) unsigned NOT NULL auto_increment,
`article_title` varchar(120) NOT NULL default '',
`article_date` datetime NOT NULL default '0000-00-00 00:00:00',
`article_text` text NOT NULL,
PRIMARY KEY (`article_id`),
UNIQUE KEY `article_id` (`article_id`),
KEY `article_id_2` (`article_id`)
) TYPE=MyISAM;
And here is the script code, but I recommend you to download the whole
version including ADOdb to make it work:
<?php
//Change this settings to fit your setup
$db_host='localhost';
$db_user='root';
$db_pass='root';
$database='test';
//Change this settings to fit your table setup
$articles_table='articles';
$article_id='article_id';
$article_title='article_title';
$article_date='article_data';
$article_text='article_text';
//Database abstraction layer
include "adodb/adodb.inc.php";
function checkRequiredData()
{
$your_name=$_POST['your_name'];
$your_email=$_POST['your_email'];
$friend_name=$_POST['friend_name'];
$friend_email=$_POST['friend_email'];
if ((empty($your_name)) || (empty($your_email)) ||
(empty($friend_name)) || (empty($friend_email)))
{
return(FALSE);
}
else return(TRUE);
}
function askRequiredData($msg)
{
global $sid;
global $PHP_SELF;
?>
<form name="mailerform" method="post" action="<?php echo
$PHP_SELF."?action=send&sid=$sid"; ?>">
<table width="313" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"><?php echo $msg; ?><br><br></td>
</tr>
<tr>
<td width="142">Your name:</td>
<td width="171">
<input type="text" name="your_name">
</td>
</tr>
<tr>
<td width="142">Your e-mail:</td>
<td width="171">
<input type="text" name="your_email">
</td>
</tr>
<tr>
<td width="142">Your friend's name:</td>
<td width="171">
<input type="text" name="friend_name">
</td>
</tr>
<tr>
<td width="142">Your friend's e-mail:</td>
<td width="171">
<input type="text" name="friend_email">
</td>
</tr>
<tr>
<td width="142"> </td>
<td width="171">
<input type="submit" name="Submit" value="Send">
</td>
</tr>
</table>
</form>
<?php
}
if (isset($sid))
{
//Connect to the database
$articles_db = NewADOConnection('mysql');
$articles_db->Connect($db_host,$db_user,$db_pass,$database);
//Perform the query
$sql="select * from $articles_table where $article_id=$sid";
$rs=$articles_db->Execute($sql);
if ($rs->RowCount()<=0) die("The requested article don't
exists!!");
//Extract the data
$title=$rs->Fields($article_title);
$art_date=$rs->Fields($article_date);
$art_text=$rs->Fields($article_text);
//Change this line to your url, to allow the recipient read the
article online
$url="http://www.yoursite.com/articles.php?id=$sid";
//The send operation is set
if ((isset($action)) && ($action=='send'))
{
//Check for required data
$required=checkRequiredData();
if (!$required)
{
//Ask again
askRequiredData("You must field all the fields!!");
}
else
{
//Get all the form data
$your_name=$_POST['your_name'];
$your_email=$_POST['your_email'];
$friend_name=$_POST['friend_name'];
$friend_email=$_POST['friend_email'];
//Build the message
$msg="Hello, $friend_name:\n";
$msg.="\n";
$msg="Your friend $your_name considered interesting the
following article and wanted to send it to you.\n";
$msg.="\n";
$msg.="$title\n";
$msg.="$art_date\n";
$msg.="\n";
$msg.="$art_text\n";
$msg.="\n";
$msg.="You can read it online at $url\n";
$msg.="\n";
$msg.="Regards.\n";
//Send mail
$headers="From: $your_email\r\n"
."Reply-To: $your_email\r\n";
if (mail($friend_email, "$your_name sends you this interesting
article", $msg, $headers))
{
echo "Mail sent successfully!!";
}
else
{
echo "There was an error sending mail";
}
}
}
else
{
askRequiredData("You will send the story <b>$title</b> to a
specified friend:");
}
$articles_db->Disconnect();
}
else
{
die ("To use this script, you must provide a Story ID!!");
}
?>
I hope this is what you need, don't hesitate to request for any
clarification.
Regards. |