Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

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

  1. #11
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Not a problem for you maybe, but I have spent hours on this already and have gone from getting the first email address only to getting a blank page. I am retrogressing!

    Is there an example of how to do this somewhere? I am not bad at modifying code when I see how it is supposed to be done, but I am not good at blazing trail. Today is my birthday... can someone please give me a break and show me explicitly how to do this? It's only a couple of lines of code I am having trouble with. Please! Mahalo, e

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

    Default

    Happy B-day and sorry I have not been able to post a response to this, but I have been extremely busy with 2 full time jobs, going to school, some freelance work, and other things that have just taken up my time completely. What exactly are you trying to accomplish with this? Just seeing where the emails will be going or what? I've modified the code a little, so see if this works for you.

    Code:
    if (mysql_num_rows($email_query)) {
    		  while ($to_member = mysql_fetch_array($email_query)) {
    			  mail($to_member['email'], $subject2, $body2, $headers2);
                              //echo $to_member['email'].'<br>'; /* DEBUGGING */
                            /*Uncomment the above and comment out the mail function to debug code */
    		  }//end while
    	  }//endif
    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 (09-23-2009)

  4. #13
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear thetestingsite:

    Thanks a million!! That worked. I had been using print_r($email_query); to try to debug and it listed only one email address so I thought it wasn't working. Not sure if it was or not. It makes me crazy that one little word can consume so many hours of my time getting nowhere. Thanks for putting me out of my misery ... the best birthday present I can imagine at this point!

    I did make the site live but don't feel 100% confident that the form is working as intended. A new mystery is that the emails that get sent have the "From: " address as the hosting account username@server#.hostname.com which is a considerable security risk. I have the "From:" defined in the headers but it is not using it. This seems to be a default return address. Any idea why this would be happening and how to fix it?

    Sorry to take up your time when you are so busy, but I love you for helping. e
    Last edited by kuau; 09-23-2009 at 03:01 PM. Reason: update

  5. #14
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Happy belated birthday.

    The explode() function is very easy to use, it splits up the string, as long as you have been consistent with your separator, and creates an array with the string split into the various elements.

    Here is a link to page that explains it:

    http://www.toknowmore.net/e/1/php/ph...e-function.php

    there is also an explanation of implode():

    http://www.toknowmore.net/e/1/php/ph...e-function.php

    Both of these links have examples that you can modify

    Salut

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

    kuau (09-23-2009)

  7. #15
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Thanks for the links. So is this correct? ...as Daniel suggested above, instead of using a while loop, I could take the array of email addresses and use implode to create one long list separated by commas and then use just one mail command?

    Code:
    $new_array=array($email_array);
    $newstring=implode(",",$new_array);
    mail($newstring, $subject2, $body2, $headers2);
    Which method is the most economical use of the server resources? And is there any downside to having an indeterminate number of email addresses in the "To:" field? ie. is there a built-in php limit for one command other than a probable limit on number of emails per hour by the host?

    Even though it may already be working, I'd like to learn the best practices way of solving this problem so that the next time I have to do something similar, I can build on it. Has anyone tried both ways and come up with a determination? Thanks.

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

    Default

    PHP emails are configured on your server, sometimes beyond what you can change in the PHP code itself-- your host may have set it up to automatically use that as the from email, in which case there isn't much you can do. I'd look at their FAQs to see if they explain a bit more. That will vary by host, and there may be some workarounds, like perhaps looking into php.ini (the configuration file for PHP), because it might be a default in that-- IF you have access to the file, and be careful because that could seriously change settings you don't want to mess with.

    As for implode/explode, you're on the right track, but here's a basic example you can work from:
    PHP Code:
    $emails "1@1.1, 2@2.2, 3@3.3, ...";
    $emails explode (', ',$emails); //note the space after the comma
    sort($emails); //arranges alphabetically-- you can do other things, but just an example
    $emails implode(', ',$emails);
    mail($emails,.....); 
    What you have above is a little confusing, maybe because there is no code before it.
    From what I can tell, though, it looks like you are using too many layers of an array--
    You are putting the $array within array($array), on your first line.
    Remember that arrays are basically lists, and that you are making a list of a list, so now you have a one item array consisting of an array. That list item then has a list inside it, but you don't need to have it so embedded.
    Once something is an array, it's just like a normal variable.

    PHP Code:
    $ar = array(1,2,3);
    $ar2 $ar//not array($ar);
    $ar==$ar2//TRUE 

    So: $new_array=array($email_array);
    should be: $new_array=$email_array;

    Or, you may not need "new_array" at all.

    Hope this helps.
    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

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

    kuau (09-25-2009)

  10. #17
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Daniel: I was able to solve the problem of the From: address by creating a forwarder BUT ever since I did that, my code stopped working. It shouldn't have anything to do with it but now it doesn't send out any emails. I successfully used implode to create a list of the categories but now the mail function doesn't work. So I have tried the method you suggested. Does this look right? Not sure why you have to explode them implode but...

    Code:
    $emails = mysql_query("SELECT email FROM members WHERE `cat_id` = '".$cat."') ");		  
      if (mysql_num_rows($emails)) {
        $emails = explode (', ',$emails); 
        $emails_nodups = array_unique($emails);
        $emails_nodups = implode(', ',$emails_nodups);
        mail($emails_nodups, $subject2, $body2, $headers2);  			
      }//endif
    Thanks for your help. e

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

    Default

    implode(explode(X)) == X, so you do not need to use them.
    However, using them gives you an array in the middle and that way you can use the array_unique function, which is what you wanted before.

    The code looks fine, assuming $subject2, $body2, $headers2 are all ok.

    You could test what values they are getting by changing "mail(" to "print_r(", and you'll print out all the data send through the mail function to see what's actually being sent.
    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

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

    kuau (09-26-2009)

  13. #19
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear D: I got this error message when trying to test with print_r...

    Code:
    Warning: print_r() expects at most 2 parameters, 4 given...
    Then I removed all but $emails and got this message...

    Code:
    Resource id #3Resource id #4Resource id #5Resource id #6
    Then I tried making a while loop with an echo command and got this...

    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
    I have The Testingsite's method now working perfectly (thanks D!), but I wanted to compare this method. I just can't get it to list the array so I can test it. Is there any difference in efficiency between sending the emails one by one with a loop compared to sending to all the addresses at once using one mail command?

    Thanks for your help. I always like to learn the best way. e
    Last edited by kuau; 09-26-2009 at 08:20 PM. Reason: added info

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

    Default

    It's more efficient to send them all at once, but it's not going to crash the server or anything.

    Sorry about that-- you do just need one input for print_r.... I forgot about that. However, yes, just use $emails.

    I'm not sure why you are getting "resource_id...". That is what happens when you reference a mysql query result directly, rather than properly extracting its contents. In other words, you need to do a while(mysql_fetch... loop first, getting all of the emails ready, then do the code above. I'm not exactly sure where that went wrong in your code, but it definitely explains why it's not working to send anything. But the email code itself is probably fine-- the data just needs a bit of preparation first.
    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
  •