How to send multiple emails at once using php mail?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    How to send multiple emails at once using php mail?

    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?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    By using the mail() function.

    All the information you could ever want on this subject is only a Google search away.

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      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...

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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 :)

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          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.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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.

            Comment

            • agbb0cg
              New Member
              • Jul 2010
              • 8

              #7
              you should also sleep() the while for a couple of secs every 5/10 emails sent... ;)

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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).

                Comment

                • londres9b
                  New Member
                  • Apr 2010
                  • 106

                  #9
                  Thanks! I got it!

                  @agbb0cg

                  I've looked at sleep() but how can I implement it on my script?

                  Comment

                  • londres9b
                    New Member
                    • Apr 2010
                    • 106

                    #10
                    Here's my code:

                    Code:
                    <?php
                    
                    //connect to database
                    
                    $query = sprintf("SELECT * FROM mail");
                    
                    $result = mysql_query($query);
                    
                    while ($row = mysql_fetch_assoc($result)) {
                        $email = $row['Email'];
                        mail($email, 'My Subject', $message);
                    }
                    ?>

                    Comment

                    • agbb0cg
                      New Member
                      • Jul 2010
                      • 8

                      #11
                      Code:
                      <?php
                       
                      //connect to database
                       
                      $query = sprintf("SELECT * FROM mail");
                       
                      $result = mysql_query($query);
                       
                      $count = 1;
                      while ($row = mysql_fetch_assoc($result)) {
                          $email = $row['Email'];
                          mail($email, 'My Subject', $message);
                          if ($count % 5 == 0) {
                            sleep(5); // this will wait 5 secs every 5 emails sent, and then continue the while loop
                          }
                          $count++;
                      }
                      ?>

                      Comment

                      • londres9b
                        New Member
                        • Apr 2010
                        • 106

                        #12
                        Originally posted by agbb0cg
                        .
                        thanks ! :)


                        Originally posted by dormilich
                        .
                        No it's just something simple, but thanks :D


                        Originally posted by Atli
                        .
                        Thanks for your help :)

                        Comment

                        Working...