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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php mail() multiple recipients

    Hello all,

    I am having issues trying to write a simple newsletter script, this is what I have so far:

    PHP Code:
    //get the variables
    $subject $_POST['nform_newsltitle'];
    $message $_POST['nform_newsltext'];
    $recipients $_POST['nform_newslrecipients'];
    $email "contact@example.com";

    //if ALL and SPECIFIC users have been selected, go back to the form with an error
    if ($recipients[0] == "ALL" && strlen($recipients[1]) > 1) {

        
    $link 'Location: index.php?page=writenewsletter&newsl_error=1&newstitle='.$title.'&newsltext='.$text;
        
    header($link);

    } else {

                
    //include our db data to make the connection
                
    include ('db.php');
                
                if (
    $recipients[0] == "ALL") {

                        
    $content1 mysql_query("SELECT email FROM users WHERE newsletter = 1");
                
                            while(
    $row mysql_fetch_array($content1))
                                  {
                                   
    $to $row['email'].',';
                                   echo 
    $to;
                                  }
                  
                  } else {
                  
                  }
                  
                  
    mail($to,$subject,$message,"From: ".$email.""); //a very simple send
        

    But it's only sending one email and ignoring the other recipients.

    Any experienced users know what I am doing wrong?

    Thank you!

  • #2
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I seem to have posted this in the wrong subforum. Can a mod move it for me please? Thanks!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You keep overwriting the $to:
    PHP Code:
                            while($row mysql_fetch_array($content1))
                                  {
                                   
    $to $row['email'].',';
                                   echo 
    $to;
                                  } 
    Concat the $to variable using $to .= $row['email'] . ',';. You may want to consider stripping the last ',' as well with rtrim($to, ',');
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks that got it to work.

    Now I have another issue. Using the form that I created it is possible to send either to ALL or to specific recipients.

    If sending to ALL, then the above code works nicely.

    But in the case of select recipients I have not figured out a way to go through the array and select one recipients email at a time, add it to the $to and then continue going through the array until its finished.

    This is what I have been trying, but it's not working:

    PHP Code:
    for ( $i=0$i count($recipients); $i++ ) {

    $prename $recipients[$i];

        
    $content mysql_query("SELECT email FROM users where username=$prename");

    while(
    $row mysql_fetch_array($content))
      {
            
    $to .=$row['email'] . ',';
            
    rtrim($to',');
      }
        
        } 
    Any ideas ?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    PHP Code:
      $selected_addrs = array('me@hotmail.com','you@gmail.com','him@this.com');

      while(
    $row mysql_fetch_array($content1))
                                  {
                                       if(
    in_array($row['email'], $selected_addrs))
                                                {
                                                       
    $to $row['email'].',';
                                                       echo 
    $to;
                                                 }
                                  } 
    Good luck

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    if you have the user id's that you want to send to you can simply edit your query to have
    WHERE `id` IN(1,2,3,4,5)
    where 1,2,3,4,5 are the user ids

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to the both of you, this is the problem I am having though:

    My form has the following:

    <select name="nform_newslrecipients[]">
    <option>$row['username']</option>
    </select>

    So I end up with an Array of usernames in $_GET['recipients']

    So I need a step to get each username, obtain the email for it and add it to $to.

    Or is there an easier method? I have the feeling I am complicating things?

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    there are a few ways to do this,
    personally, I'd prefer to set the option value to a user id then in the mail script run a select to get the username and email.

    alternatively, you could do just a delimiter in the value like:

    PHP Code:
    <option value='Bob|Bob@domain.com'>Bob</option
    then just explode the value in your loop.

  • #9
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by partisanentity View Post
    Thanks to the both of you, this is the problem I am having though:

    My form has the following:

    <select name="nform_newslrecipients[]">
    <option>$row['username']</option>
    </select>

    So I end up with an Array of usernames in $_GET['recipients']

    So I need a step to get each username, obtain the email for it and add it to $to.

    Or is there an easier method? I have the feeling I am complicating things?
    I think you might mean to say that you have an array of results in $recipients = $_POST['nform_newslrecipients'];

    Is the form code you entered the actual code from the page or just a rough estimation? If it's not copy/pasted from your actual page please update it to be the actual code.

    Under normal circumstances select boxes will return only one value (the one the user chose), not every value in the select box. If you are getting an array of results then something is not being done as it should. If your code truly does read as above then I believe your only issue is that the name used for the select element has brackets "[]" behind it. I have never played around with that method, but my understanding is that this indicates to the browser that this element will return multiple values (so the browser returns multiple values).

    Try removing those brackets and see if you get only one entry for the select element.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    pretty sure it's to have an array of elements. Do you have multiple dropdowns of usernames and you select them with those? Checkboxes would definitely be a better choice IMO

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Quote Originally Posted by Rowsdower! View Post

    Try removing those brackets and see if you get only one entry for the select element.
    I think that maybe you've missed the point, he wants the array ( though yes, you can only get one value from a normal select, but he's likely use a multi ), he just needs a way to get both the persons name in email in one shot.

  • #12
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Also, like it's been said above you need to have a value for your selects...

  • #13
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by angst View Post
    I think that maybe you've missed the point, he wants the array ( though yes, you can only get one value from a normal select, but he's likely use a multi ), he just needs a way to get both the persons name in email in one shot.
    Which is some of the source of my confusion. That's why I'm asking for a solid copy/paste from the code. Otherwise, using the brackets without using a multi select will (I believe) return all results in the select. I might be totally wrong about that.

    Then again, as I said, I've never really toyed with this specific issue so I'm half guessing at a lot of it which probably isn't the most productive thing in the world to do.

    Quote Originally Posted by JAY6390 View Post
    Also, like it's been said above you need to have a value for your selects...
    For the <option> tags, actually but yes, values certainly do need to be inserted here.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #14
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes, i agree, current code would be nice.

    also, without the multi, only one value would still be returned. but who knows, maybe he has a few of these single selects on one page. I *believe* that would still return the same result as a multi.

  • #15
    New Coder
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey guys, sorry for the causing the confusion.

    Some background info:

    I wrote a simple newsletter/emailing feature. In the form the user can choose to send to:

    ALL
    or
    select recipients

    the form pulls all usernames from the db who have chosen to receive a newsletter and displays them in a <select multiple="multiple"> box.

    Here is the complete code for the form (i have since added the email as a value for the <option>'s:

    PHP Code:
    <h2>Write Newsletter</h2>
    <?php

    if ($_SESSION['username'] == "admin" && $_SESSION['logged_in'] == 1) {

    //include our db data to make the connection
    include ('db.php');

    if (
    $_SESSION['newsl_error'] == 1) {
        echo 
    'You cannot select <b>ALL</b> and <b>specific</b> users, select either one or the other<br /><br />';
        unset(
    $_SESSION['newsl_error']);
    }

    if (isset(
    $_SESSION['newsl_title']) && strlen($_SESSION['newsl_text']) > 5) {
    $title 'value="'.$_SESSION['newsl_title'].'"';
    $text $_SESSION['newsl_text'];
    unset(
    $_SESSION['newsl_title']);
    unset(
    $_SESSION['newsl_text']);
    }

    //get all usernames who agreed to receive a newsletter
    $content mysql_query("SELECT * FROM users WHERE id>1 AND newsletter=1");

    echo 
    '<form action="newsletterfunc.php" method="post" >';
    echo 
    '<table>';
    echo 
    '    <tr><td>Title: </td><td><input type="text" name="nform_newsltitle"'.$title.'></td><td rowspan="3">';
    echo 
    'Select recipients';
    echo 
    '<select name="nform_newslrecipients[]" style="width: 100%;" size="30" multiple="multiple">';
    echo 
    '<option>ALL</option>';
    while(
    $row mysql_fetch_array($content))
      {
          echo 
    '<option value="'.$row['email'].'">'.$row['username'].'</option>';
      }
    echo 
    '</select>'
    echo 
    '</td></tr>'
    echo 
    '<tr><td>Text: </td><td><textarea name="nform_newsltext" cols=80 rows=30>'.$text.'</textarea></td><td></td></tr>';
    echo 
    '<tr><td></td><td><input type="submit" value="send"></td><td></td></tr>';
    echo 
    '</table>';
    echo 
    '</form>';

    } else {
        echo 
    '<h2>Access denied</h2>';
    }
    ?>
    Here is the complete php code:

    PHP Code:
    <?php
    //get the variables
    $subject $_POST['nform_newsltitle'];
    $message $_POST['nform_newsltext'];
    $recipients $_POST['nform_newslrecipients'];
    $email "contact@alhaffar.com";



    //if ALL and SPECIFIC users have been selected, go back to the form with an error
    if ($recipients[0] == "ALL" && strlen($recipients[1]) > 1) {

        
    $link 'Location: index.php?page=writenewsletter&newsl_error=1&newstitle='.$title.'&newsltext='.$text;
        
    header($link);

    } else {

                
    //include our db data to make the connection
                
    include ('db.php');
                
                if (
    $recipients[0] == "ALL") {

                        
    $content1 mysql_query("SELECT email FROM users WHERE newsletter = 1");
                
                            while(
    $row mysql_fetch_array($content1))
                                  {
                                   
    $to .= $row['email'] . ',';
                                   
    rtrim($to',');
                                  }
                  
                  } else {
                  
                          
    //missing bit for sending mail to the select users only
                  
                  
    }
                  
                  
    mail($to,$subject,$message,"From: ".$email.""); //a very simple send
        

        
        
    ?>
    p.s. I only recently started learning to code in PHP so I bet this is painfully primitve and brutal to your eyes sorry


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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