473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 17691
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 once a month. So assuming that I have the...
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 have one query that pulls the names and the...
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() +duplicate&rnum=7#ea9bf92a9c6b807c), but there didnt...
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 ? Thanks Serena
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. The first email $to but i add two mail id mail not...
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 Stationery. But, a simple way to send your own HTML...
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 addresses, but I can't seem to get it to work. One...
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? and how to limit the file size to 3mb? $email =...
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 me with the VBA code please.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.