Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How to use php to send multiple emails at a time

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default How to use php to send multiple emails at a time

    On a website users can select categories of what information they want to receive from the various members, ie. if they select category 'Flowers', their request is emailed only to members in the Flowers category who can then respond directly to the user.

    I know how to:

    Code:
    SELECT email FROM members WHERE category = 'Flowers';
    but what is the best way to mail an email message using php to all the email addresses that are listed in the result?

    And what if they select more than one category?

    Thanks! e
    Last edited by kuau; 06-25-2009 at 12:13 AM. Reason: syntax

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Something like this perhaps? Change $_POST to $_GET if needed.

    PHP Code:
    <?php
     
    include('db_connect.php'); //connect to your db

     
    if (isset($_POST['categories'])) { //make the checkmarks with name categories[]
      
    foreach ($_POST['categories'] as $cat) {
       
    $email_query mysql_query("SELECT email FROM members WHERE `category` = '" $cat "'");

       if (
    mysql_num_rows($email_query)) {
        while (
    $email mysql_fetch_array($email_query)) {
          
    mail($email'Subject','Message Goes Here'"From: <noreply@domain.com>");
        }
       }
      }
     }
    ?>
    This is just a basic script for you to learn from. Let me know if you need any more help with this.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (06-25-2009)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear TestingSite: It's great to hear from you!! I was wondering how you were doing. I have to get some sleep right now but as soon as I wake up I'll make an attempt at this.

    One question that occurs is how do I send myself a copy of the email only once?

    Thanks for the help and I'm so glad you are still on the scene.

  5. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Just add the mail function just outside the foreach loop (before the last } ).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (09-21-2009)

  7. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Sorry for the long delay but a million things happened in between and I am just able to get back to it now.

    Here is the code that displays the categories in the form:
    Code:
    <input type="checkbox" name="categories[]" value="'.$row['cat_id'].'">' .$row['category']
    Here is the code adapted from your code above:
    Code:
    $subject2 = "Maui Wedding Club Information Request"; 
    $headers2 = "From: Maui Wedding Club <noreply@mauiweddingclub.com>\n";
    
    include_once('db-connect.php'); 
    
    if (isset($_POST['categories'])) { //make the checkmarks with name categories[]
    	foreach ($_POST['categories'] as $cat) {
    	  $email_query = mysql_query("SELECT email FROM members WHERE member_id IN(SELECT member_id FROM membercat WHERE `cat_id` = '" . $cat . "'"));
    	
    	  if (mysql_num_rows($email_query)) {
    		  while ($to_member = mysql_fetch_array($email_query)) {
    			  mail($to_member, $subject2, $body2, $headers2);
    		  }//end while
    	  }//endif
    	}//end foreach
    	mail(info@mauiweddingclub.com, $subject2, $body2, $headers2);
    }//endif
    1) Is there a way to test the mailing without sending out any messages but still showing me where the messages would have been sent and what they would look like?

    2) I suspect there will be a problem with the value of $cat which needs to be equal to the cat_id (an integer) but it looks as if it would be cat_id joined with category name. The membercat table has only 2 fields: member_id and cat_id so category description is not in that table.

    3) When you click Reply to a noreply@domain.com address, does it always let you know immediately that the message cannot go? I find with forms, people often Reply instead of clicking on the client's email link.

    As soon as I get this form working I can launch the site, so I am very eager. Thanks for any assistance. Mahalo!

  8. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You could just echo all of the inputted variables, wouldn't that work?
    That way, it shows what the email would've looked like.

    PHP Code:
    echo $body2
    - Josh

  9. The Following User Says Thank You to JShor For This Useful Post:

    kuau (09-22-2009)

  10. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In general, PHP emails are like regular emails-- you can send it to as many people as you want using the standard format (I think it's just a comma, but look that up to be sure).
    So, basically all you need to do is gather a link however you'd like and then put it in the mail() function. You can use cc and bcc as well, if you'd like. The documentation is all at php.net.

    For what you want specifically, though, you'll find explode() and implode() useful (for getting the information out of the database into an array, then back into a list form separated by commas), and you can just use arrays-- get as many lists as arrays as you want, then merge the arrays and you can sort/delete duplicates easily using the basic array functions (see php.net for that, too).

    If you've already got what you need from above, then that's fine-- if not, here's some more to give an idea at how you can approach things in general.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (09-22-2009)

  12. #8
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear JShor:

    You're right, but I was able to see what it looked like anyway because I was able to get it to send to me even though it wouldn't send to anyone from a category. So I have the email looking the way I want now. But these lines don't work..

    Code:
    if (mysql_num_rows($email_query)) {
    		  while ($to_member = mysql_fetch_array($email_query)) {
    			  mail($to_member, $subject2, $body2, $headers2);
    		  }//end while
    	  }//endif
    I've never seen msysql_num_rows used like that... is that a proper command?

    Back to the drawing board.

  13. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear Daniel:

    I like the approach you are suggesting. I'm heading to php.net now to read up on implode/explode. I have a few gaps in my understanding about arrays and loops. I'm embarrassed to be labeled 'Senior Coder' when I'm just an amateur who asks a lot of questions. Thanks for your help. e

  14. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Not a problem. Arrays and loops can get confusing (especially when you're doing a lot of layers on a complex project), but implode and explode are very easy. The only trouble with them is that they don't always work for everything you need to do-- but if they do it's a fast and efficient way to process lists. But also don't be afraid to get into arrays and loops at some point because they are very useful and once you understand how they work it will open a lot of possibilities for you.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •