Forums

PHP

This topic is locked

Batch email from a Form-PHP

Posted 09 Mar 2007 08:43:26
1
has voted
09 Mar 2007 08:43:26 Michael Smith posted:
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">
&lt;/form&gt;<b></b>

**END FORM CODE**

**BEGIN RECORDSET CODE**
&lt;?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());
}
?&gt;
**END RECORDSET CODE**

Thanks much,
Michael Smith

Replies

Replied 09 Mar 2007 17:55:49
09 Mar 2007 17:55:49 Alan C replied:
HI

I had to do a form recently and decided to try Forms to go - shareware, that generates checking code for forms.

It worked fine just needed some thinking about, then I wanted something like your requirement, I wanted to send an automatic confirmation to the person who completed the form. I looked at the code the Forms to go had generated and found that it was very easy to understand and modify.

It uses the mail function in php - like

mail($emailTo, $emailSubject, $emailBody, $emailHeader);

so that would be one way forward that you could try.

Something I have to be wary of on my server is that there is a restriction of 100 emails sent per hour from any domain.
Replied 09 Mar 2007 19:17:57
09 Mar 2007 19:17:57 Michael Smith replied:
Alan,

I'll look into that, thanks for your reply. 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.


&lt;?php require_once('Connections/baseball.php'); ?&gt;
&lt;?php require_once "swift/Swift.php"; ?&gt;
&lt;?php require_once "swift/Swift/Connection/SMTP.php"; ?&gt;

&lt;?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);
?&gt;
&lt;?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());
}
?&gt;

&lt;?php
mysql_free_result($rsenews);

mysql_free_result($rssubscribers);
?&gt;
&lt;?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-&gt;addTo(" ", "Michael Smith";
$recipients-&gt;addTo("@hotmail.com", "Lynn ";
$recipients-&gt;addTo("@aol.com", "Kathie ";
$swift-&gt;batchSend($message, $recipients, new Swift_Address(" ", "Calb.org");
?&gt;

Thanks,
Michael Smith

Replied 12 Mar 2007 19:17:37
12 Mar 2007 19:17:37 Alan C replied:
swiftmailer looks interesting - I'll take a look at that when I've more tme <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

those last few lines look ok, although I'm not just sure about the instantiation lines with the & in them, I would have expected to see something like this

/* Initialize mailer object */
$CSS = new CSS;


but if the instantiation didn't work you would get all sorts of errors at runtime.

I would stick a couple of echo lines in around the code to see just what was going on . . .

<pre id=code><font face=courier size=2 id=code> $recipients =& new Swift_RecipientList();

echo 'Instantiated $recipients'.'&lt;br /&gt;';

$recipients-&gt;addTo(" ", "Michael Smith";

echo 'Added michael to recipients'.'&lt;br /&gt;';

$recipients-&gt;addTo("@hotmail.com", "Lynn ";

$recipients-&gt;addTo("@aol.com", "Kathie ";

echo 'Starting batch send'.'&lt;br /&gt;';

$swift-&gt;batchSend($message, $recipients, new Swift_Address(" ", "Calb.org");</font id=code></pre id=code>

There is supposed to be a break tag at the end of that echo line, but I think it's getting interpreted literally <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

or something like that and maybe then another in the object itself, although that will probably give you other error when you run it.

That should give you more info about just what is happening.

Reply to this topic