Page 1 of 1

form not submitting to MySQL list

Posted: Sat Nov 12, 2005 5:01 pm
by bruceg
I am trying to set up a form which emails to a list of people who have been entered into a MYSQL database. Everything works perfect as far as being registered into the database except when I send out a sample form to members in that database (right now me), it doesn't want to send. (Don't get an email even though no errors occur and I am sent to the confirmation screen.

the code for so send to the list of emails is:

Code: Select all

<?php
// this script is used to as the action for the form on sendmailform.php
// it sends the email to all persons who have subscribed to the mailinglist and confirmed their subscription

//include the config file
include("config.php");

$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

//Variables for the headers
// customize this stuff
$sender = "Bruce Gilbert <webguync@gmail.com>\n"; //put your name and sending address here
$reply_to = "<webguync@gmail.com>\n"; //reply-to, insert your address here,  might not be supported by your server
$return_path = "webguync@gmail.com\n"; // return-path, if you have one, also might not be supported by your server
$x_sender = "<webguync@gmail.com>\n"; //your address, another setting possibly not supported by your server

$message .= "\n\n This is a double opt-in mailing list. All recipients have confirmed their subscription. If you no longer wish to receive these emails, please go to http://$list_owner_domain_name \n
					Get this custom mailing list system for your site. Go to http://www.karlcore.com for more info! \n";

// this selects the table and orders the results by the name
// it only selects the listings that have been confirmed
$query = "
	SELECT 
		* 
	FROM 
		mailinglist 
	WHERE 
		subscribe=1 
		AND 
		confirmed=1";

$result = mysql_query($query);

while ( $row = mysql_fetch_array($result))
	{
	$rec_id = $row["rec_id"];
	$email = $row["email"];

	$recipient = $email;

	$headers = "From: $sender\r\n";
	$headers .= "Reply-To: $reply_to\r\n";
	$headers .= "Return-Path: $return_path\r\n";
	$headers .= "X-Sender: $x_sender\r\n";
	$headers .= "X-Mailer: PHP4\r\n"; //mailer
	$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
	$headers .= "Mime-Version:1.0\n Content-Type: text/plain; charset=\"iso-8859-1\nContent-Transfer-Encoding: 8bit\n";
		
	mail( $recipient, $subject, stripslashes($message), $headers ); 
	sleep(1);

}

// run second query to automatically dump unsubscribed email addresses.
$query2 = "
	DELETE FROM 
		mailinglist 
	WHERE 
		subscribe='0' 
		AND 
		confirmed='0' ";

//run the query
mysql_query($query2, $link) or die (mysql_error());

mysql_close();

header("location: mailsent.php");
exit;
?>
the form can be found here:

http://www.inspired-evolution.com/sendmailform.php

Posted: Sun Nov 13, 2005 12:33 pm
by twigletmac
Does a simple mail test work on your server?

Code: Select all

<?php

$to      = 'you@yourdomain.com';
$subject = 'test';
$message = 'test';

mail($to, $subject, $message);

?>
Mac

Posted: Sun Nov 13, 2005 4:12 pm
by bruceg
yes, I can get emails sent using a simple form. The problem seems to be in getting the form to interact with the MySQL database and send to the email addresses listed there.

I guess that would ne this part:

Code: Select all

SELECT 
		* 
	FROM 
		mailinglist 
	WHERE 
		subscribe=1 
		AND 
		confirmed=1";

$result = mysql_query($query);

while ( $row = mysql_fetch_array($result))
	{
	$rec_id = $row["rec_id"];
	$email = $row["email"];

Posted: Mon Nov 14, 2005 6:17 am
by twigletmac
How many rows are getting returned from the database? Try:

Code: Select all

$result = mysql_query($query);
echo '<p>Query returns '.mysql_num_rows($query).' rows.</p>';
Mac

Posted: Mon Nov 14, 2005 7:45 pm
by bruceg
hey would that replace all of this

Code: Select all

$result = mysql_query($query); 

while ( $row = mysql_fetch_array($result)) 
    { 
    $rec_id = $row["rec_id"]; 
    $email = $row["email"];
or just the line with :

Code: Select all

$result = mysql_query($query);
thanks!

Posted: Mon Nov 14, 2005 7:47 pm
by Nathaniel
just the line $result = mysql_query($query);

:)

Posted: Mon Nov 14, 2005 7:49 pm
by Burrito
using the while will loop over all of the results it finds.

just creating the associative array for the resource will only pull out one result.

if you want the email to go to mulitple addresses, use the while loop and concat the addresses to an existing string.

ex:

Code: Select all

$emaillist = "";
while($row = mysql_fetch_assoc($result))
{
  $emaillist = $row['email'].",";
}
$emaillist = substr(0,strlen($emailist)-1);