469,328 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How to send multiple emails at once using php mail?

106 100+
I want to send multiple emails from php with one unique script.
The emails are from a mysql database table.

How can I achieve this?
Aug 9 '10 #1

✓ answered by agbb0cg

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //connect to database
  4.  
  5. $query = sprintf("SELECT * FROM mail");
  6.  
  7. $result = mysql_query($query);
  8.  
  9. $count = 1;
  10. while ($row = mysql_fetch_assoc($result)) {
  11.     $email = $row['Email'];
  12.     mail($email, 'My Subject', $message);
  13.     if ($count % 5 == 0) {
  14.       sleep(5); // this will wait 5 secs every 5 emails sent, and then continue the while loop
  15.     }
  16.     $count++;
  17. }
  18. ?>

11 16989
Atli
5,058 Expert 4TB
By using the mail() function.

All the information you could ever want on this subject is only a Google search away.
Aug 9 '10 #2
londres9b
106 100+
I appreciate your help but either you didn't read what I posted or you ignored it..

I know how to use mail() function, I know how to send to multiple email addresses using mail() function.

But you didn't read the most important part - and it was in bold! -

The emails are from a mysql database table.


I need to retrieve the emails from the mysql table and then use the mail() function to send the emails.

How to do this?

P.S. Maybe should be on MySql topic...
Aug 9 '10 #3
Atli
5,058 Expert 4TB
Retrieving the addresses from the MySQL database is fairly simple. You just need to build a SQL query that will select them all (a simple SELECT query should do, but I don't really know you database well enough to say that for sure), and use the mysql_query function to execute it. Then you just loop through the results and send an email for each one. (The manual has excellent examples that show how that works.)

And as you already know how to send an email, this should be fairly straight forward. If you run into any problems, post your code here and we will try to help solve them.

P.S.
If you are new to database interaction in PHP, it may be a good idea to read through a couple of tutorials. PHP and MySQL is probably one of the most written about software combo on the internet, so you should have no trouble finding something you like :)
Aug 9 '10 #4
londres9b
106 100+
I know how to retrieve the emails but I don't know how to loop through them and email each one.. I've searched but haven't find nothing on this.
Aug 10 '10 #5
Atli
5,058 Expert 4TB
Look at example #2 in the mysql_query entry in the manual. The lines inside the while loop is where you would put your mail() code.

If you change the SQL query in that example so that it gives you a list of email addresses, each iteration of the while loop will give you a new email address. You simply put the email code inside the loop and use the email address the loop provides.

Try it. If it doesn't work, post what you tried here and we will guide you through it.
Aug 10 '10 #6
you should also sleep() the while for a couple of secs every 5/10 emails sent... ;)
Aug 10 '10 #7
Dormilich
8,651 Expert Mod 8TB
if youíre out for some more advanced code, some mail libraries also suport batch sending of emails (including the DB queries to get the data).
Aug 12 '10 #8
londres9b
106 100+
Thanks! I got it!

@agbb0cg

I've looked at sleep() but how can I implement it on my script?
Aug 13 '10 #9
londres9b
106 100+
Here's my code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //connect to database
  4.  
  5. $query = sprintf("SELECT * FROM mail");
  6.  
  7. $result = mysql_query($query);
  8.  
  9. while ($row = mysql_fetch_assoc($result)) {
  10.     $email = $row['Email'];
  11.     mail($email, 'My Subject', $message);
  12. }
  13. ?>
Aug 13 '10 #10
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //connect to database
  4.  
  5. $query = sprintf("SELECT * FROM mail");
  6.  
  7. $result = mysql_query($query);
  8.  
  9. $count = 1;
  10. while ($row = mysql_fetch_assoc($result)) {
  11.     $email = $row['Email'];
  12.     mail($email, 'My Subject', $message);
  13.     if ($count % 5 == 0) {
  14.       sleep(5); // this will wait 5 secs every 5 emails sent, and then continue the while loop
  15.     }
  16.     $count++;
  17. }
  18. ?>
Aug 13 '10 #11
londres9b
106 100+
@agbb0cg
thanks ! :)


@dormilich
No it's just something simple, but thanks :D


@Atli
Thanks for your help :)
Aug 14 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by crazycooter | last post: by
2 posts views Thread by palanisiva | last post: by
1 post views Thread by swetha123 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.