Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Bulk email code needed

Posted on 2010-09-16
22
301 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:error77
  • 12
  • 7
  • 2
  • +1
22 Comments
 
LVL 3

Expert Comment

by:Progressed
ID: 33689542
how large is this list? and what are you going to e-mail?

You have to be carefull with timeouts...
0
 

Author Comment

by:error77
ID: 33689583
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
 
LVL 3

Expert Comment

by:Progressed
ID: 33689751
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:error77
ID: 33689797
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
 
LVL 3

Expert Comment

by:Progressed
ID: 33689939
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
 

Author Comment

by:error77
ID: 33690151
getting: error bool(false)  on var_dump line $result = mysql_query("SELECT email FROM mytable");

any ideas pls?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33690227
When mysql_query returns FALSE, inspect the contents of mysql_error()
0
 

Author Comment

by:error77
ID: 33690299
I can't call a table 'keys' ?
0
 

Author Comment

by:error77
ID: 33690333
Hi Progressed,

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

0
 
LVL 3

Expert Comment

by:Progressed
ID: 33690354
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
 

Author Comment

by:error77
ID: 33690419
same error: mysql_fetch_array() expects parameter 1 to be resource, boolean given :o/
0
 

Author Comment

by:error77
ID: 33690427
Something wrong here?

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

0
 
LVL 3

Expert Comment

by:Progressed
ID: 33690502
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
 

Author Comment

by:error77
ID: 33690547
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
 
LVL 3

Expert Comment

by:Progressed
ID: 33690758
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
 
LVL 3

Expert Comment

by:Progressed
ID: 33690766
and you forgot to // the Dear $name row.
0
 

Author Comment

by:error77
ID: 33690874
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
 

Author Comment

by:error77
ID: 33690897
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
 
LVL 4

Accepted Solution

by:
trencH87 earned 500 total points
ID: 33691412
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33691688
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
 

Author Closing Comment

by:error77
ID: 33696430
Thanks!
0
 

Author Comment

by:error77
ID: 33696446
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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question