Bulk email code needed

Hi all,
I a table I have a list of email addresses which I
Need to sent. I basically need some code that
Will connect to the db and email to all the addresses
In this table and as they are sent I need these email addresses to be deleted.

Hope someone can help

Thanks
error77Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
trencH87Connect With a Mentor Commented:
First of all: Why are you running a foreach() within your while() loop ?

Check if you are getting any errors with this:
<?php

$con = mysql_connect("localhost","root","");
if (!$con) { 
	die('Could not connect: ' . mysql_error()); 
}

mysql_select_db("test_db", $con);


$result = mysql_query("SELECT * FROM `keys`") or die("Query failed: ".mysql_error());
while($row = mysql_fetch_array($result))  // <--- Errors here
{
	$email = $row['email'];

	$to  = $row['email'];
                  
	// subject
	$subject = 'Subject';

	// message
	$message = 'Your message';

	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	// Additional headers
	$headers .= 'To:  <'.$email.'>' . "\r\n";
	$headers .= 'From: YOU <your@emailadress.com>' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);
}

?>

Open in new window

0
 
ProgressedCommented:
how large is this list? and what are you going to e-mail?

You have to be carefull with timeouts...
0
 
error77Author Commented:
The list could be 10 or 1000 and the content will be a title and body text from another table if possible. No attachments needed, just text.

Hope this helps
Thanks
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ProgressedCommented:
i'd really think you should use software to bulkmail... large lists cannot be send via php because of the timeout settings of php.
0
 
error77Author Commented:
I'm just going to be sending 50 or so at a time right now so I just want some working code as it's just for temp use.
0
 
ProgressedCommented:
In that case the "I told you so" factor will get in affect when it happens to you! :D
I started typing and this is what i came up with... i cant test it so you should debug where necassery!
PS... dont forget to make a connection to you database :)

<?php

$result = mysql_query("SELECT *, email FROM table");
while($row = mysql_fetch_array($result))
  {
  $e-mail = $row['email'];
  $name = $row['sirname'];

		foreach ($email as $key => $error) {

			$to  = '$email';
			
			// subject
			$subject = 'your subjectline';

			// message
			$message = '
				<html>
					<head>
						<title>this is a title</title>
					</head>
				<body>
				<p>Dear ".$name.",</p>
				<p> This is just normal HTML... should be to hard... you could use tables or whatever</p>

				<table>
					<tr>
						<th>email</th><th>sirname</th>
					</tr>
					<tr>
						<td>".$email."</td><td>".$name."</td>
					</tr>
				</table>
			</body>
			</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: $sirname <$email>' . "\r\n";
$headers .= 'From: YOU <your@emailadress.com>' . "\r\n";
//-- Optional CC and or BCC
// $headers .= 'Cc: email@adress.com' . "\r\n";
// $headers .= 'Bcc: email@adress.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

mysql_query("DELETE FROM table WHERE email = $email");
	}
}
?>

Open in new window

0
 
error77Author Commented:
getting: error bool(false)  on var_dump line $result = mysql_query("SELECT email FROM mytable");

any ideas pls?
0
 
Ray PaseurCommented:
When mysql_query returns FALSE, inspect the contents of mysql_error()
0
 
error77Author Commented:
I can't call a table 'keys' ?
0
 
error77Author Commented:
Hi Progressed,

Could you include the connection code too please... I can't seem to get over the errors :o/

0
 
ProgressedCommented:
dont forget to put it inside the <?php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

Open in new window

0
 
error77Author Commented:
same error: mysql_fetch_array() expects parameter 1 to be resource, boolean given :o/
0
 
error77Author Commented:
Something wrong here?

$result = mysql_query("SELECT *, email FROM keys");
while($row = mysql_fetch_array($result))

0
 
ProgressedCommented:
Offcourse you can delete ,email from that line... i gave it as example


$result = mysql_query("SELECT * FROM keys");

Post your code, because i dont see any errors in mine.

0
 
error77Author Commented:
Sure:

Here is all the code:



<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test_db", $con);



//************************************************




$result = mysql_query("SELECT * FROM keys");
while($row = mysql_fetch_array($result))  // <--- Errors here
  {
  $email = $row['email'];
  //$name = $row['sirname'];

            foreach ($email as $key => $error) {

                  $to  = '$email';
                  
                  // subject
                  $subject = 'your subjectline';

                  // message
                  $message = '
                        <html>
                              <head>
                                    <title>this is a title</title>
                              </head>
                        <body>
                        <p>Dear ".$name.",</p>
                        <p> This is just normal HTML... should be to hard... you could use tables or whatever</p>

                        <table>
                              <tr>
                                    <th>email</th>
                                    <!--<th>sirname</th>-->
                              </tr>
                              <tr>
                                    <td>".$email."</td>
                                    <!--<td>".$name."</td>-->
                              </tr>
                        </table>
                  </body>
                  </html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:  <$email>' . "\r\n";
$headers .= 'From: YOU <your@emailadress.com>' . "\r\n";
//-- Optional CC and or BCC
// $headers .= 'Cc: email@adress.com' . "\r\n";
// $headers .= 'Bcc: email@adress.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

//mysql_query("DELETE FROM table WHERE email = $email"); <--Taken off for now
      }
}


?>

Table is called 'keys' Table has more fields btw but don't need them for reading and emailing. ~Just need the email field.

thanks


0
 
ProgressedCommented:
need more info please replace this:
is the field call email? remember.. the fields are CASE SENSITIVE!
$result = mysql_query("SELECT * FROM keys");

to:
$result = mysql_query("SELECT * FROM keys") or die("MySQL ERROR: ".mysql_error());
$result = mysql_query("SELECT * FROM keys") or die("MySQL ERROR: ".mysql_error()); 

Open in new window

0
 
ProgressedCommented:
and you forgot to // the Dear $name row.
0
 
error77Author Commented:
OK, this is the error I get back ... for the code I just changed:

MySQL ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys' at line 1
0
 
error77Author Commented:
And here is the table data structure:

-- --------------------------------------------------------

--
-- Table structure for table `keys`
--

CREATE TABLE IF NOT EXISTS `keys` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` varchar(100) NOT NULL,
  `email` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;



0
 
Ray PaseurCommented:
Here is a teaching example showing how to do some of the basics in MySQL.  Note the error handling at line8, et seq.  You may want to adapt your code to do something like this so you can see the fully resolved query string (including the dynamic data values) if you hit an error.
// CREATING AND SENDING A SELECT QUERY AND TESTING THE RESULTS
// MAN PAGE:http://us2.php.net/manual/en/function.mysql-query.php
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
// MAN PAGE: http://us.php.net/manual/en/function.mysql-error.php
if (!$res)
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>QUERY FAIL: ";
   echo "<br/>$sql <br/>";
   die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS

Open in new window

0
 
error77Author Commented:
Thanks!
0
 
error77Author Commented:
Hi Progressed, I accepted yours and trencH87 as solutions but I saved and only trencH87's was saved :o/  Sorry about this...there's a problem with the site?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.