PDA

View Full Version : Batch e-mail from a form --PHP


LegionSmith
03-09-2007, 06:45 AM
Hi,

I've got a secure form for administrators enabling them to post recent news. The information from this form is then posted on the public front page of the website. No problem there, that works fine. BUT I need that same information entered into the form to ALSO be emailed to a list of about 200 subscribers. I have all those subscribers in a mysql table, and I'd like the php mail script to just pull the email addresses out of that table. Rather than having to enter all those email addresses into the script manually. I've found plenty of php mail programs that allow me to do that and that just won't work for this site. This functionality was available to my client with another provider that was using ASP for the site. I've converted all the ASP to php.
I'm using Dreamweaver 8 for this utilising recordsets for the form. Below I have pasted the form code and the Record Set code (minus the connection) from the top of the page. Any help would be much appreciated as this is really a big deal for this client.

**BEGIN FORM CODE**

form name="form1" action="<?php echo $editFormAction; ?>" method=POST>
<TABLE width="510" align=center border="1" ID="Table6" cellspacing=0 cellpadding=5 bordercolor="#000066">

<td colspan=2 bgcolor="#d9d9d9" align=center>
<b>Add Commission Corner Message</b>
</td>
</tr>


<tr>
<td width=30%>
Date
</td>
<td width=70%> <input type=text size=50 name=news_date ID="Text0">
</td>
</tr>


<tr>

<td width=30%>
Title
</td>
<td width=70%>
<input type=text size=50 name=news_title ID="Text1">
</td>
</tr>

<tr>
<td width=30% valign=top>

Content
</td>
<td width=70%>
<TEXTAREA cols=60 name=news_content rows=10 wrap=virtual ID="Textarea1"></TEXTAREA>



</td>
</tr>
<tr>
<td colspan=2 align=center>

<input type=submit value="Post Message" ID="Submit3" NAME="Submit1">
<input type=reset value="Reset Form" ID="Reset1" NAME="Reset1">
</td>
</tr>

</table>
<input type="hidden" name="MM_insert" value="form1">
</form>

**END FORM CODE**

**BEGIN RECORDSET CODE**
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO enews (news_date, news_title, news_content) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['news_date'], "date"),
GetSQLValueString($_POST['news_title'], "text"),
GetSQLValueString($_POST['news_content'], "text"));

mysql_select_db($database_baseball, $baseball);
$Result1 = mysql_query($insertSQL, $baseball) or die(mysql_error());
}
?>
**END RECORDSET CODE**

Thanks much,
Michael Smith

lux
03-09-2007, 01:57 PM
i would suggest using SwiftMailer: http://www.swiftmailer.org/

you should be up and running with it in a couple of hours and the docs are very clear and easy to understand.

LegionSmith
03-09-2007, 05:19 PM
Thanks for your reply Lux8uk,

I've just tried using swiftmailer and setting the form to point to a processing file. within this processing file I've got the connections for my recordsets, and for swiftmailer. Funny thing is that dreamweaver tells me my "insert record" recordset won't work because there is no form in this file. yet when I tested I it posted the info to mysql and to the frontpage. But it didn't email like I need it to.
Funny that the insert record behavior would work but the swiftmailer didn't...Also there is no redirect to a success or failure page at this moment, Obviously I get just a white page. I just don't understand how to make the form post to the front page AND email a list of people pulled from mysql database. I know it can be done because it was done before on this site using ASP and I know of PHP based shopping carts that can do it.
Here is the PHP in the processing file. The last two recipients emails and last names have been omitted from this posting.


<?php require_once('Connections/baseball.php'); ?>
<?php require_once "swift/Swift.php"; ?>
<?php require_once "swift/Swift/Connection/SMTP.php"; ?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_baseball, $baseball);
$query_rsenews = "SELECT * FROM enews";
$rsenews = mysql_query($query_rsenews, $baseball) or die(mysql_error());
$row_rsenews = mysql_fetch_assoc($rsenews);
$totalRows_rsenews = mysql_num_rows($rsenews);
mysql_select_db($database_baseball, $baseball);
$query_rssubscribers = "SELECT subscriber_name, subscriber_email FROM subscribers";
$rssubscribers = mysql_query($query_rssubscribers, $baseball) or die(mysql_error());
$row_rssubscribers = mysql_fetch_assoc($rssubscribers);
$totalRows_rssubscribers = mysql_num_rows($rssubscribers);
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO enews (news_date, news_title, news_content) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['news_date'], "date"),
GetSQLValueString($_POST['news_title'], "text"),
GetSQLValueString($_POST['news_content'], "text"));
mysql_select_db($database_baseball, $baseball);
$Result1 = mysql_query($insertSQL, $baseball) or die(mysql_error());
}
?>
<?php
mysql_free_result($rsenews);
mysql_free_result($rssubscribers);
?>
<?php
$swift =& new Swift(new Swift_Connection_SMTP("smtp.legiondata.org"));

$message =& new Swift_Message("Commission Corner E-News", "Latest news");

$recipients =& new Swift_RecipientList();
$recipients->addTo("michael@legiondata.org", "Michael Smith");
$recipients->addTo("@hotmail.com", "Lynn ");
$recipients->addTo("@aol.com", "Kathie ");
$swift->batchSend($message, $recipients, new Swift_Address("info@calb.org", "Calb.org"));
?>
Thanks,
Michael Smith

lux
03-09-2007, 06:06 PM
I would suggest that you first separate out the tasks that you are trying to complete to ensure each part works before you try to bundle it all together.

so create separate pages to handle adding the eNews article and using SwiftMailer to email the new news article to the list of subscribers.

Also i think you will have a number of problems if you try to use dreamweaver's code as it is not very forgiving when trying to include further functionality such as mailing a list of people when a new record is added to a db.

