Solved

Bulk email code needed

Posted on 2010-09-16
22
297 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
 

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 108

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now