Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2010, 11:26 AM   PM User | #1
partisanentity
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
partisanentity is an unknown quantity at this point
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!
partisanentity is offline   Reply With Quote
Old 01-07-2010, 11:29 AM   PM User | #2
partisanentity
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
partisanentity is an unknown quantity at this point
I seem to have posted this in the wrong subforum. Can a mod move it for me please? Thanks!
partisanentity is offline   Reply With Quote
Old 01-07-2010, 12:51 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 01-07-2010, 01:34 PM   PM User | #4
partisanentity
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
partisanentity is an unknown quantity at this point
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 ?
partisanentity is offline   Reply With Quote
Old 01-07-2010, 02:22 PM   PM User | #5
sir pannels
Regular Coder

 
Join Date: Jun 2002
Posts: 905
Thanks: 23
Thanked 5 Times in 5 Posts
sir pannels is an unknown quantity at this point
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
sir pannels is offline   Reply With Quote
Old 01-07-2010, 03:03 PM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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
JAY6390 is offline   Reply With Quote
Old 01-07-2010, 04:04 PM   PM User | #7
partisanentity
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
partisanentity is an unknown quantity at this point
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?
partisanentity is offline   Reply With Quote
Old 01-07-2010, 04:17 PM   PM User | #8
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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.
angst is offline   Reply With Quote
Old 01-07-2010, 04:24 PM   PM User | #9
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Old 01-07-2010, 04:28 PM   PM User | #10
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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
JAY6390 is offline   Reply With Quote
Old 01-07-2010, 04:29 PM   PM User | #11
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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.
angst is offline   Reply With Quote
Old 01-07-2010, 04:29 PM   PM User | #12
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
Also, like it's been said above you need to have a value for your selects...
JAY6390 is offline   Reply With Quote
Old 01-07-2010, 04:35 PM   PM User | #13
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,015
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
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
Rowsdower! is offline   Reply With Quote
Old 01-07-2010, 04:37 PM   PM User | #14
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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.
angst is offline   Reply With Quote
Old 01-07-2010, 04:45 PM   PM User | #15
partisanentity
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
partisanentity is an unknown quantity at this point
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
partisanentity is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:17 AM.


Advertisement
Log in to turn off these ads.