Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Mail: Multiple tos

    How could you use the mail function to send to more than one to, for the purpose of this, let's use this example from the PHP Manual:
    PHP Code:
    <?PHP
    mail
    ("nobody@example.com""the subject"$message,
         
    "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
         
    "X-Mailer: PHP/" phpversion());
    ?>
    Last edited by gsnedders; 06-03-2004 at 08:01 PM.

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Code:
    function mailStuff($email) {
    mail($email, "the subject", $message,
         "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" .
         "X-Mailer: PHP/" . phpversion()); 
    }
    Then somewhere else...
    Code:
    $addresses = array("john@dot.com","doe@john.com","mary@smith.com");
    for($i=0;$i<count($addresses);$i++) {
         mailStuff($addresses[$i]);
    }

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You don't need to repeatedly invoke the mail command for each recipient. You just need to seperate the addresses with a comma just like it shows in the PHP manual

    PHP Code:

    "To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n"
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by Spookster
    You don't need to repeatedly invoke the mail command for each recipient. You just need to seperate the addresses with a comma just like it shows in the PHP manual

    PHP Code:

    "To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n"
    Hail Spookster

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    And you can still use some of bcarl's suggestion by defining the addresses elsewhere and storing them in just a string variable instead of an array.

    PHP Code:

    $addresses 
    "To: Mary <mary@example.com>, Kelly <kelly@example.com>\r\n";

    mail($addresses"the subject"$message
         
    "From: webmaster@{$_SERVER['SERVER_NAME']}\r\n" 
         
    "X-Mailer: PHP/" phpversion()); 
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    OK, I'm having problems with:
    PHP Code:
    <?php
    /* recipients */
    $db 'geoffers_newsletter';
    $db_user 'dunnomy';
    $db_pass 'dunnomypass';

     
    mysql_connect(localhost,$db_user,$db_pass);
     @
    mysql_select_db($db) or die( "Unable to select database");
     
    $query="SELECT email FROM email";
     
    $result=mysql_query($query);

     
    $num=mysql_numrows($result);

     
    mysql_close();

     
    $i=0;
     while (
    $i $num) {

     
    $to=mysql_result($result,$i,"email");

     ++
    $i;
     }

    /* subject */
    $subject 'test';

    /* message */
    $message 'test';

    /* additional headers */
    $headers .= "From: Geoffrey Sneddon <geoffers@geoffers.uni.cc>\r\n";

    /* and now mail it */
    mail($to$subject$message$headers);
    ?>

  • #7
    New Coder
    Join Date
    May 2004
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shouldnt...

    PHP Code:
    $num=mysql_numrows($result); 
    ... be ...

    PHP Code:
    $num=mysql_num_rows($result); 
    "The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."

    --Tim Berners-Lee, W3C Director and Inventor of the World Wide Web

  • #8
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    The reason I would call a function and mail individually is so the recipients do not get all the other email addresses. Some people get freaky about that.

    But both methods work just fine.

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Id have to agree with bcarl, becuase I wouldnt want many people getting my email address...

    However, I also agree that it is not always suitable to make multiple calls to mail(), which would take a long time.

    Going more advanced now, you can get hold of SMTP mail scripts... Its much more complex but for large mail sending is much faster. But for sending 2 or 3 emails on a page, shouldnt be required.

  • #10
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Missing...

    I've actually been looking for something like that, can you point me to some resources?

    B

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe this will be of some use (I haven't had a look over properly):

    http://www.hotscripts.com/cgi-bin/se...mail&catid=all

  • #12
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    How could I put in a , if there was another address?

  • #13
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    to send all the mail in 1 go without sharing the email address could you not just send the mail as a BCC because then you cannot see all the other mail addresses it has been sent to

    not sure how you would do it with the mail feature though - anyone, does it work?

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sending 1 mail with all receivers in bcc is the way to go.

    mail() will open a new socket + close it for eacht iterantion if you send them inside a loop, so it will be 'rather slow'. From the crashtests i did a while back, i found that you can easely send out 500 mails before your script times out, but that the time it took to get a chunk of 15 mails out drastically increased with each chunk (i sent out 30 chunks of 15 mails and included the resuls of a timerfunction in the message.)

    to send them as bcc's, all you need to do is add them in the header-atribute as shown in example 4 of http://www.php.net/function.mail
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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