472,352 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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 17513
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,658 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

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

Similar topics

17
by: rdemyan | last post by:
My app creates a building report. My users have requested that I provide functionality to e-mail these "building reports" to building managers...
1
by: Jeremy | last post by:
I have a table with user names and their balance. I am going to say there are 1000 people that need to get an email of their balance statement. I...
10
by: crazycooter | last post by:
I found an old thread on this (http://groups.google.com/group/alt.php/ browse_thread/thread/751edb9c723316c4/ea9bf92a9c6b807c?lnk=gst&q=mail()...
5
by: serena9131 | last post by:
Dear all, I having problem for sending subject in cheses language using mail(), Can anyone let me know how to send send non unicode subject ? ...
2
by: palanisiva | last post by:
Hi, I am using PHP mailer to send out 2 different emails. The problem I am having is only the first email is ever sent out. Below is my code....
3
digicrowd
by: digicrowd | last post by:
http://bytes.com/images/howtos/applemail_sig_icon.jpg You can make your emails fancy in Mail.app by using Rich Text formatting or even included...
5
by: wktarin | last post by:
Hi. I'm a relative newcomer to the world of php, and I keep bumping into a problem with a mail () form. I need to send an automatic email to two...
1
by: swetha123 | last post by:
hello, can any tell me how to send mails from site using mail() in php do we need to change in the php.ini these are in php.ini
1
by: perhapscwk | last post by:
Below code works however it seems it will upload and save the attachment to server, how can send attachment without save the file into my server?...
0
by: rserrador | last post by:
Hello. I'm creating a db in access 2007 and i want to have a buton on a report where i can send bcc multiple emails from a query. Can someone help...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.