use davidj's video tutorials to understand how to add records to a database via a from here: http://www.dreamweaverclub.com/vtm/

give me a shout if you have more questions, but i really think trying to achieve want you want with dreamweavers automatically generated code will be very difficult

lux
03-10-2007, 10:17 AM
i have written this, based on davadj's tutorials for adding records to a db that adds news articles and users. use this as a starting point to work with swiftmailer.


<?php

include('Connections/CONN.php'); //database connection
///////////////////////////////////////////////////////////////////////
/* Code to add a new news article */
$newsTitle = $_POST['newsTitle'];
$newsBody = $_POST['newsBody'];
$newsSubmit = $_POST['newsSubmit'];

if(newsSubmit && $newsTitle && $newsBody)
{
$query = sprintf("insert into news (newsTitle, newsBody) values ('$newsTitle', '$newsBody')");
$execute = mysql_query($query) or die(mysql_error());
} elseif ($newsSubmit){
echo "You have not completed the news article form";
}

$newsQuery = sprintf("select * from news");
$newsResult = mysql_query($newsQuery);
$newsRow = mysql_fetch_array($newsResult);
/////////////////////////////////////////////////////////////////////
/* Code to add a new subscriber */

$subscriber = $_POST['subscriber'];
$email = $_POST['email'];
$subscriberSubmit = $_POST['subscriberSubmit'];

if($subscriberSubmit && $subscriber && $email)
{
$query = sprintf("insert into subscribers (subscriber, email) values ('$subscriber','$email')");
$execute = mysql_query($query) or die(mysql_error());
}elseif($subscriberSubmit) {
echo "You have not completed the subscriber form";
}

$subQuery = sprintf("select * from subscribers");
$subResult = mysql_query($subQuery);
$subRow = mysql_fetch_array($subResult);

?>


<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<table border="1" cellpadding="4">
<th colspan="2"><h1>User List and Email subscribers</h1></th>
<tr>
<td>
<h1>Add Subcriber Form</h1>
<form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<p>Subscriber Name<br /><input type="subscriber" name="subscriber" id="subscriber" /></p>
<p>Email addresss<br /><input type="text" name="email" id="email" /></p>
<input type="hidden" name="subscriberSubmit" value="1" />
<input type="submit" name="Submit" value="Submit" />
</form>
<p>
<table border="1">
<th colspan="3">List of subscribers articles</th>
<tr>
<td>Subscriber Id</td>
<td>Subscriber Name</td>
<td>Email</td>
</tr>
<?php

do { ?>
<tr>
<td><?php echo $subRow['subscriberId']; ?></td>
<td><?php echo $subRow['subscriber']; ?></td>
<td><?php echo $subRow['email'];?></td>
</tr>
<?php

}while($subRow = mysql_fetch_array($subResult))

?>

</table>
</p>
</td>
<td>
<h1>Add News Article Form</h1>
<form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<p>News Title<br /><input type="text" name="newsTitle" id="newsTitle" /></p>
<p>News Body<br /><textarea name="newsBody" id="newsBody"></textarea></p>
<input type="hidden" name="newsSubmit" value="1" />
<input type="submit" name="Submit" value="Submit" />
</form>
<p>
<table border="1">
<th colspan="4">List of news articles</th>
<tr>
<td>News Id</td>
<td>News Title</td>
<td>News Content</td>
<td>Email News</td>
</tr>
<?php

do { ?>
<tr>
<td><?php echo $newsRow['newsId']; ?></td>
<td><?php echo $newsRow['newsTitle']; ?></td>
<td><?php echo $newsRow['newsBody'];?></td>
<td><a href="#">Email</a></td>
</tr>
<?php

}while($newsRow = mysql_fetch_array($newsResult))

?>

</table>
</td>
</tr>
</table>

</p>
</body>
</html>



this is an sql dump of the db i created to work with the forms


# SQL Manager 2005 Lite for MySQL 3.7.7.1
# ---------------------------------------
# Host : localhost
# Port : 3306
# Database : mailer


SET FOREIGN_KEY_CHECKS=0;

CREATE DATABASE `mailer`;

USE `mailer`;

#
# Structure for the `news` table :
#

CREATE TABLE `news` (
`newsId` int(11) NOT NULL auto_increment,
`newsTitle` varchar(255) default NULL,
`newsBody` text,
PRIMARY KEY (`newsId`),
UNIQUE KEY `newsId` (`newsId`)
) TYPE=MyISAM;

#
# Structure for the `subscribers` table :
#

CREATE TABLE `subscribers` (
`subscriberId` int(11) NOT NULL auto_increment,
`subscriber` varchar(255) default NULL,
`email` varchar(255) default NULL,
PRIMARY KEY (`subscriberId`)
) TYPE=MyISAM;

#
# Data for the `news` table (LIMIT 0,500)
#

INSERT INTO `news` (`newsId`, `newsTitle`, `newsBody`) VALUES
(15,'news title','blah blah blah');

COMMIT;

#
# Data for the `subscribers` table (LIMIT 0,500)
#

INSERT INTO `subscribers` (`subscriberId`, `subscriber`, `email`) VALUES
(12,'subscriber','sub@email.com');

COMMIT;



read the swiftmailer docs and then give it a go. If thats no good search for php's mail() function either at php.net or google

regards

LegionSmith
03-10-2007, 03:55 PM
Lux,

ok cool, I'll go through that this weekend and see what I can do. I should be able to modify it for my site just fine. Swiftmailer's doc's seem a little light to me. Maybe it just looks too easy. BUt this is a big help.


I'll post any results as they come up.

Thanks a lot Lux I really appreciate the help.

Michael